@@ -51,7 +51,12 @@ public static class RabbitMQActivitySource
51
51
public static Func < IReadOnlyBasicProperties , ActivityContext > ContextExtractor { get ; set ; } =
52
52
DefaultContextExtractor ;
53
53
54
- public static bool UseRoutingKeyAsOperationName { get ; set ; } = true ;
54
+ public static bool UseRoutingKeyAsOperationName
55
+ {
56
+ get => TracingOptions . UseRoutingKeyAsOperationName ;
57
+ set => TracingOptions . UseRoutingKeyAsOperationName = value ;
58
+ }
59
+ public static RabbitMQTracingOptions TracingOptions { get ; set ; } = new RabbitMQTracingOptions ( ) ;
55
60
internal static bool PublisherHasListeners => s_publisherSource . HasListeners ( ) ;
56
61
57
62
internal static readonly IEnumerable < KeyValuePair < string , object ? > > CreationTags = new [ ]
@@ -115,9 +120,14 @@ public static class RabbitMQActivitySource
115
120
}
116
121
117
122
// Extract the PropagationContext of the upstream parent from the message headers.
123
+ ActivityContext linkedContext = ContextExtractor ( readOnlyBasicProperties ) ;
124
+ ActivityContext parentContext = TracingOptions . UsePublisherAsParent ? linkedContext : default ;
125
+
118
126
Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
119
127
UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicGet } { routingKey } " : MessagingOperationNameBasicGet , ActivityKind . Consumer ,
120
- ContextExtractor ( readOnlyBasicProperties ) ) ;
128
+ linkedContext , parentContext ) ;
129
+
130
+
121
131
if ( activity != null && activity . IsAllDataRequested )
122
132
{
123
133
PopulateMessagingTags ( MessagingOperationTypeReceive , MessagingOperationNameBasicGet , routingKey , exchange , deliveryTag , readOnlyBasicProperties ,
@@ -128,21 +138,24 @@ public static class RabbitMQActivitySource
128
138
}
129
139
130
140
internal static Activity ? Deliver ( string routingKey , string exchange , ulong deliveryTag ,
131
- IReadOnlyBasicProperties basicProperties , int bodySize )
141
+ IReadOnlyBasicProperties readOnlyBasicProperties , int bodySize )
132
142
{
133
143
if ( ! s_subscriberSource . HasListeners ( ) )
134
144
{
135
145
return null ;
136
146
}
137
147
138
148
// Extract the PropagationContext of the upstream parent from the message headers.
149
+ ActivityContext linkedContext = ContextExtractor ( readOnlyBasicProperties ) ;
150
+ ActivityContext parentContext = TracingOptions . UsePublisherAsParent ? linkedContext : default ;
151
+
139
152
Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
140
153
UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicDeliver } { routingKey } " : MessagingOperationNameBasicDeliver ,
141
- ActivityKind . Consumer , ContextExtractor ( basicProperties ) ) ;
154
+ ActivityKind . Consumer , linkedContext , parentContext ) ;
142
155
if ( activity != null && activity . IsAllDataRequested )
143
156
{
144
157
PopulateMessagingTags ( MessagingOperationTypeProcess , MessagingOperationNameBasicDeliver , routingKey , exchange ,
145
- deliveryTag , basicProperties , bodySize , activity ) ;
158
+ deliveryTag , readOnlyBasicProperties , bodySize , activity ) ;
146
159
}
147
160
148
161
return activity ;
@@ -157,8 +170,14 @@ public static class RabbitMQActivitySource
157
170
private static Activity ? StartLinkedRabbitMQActivity ( this ActivitySource source , string name , ActivityKind kind ,
158
171
ActivityContext linkedContext = default , ActivityContext parentContext = default )
159
172
{
173
+ List < ActivityLink > ? links = null ;
174
+ if ( linkedContext != default )
175
+ {
176
+ links = new List < ActivityLink > ( ) ;
177
+ links . Add ( new ActivityLink ( linkedContext ) ) ;
178
+ }
160
179
return source . CreateActivity ( name , kind , parentContext : parentContext ,
161
- links : new [ ] { new ActivityLink ( linkedContext ) } , idFormat : ActivityIdFormat . W3C ,
180
+ links : links , idFormat : ActivityIdFormat . W3C ,
162
181
tags : CreationTags )
163
182
? . Start ( ) ;
164
183
}
0 commit comments