@@ -28,43 +28,57 @@ static RabbitMQActivitySource()
28
28
29
29
internal static Activity Send ( BasicPublish basicPublish , int bodySize )
30
30
{
31
- Activity activity = StartRabbitMQActivity ( $ "{ basicPublish . _routingKey } send", ActivityKind . Producer , Activity . Current ? . Context ?? default ) ;
32
- if ( activity != null && activity . IsAllDataRequested )
31
+ if ( source . HasListeners ( ) )
33
32
{
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
+ }
36
38
37
- return activity ;
39
+ return activity ;
40
+ }
41
+
42
+ return null ;
38
43
}
39
44
40
45
internal static Activity Receive ( string routingKey , string exchange , ulong deliveryTag , IReadOnlyBasicProperties readOnlyBasicProperties , int bodySize )
41
46
{
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 ( ) )
48
48
{
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 ;
50
59
}
51
60
52
- return activity ;
61
+ return null ;
53
62
}
54
63
55
64
internal static Activity Process ( BasicDeliverEventArgs deliverEventArgs )
56
65
{
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 ( ) )
63
67
{
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 ;
65
79
}
66
80
67
- return activity ;
81
+ return null ;
68
82
}
69
83
70
84
private static Activity StartRabbitMQActivity ( string name , ActivityKind kind , ActivityContext parentContext = default )
@@ -115,10 +129,8 @@ static IEnumerable<string> ExtractTraceContextFromBasicProperties<T>(T props, st
115
129
{
116
130
if ( props . Headers . TryGetValue ( key , out var value ) && value is byte [ ] bytes )
117
131
{
118
- return new [ ] { Encoding . UTF8 . GetString ( bytes ) } ;
132
+ yield return Encoding . UTF8 . GetString ( bytes ) ;
119
133
}
120
-
121
- return Enumerable . Empty < string > ( ) ;
122
134
}
123
135
}
124
136
}
0 commit comments