Skip to content

Commit 9bb228a

Browse files
committed
chore: use system.diag in version 9 to have AddException method clean has listener
1 parent ebe12de commit 9bb228a

File tree

5 files changed

+9
-49
lines changed

5 files changed

+9
-49
lines changed

projects/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageVersion Include="OpenTelemetry.Api" Version="1.9.0" />
1111
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="1.9.0" />
1212
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
13+
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="9.0.4" />
1314
<!--
1415
Note: do NOT upgrade the System.IO.Pipelines dependency unless necessary
1516
See https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/1481#pullrequestreview-1847905299
@@ -44,4 +45,4 @@
4445
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
4546
<GlobalPackageReference Include="MinVer" Version="6.0.0" />
4647
</ItemGroup>
47-
</Project>
48+
</Project>

projects/RabbitMQ.Client/Impl/Channel.BasicPublish.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ await MaybeEnforceFlowControlAsync(cancellationToken)
6060

6161
var cmd = new BasicPublish(exchange, routingKey, mandatory, default);
6262

63-
using Activity? sendActivity = RabbitMQActivitySource.PublisherHasListeners
64-
? RabbitMQActivitySource.BasicPublish(routingKey, exchange, body.Length)
65-
: default;
63+
using Activity? sendActivity = RabbitMQActivitySource.BasicPublish(routingKey, exchange, body.Length);
6664

6765
ulong publishSequenceNumber = 0;
6866
if (publisherConfirmationInfo is not null)
@@ -115,9 +113,7 @@ await MaybeEnforceFlowControlAsync(cancellationToken)
115113

116114
var cmd = new BasicPublishMemory(exchange.Bytes, routingKey.Bytes, mandatory, default);
117115

118-
using Activity? sendActivity = RabbitMQActivitySource.PublisherHasListeners
119-
? RabbitMQActivitySource.BasicPublish(routingKey.Value, exchange.Value, body.Length)
120-
: default;
116+
using Activity? sendActivity = RabbitMQActivitySource.BasicPublish(routingKey.Value, exchange.Value, body.Length);
121117

122118
ulong publishSequenceNumber = 0;
123119
if (publisherConfirmationInfo is not null)

projects/RabbitMQ.Client/Impl/Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ internal void TakeOver(Connection other)
228228
internal async ValueTask<IConnection> OpenAsync(CancellationToken cancellationToken)
229229
{
230230
cancellationToken.ThrowIfCancellationRequested();
231-
Activity? connectionActivity = RabbitMQActivitySource.OpenConnection(_frameHandler);
231+
using Activity? connectionActivity = RabbitMQActivitySource.OpenConnection(_frameHandler);
232232
try
233233
{
234234
RabbitMqClientEventSource.Log.ConnectionOpened();
@@ -251,7 +251,7 @@ await _channel0.ConnectionOpenAsync(_config.VirtualHost, cancellationToken)
251251
}
252252
catch (Exception ex)
253253
{
254-
connectionActivity?.ReportException(ex);
254+
connectionActivity?.AddException(ex);
255255
try
256256
{
257257
var ea = new ShutdownEventArgs(ShutdownInitiator.Library, Constants.InternalError, "FailedOpen");

projects/RabbitMQ.Client/Impl/RabbitMQActivitySource.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public static class RabbitMQActivitySource
6363
DefaultContextExtractor;
6464

6565
public static bool UseRoutingKeyAsOperationName { get; set; } = true;
66-
internal static bool PublisherHasListeners => s_publisherSource.HasListeners();
6766

6867
internal static readonly IEnumerable<KeyValuePair<string, object?>> CreationTags = new[]
6968
{
@@ -74,11 +73,6 @@ public static class RabbitMQActivitySource
7473

7574
internal static Activity? OpenConnection(IFrameHandler frameHandler)
7675
{
77-
if (!s_connectionSource.HasListeners())
78-
{
79-
return null;
80-
}
81-
8276
Activity? connectionActivity =
8377
s_connectionSource.StartRabbitMQActivity("connection attempt", ActivityKind.Client);
8478
connectionActivity?
@@ -89,11 +83,6 @@ public static class RabbitMQActivitySource
8983
internal static Activity? BasicPublish(string routingKey, string exchange, int bodySize,
9084
ActivityContext linkedContext = default)
9185
{
92-
if (!s_publisherSource.HasListeners())
93-
{
94-
return null;
95-
}
96-
9786
Activity? activity = linkedContext == default
9887
? s_publisherSource.StartRabbitMQActivity(
9988
UseRoutingKeyAsOperationName ? $"{MessagingOperationNameBasicPublish} {routingKey}" : MessagingOperationNameBasicPublish,
@@ -111,11 +100,6 @@ public static class RabbitMQActivitySource
111100

112101
internal static Activity? BasicGetEmpty(string queue)
113102
{
114-
if (!s_subscriberSource.HasListeners())
115-
{
116-
return null;
117-
}
118-
119103
Activity? activity = s_subscriberSource.StartRabbitMQActivity(
120104
UseRoutingKeyAsOperationName ? $"{MessagingOperationNameBasicGetEmpty} {queue}" : MessagingOperationNameBasicGetEmpty,
121105
ActivityKind.Consumer);
@@ -133,11 +117,6 @@ public static class RabbitMQActivitySource
133117
internal static Activity? BasicGet(string routingKey, string exchange, ulong deliveryTag,
134118
IReadOnlyBasicProperties readOnlyBasicProperties, int bodySize)
135119
{
136-
if (!s_subscriberSource.HasListeners())
137-
{
138-
return null;
139-
}
140-
141120
// Extract the PropagationContext of the upstream parent from the message headers.
142121
Activity? activity = s_subscriberSource.StartLinkedRabbitMQActivity(
143122
UseRoutingKeyAsOperationName ? $"{MessagingOperationNameBasicGet} {routingKey}" : MessagingOperationNameBasicGet, ActivityKind.Consumer,
@@ -154,11 +133,6 @@ public static class RabbitMQActivitySource
154133
internal static Activity? Deliver(string routingKey, string exchange, ulong deliveryTag,
155134
IReadOnlyBasicProperties basicProperties, int bodySize)
156135
{
157-
if (!s_subscriberSource.HasListeners())
158-
{
159-
return null;
160-
}
161-
162136
// Extract the PropagationContext of the upstream parent from the message headers.
163137
Activity? activity = s_subscriberSource.StartLinkedRabbitMQActivity(
164138
UseRoutingKeyAsOperationName ? $"{MessagingOperationNameBasicDeliver} {routingKey}" : MessagingOperationNameBasicDeliver,
@@ -172,17 +146,6 @@ public static class RabbitMQActivitySource
172146
return activity;
173147
}
174148

175-
internal static void ReportException(this Activity activity, Exception exception)
176-
{
177-
ActivityTagsCollection exceptionTags = new();
178-
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionMessageTag, exception.Message));
179-
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString()));
180-
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionTypeTag, exception.GetType().ToString()));
181-
activity.AddEvent(new ActivityEvent(ExceptionEventName, default, exceptionTags));
182-
183-
activity.SetStatus(ActivityStatusCode.Error);
184-
}
185-
186149
private static Activity? StartRabbitMQActivity(this ActivitySource source, string name, ActivityKind kind,
187150
ActivityContext parentContext = default)
188151
{
@@ -232,15 +195,15 @@ private static void PopulateMessagingTags(string operationType, string operation
232195

233196
internal static void PopulateMessageEnvelopeSize(Activity? activity, int size)
234197
{
235-
if (activity != null && activity.IsAllDataRequested && PublisherHasListeners)
198+
if (activity?.IsAllDataRequested ?? false)
236199
{
237200
activity.SetTag(MessagingEnvelopeSize, size);
238201
}
239202
}
240203

241204
internal static void SetNetworkTags(this Activity? activity, IFrameHandler frameHandler)
242205
{
243-
if (PublisherHasListeners && activity != null && activity.IsAllDataRequested)
206+
if (activity?.IsAllDataRequested ?? false)
244207
{
245208
switch (frameHandler.RemoteEndPoint.AddressFamily)
246209
{

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<PackageReference Include="System.IO.Pipelines" />
6868
<PackageReference Include="System.Threading.RateLimiting" />
6969
<PackageReference Include="Nullable" PrivateAssets="all" />
70+
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
7071
</ItemGroup>
7172

7273
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
@@ -76,7 +77,6 @@
7677
* https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/1481#pullrequestreview-1847905299
7778
* https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/1594
7879
-->
79-
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
8080
<PackageReference Include="System.Memory" />
8181
<PackageReference Include="System.Threading.Channels" />
8282
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />

0 commit comments

Comments
 (0)