Skip to content

Commit f9f0400

Browse files
committed
Adding missing check for listeners before trying to extract propagation activites.
1 parent 2fb27f3 commit f9f0400

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

projects/RabbitMQ.Client/client/impl/RabbitMQActivitySource.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,57 @@ static RabbitMQActivitySource()
2828

2929
internal static Activity Send(BasicPublish basicPublish, int bodySize)
3030
{
31-
Activity activity = StartRabbitMQActivity($"{basicPublish._routingKey} send", ActivityKind.Producer, Activity.Current?.Context ?? default);
32-
if (activity != null && activity.IsAllDataRequested)
31+
if (source.HasListeners())
3332
{
34-
PopulateMessagingTags("send", basicPublish._routingKey, basicPublish._exchange, 0, null, bodySize, activity);
35-
}
33+
Activity activity = StartRabbitMQActivity($"{basicPublish._routingKey} send", ActivityKind.Producer, Activity.Current?.Context ?? default);
34+
if (activity != null && activity.IsAllDataRequested)
35+
{
36+
PopulateMessagingTags("send", basicPublish._routingKey, basicPublish._exchange, 0, null, bodySize, activity);
37+
}
3638

37-
return activity;
39+
return activity;
40+
}
41+
42+
return null;
3843
}
3944

4045
internal static Activity Receive(string routingKey, string exchange, ulong deliveryTag, IReadOnlyBasicProperties readOnlyBasicProperties, int bodySize)
4146
{
42-
// Extract the PropagationContext of the upstream parent from the message headers.
43-
PropagationContext parentContext = Propagator.Extract(default, readOnlyBasicProperties, ExtractTraceContextFromBasicProperties);
44-
Baggage.Current = parentContext.Baggage;
45-
46-
Activity activity = StartRabbitMQActivity($"{routingKey} receive", ActivityKind.Consumer, parentContext.ActivityContext);
47-
if (activity != null && activity.IsAllDataRequested)
47+
if (source.HasListeners())
4848
{
49-
PopulateMessagingTags("receive", routingKey, exchange, deliveryTag, readOnlyBasicProperties, bodySize, activity);
49+
// Extract the PropagationContext of the upstream parent from the message headers.
50+
PropagationContext parentContext = Propagator.Extract(default, readOnlyBasicProperties, ExtractTraceContextFromBasicProperties);
51+
Baggage.Current = parentContext.Baggage;
52+
Activity activity = StartRabbitMQActivity($"{routingKey} receive", ActivityKind.Consumer, parentContext.ActivityContext);
53+
if (activity != null && activity.IsAllDataRequested)
54+
{
55+
PopulateMessagingTags("receive", routingKey, exchange, deliveryTag, readOnlyBasicProperties, bodySize, activity);
56+
}
57+
58+
return activity;
5059
}
5160

52-
return activity;
61+
return null;
5362
}
5463

5564
internal static Activity Process(BasicDeliverEventArgs deliverEventArgs)
5665
{
57-
// Extract the PropagationContext of the upstream parent from the message headers.
58-
PropagationContext parentContext = Propagator.Extract(default, deliverEventArgs.BasicProperties, ExtractTraceContextFromBasicProperties);
59-
Baggage.Current = parentContext.Baggage;
60-
61-
Activity activity = StartRabbitMQActivity($"{deliverEventArgs.RoutingKey} process", ActivityKind.Consumer, parentContext.ActivityContext);
62-
if (activity != null && activity.IsAllDataRequested)
66+
if (source.HasListeners())
6367
{
64-
PopulateMessagingTags("process", deliverEventArgs.RoutingKey, deliverEventArgs.Exchange, deliverEventArgs.DeliveryTag, deliverEventArgs.BasicProperties, deliverEventArgs.Body.Length, activity);
68+
// Extract the PropagationContext of the upstream parent from the message headers.
69+
PropagationContext parentContext = Propagator.Extract(default, deliverEventArgs.BasicProperties, ExtractTraceContextFromBasicProperties);
70+
Baggage.Current = parentContext.Baggage;
71+
72+
Activity activity = StartRabbitMQActivity($"{deliverEventArgs.RoutingKey} process", ActivityKind.Consumer, parentContext.ActivityContext);
73+
if (activity != null && activity.IsAllDataRequested)
74+
{
75+
PopulateMessagingTags("process", deliverEventArgs.RoutingKey, deliverEventArgs.Exchange, deliverEventArgs.DeliveryTag, deliverEventArgs.BasicProperties, deliverEventArgs.Body.Length, activity);
76+
}
77+
78+
return activity;
6579
}
6680

67-
return activity;
81+
return null;
6882
}
6983

7084
private static Activity StartRabbitMQActivity(string name, ActivityKind kind, ActivityContext parentContext = default)
@@ -115,10 +129,8 @@ static IEnumerable<string> ExtractTraceContextFromBasicProperties<T>(T props, st
115129
{
116130
if (props.Headers.TryGetValue(key, out var value) && value is byte[] bytes)
117131
{
118-
return new[] { Encoding.UTF8.GetString(bytes) };
132+
yield return Encoding.UTF8.GetString(bytes);
119133
}
120-
121-
return Enumerable.Empty<string>();
122134
}
123135
}
124136
}

0 commit comments

Comments
 (0)