7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
9
10
- using OpenTelemetry ;
11
- using OpenTelemetry . Context . Propagation ;
12
-
13
10
using RabbitMQ . Client . Events ;
14
11
using RabbitMQ . Client . Framing . Impl ;
15
12
@@ -18,7 +15,6 @@ namespace RabbitMQ.Client
18
15
internal class RabbitMQActivitySource
19
16
{
20
17
internal static ActivitySource source = new ActivitySource ( "RabbitMQ.Client" , typeof ( RabbitMQActivitySource ) . Assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion ) ;
21
- private static readonly TextMapPropagator Propagator = new CompositeTextMapPropagator ( new TextMapPropagator [ ] { new TraceContextPropagator ( ) , new BaggagePropagator ( ) } ) ;
22
18
23
19
static RabbitMQActivitySource ( )
24
20
{
@@ -46,11 +42,20 @@ internal static Activity Receive(string routingKey, string exchange, ulong deliv
46
42
if ( source . HasListeners ( ) )
47
43
{
48
44
// Extract the PropagationContext of the upstream parent from the message headers.
49
- PropagationContext parentContext = Propagator . Extract ( default , readOnlyBasicProperties , ExtractTraceContextFromBasicProperties ) ;
50
- Baggage . Current = parentContext . Baggage ;
51
- Activity activity = StartRabbitMQActivity ( $ "{ routingKey } receive", ActivityKind . Consumer , parentContext . ActivityContext ) ;
45
+ DistributedContextPropagator . Current . ExtractTraceIdAndState ( readOnlyBasicProperties , ExtractTraceIdAndState , out string traceId , out string traceState ) ;
46
+ IEnumerable < KeyValuePair < string , string > > baggage = DistributedContextPropagator . Current . ExtractBaggage ( readOnlyBasicProperties , ExtractTraceIdAndState ) ;
47
+ ActivityContext . TryParse ( traceId , traceState , out ActivityContext parentContext ) ;
48
+ Activity activity = StartRabbitMQActivity ( $ "{ routingKey } receive", ActivityKind . Consumer , parentContext ) ;
52
49
if ( activity != null && activity . IsAllDataRequested )
53
50
{
51
+ if ( baggage != null )
52
+ {
53
+ foreach ( var item in baggage )
54
+ {
55
+ Activity . Current ? . SetBaggage ( item . Key , item . Value ) ;
56
+ }
57
+ }
58
+
54
59
PopulateMessagingTags ( "receive" , routingKey , exchange , deliveryTag , readOnlyBasicProperties , bodySize , activity ) ;
55
60
}
56
61
@@ -65,12 +70,20 @@ internal static Activity Process(BasicDeliverEventArgs deliverEventArgs)
65
70
if ( source . HasListeners ( ) )
66
71
{
67
72
// Extract the PropagationContext of the upstream parent from the message headers.
68
- PropagationContext parentContext = Propagator . Extract ( default , deliverEventArgs . BasicProperties , ExtractTraceContextFromBasicProperties ) ;
69
- Baggage . Current = parentContext . Baggage ;
70
-
71
- Activity activity = StartRabbitMQActivity ( $ "{ deliverEventArgs . RoutingKey } process", ActivityKind . Consumer , parentContext . ActivityContext ) ;
73
+ DistributedContextPropagator . Current . ExtractTraceIdAndState ( deliverEventArgs . BasicProperties , ExtractTraceIdAndState , out string traceId , out string traceState ) ;
74
+ IEnumerable < KeyValuePair < string , string > > baggage = DistributedContextPropagator . Current . ExtractBaggage ( deliverEventArgs . BasicProperties , ExtractTraceIdAndState ) ;
75
+ ActivityContext . TryParse ( traceId , traceState , out ActivityContext parentContext ) ;
76
+ Activity activity = StartRabbitMQActivity ( $ "{ deliverEventArgs . RoutingKey } process", ActivityKind . Consumer , parentContext ) ;
72
77
if ( activity != null && activity . IsAllDataRequested )
73
78
{
79
+ if ( baggage != null )
80
+ {
81
+ foreach ( var item in baggage )
82
+ {
83
+ Activity . Current ? . SetBaggage ( item . Key , item . Value ) ;
84
+ }
85
+ }
86
+
74
87
PopulateMessagingTags ( "process" , deliverEventArgs . RoutingKey , deliverEventArgs . Exchange , deliverEventArgs . DeliveryTag , deliverEventArgs . BasicProperties , deliverEventArgs . Body . Length , activity ) ;
75
88
}
76
89
@@ -123,12 +136,18 @@ private static void PopulateMessagingTags(string operation, string routingKey, s
123
136
}
124
137
}
125
138
}
126
-
127
- static IEnumerable < string > ExtractTraceContextFromBasicProperties < T > ( T props , string key ) where T : IReadOnlyBasicProperties
139
+
140
+ private static void ExtractTraceIdAndState ( object carrier , string name , out string value , out IEnumerable < string > values )
128
141
{
129
- if ( props . Headers . TryGetValue ( key , out var value ) && value is byte [ ] bytes )
142
+ if ( carrier is IReadOnlyBasicProperties props && props . Headers is not null && props . Headers . TryGetValue ( name , out object propsVal ) && propsVal is byte [ ] bytes )
143
+ {
144
+ value = Encoding . UTF8 . GetString ( bytes ) ;
145
+ values = default ;
146
+ }
147
+ else
130
148
{
131
- yield return Encoding . UTF8 . GetString ( bytes ) ;
149
+ value = default ;
150
+ values = default ;
132
151
}
133
152
}
134
153
}
0 commit comments