@@ -51,7 +51,12 @@ public static class RabbitMQActivitySource
5151 public static Func < IReadOnlyBasicProperties , ActivityContext > ContextExtractor { get ; set ; } =
5252 DefaultContextExtractor ;
5353
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 ( ) ;
5560 internal static bool PublisherHasListeners => s_publisherSource . HasListeners ( ) ;
5661
5762 internal static readonly IEnumerable < KeyValuePair < string , object ? > > CreationTags = new [ ]
@@ -115,9 +120,14 @@ public static class RabbitMQActivitySource
115120 }
116121
117122 // Extract the PropagationContext of the upstream parent from the message headers.
123+ ActivityContext linkedContext = ContextExtractor ( readOnlyBasicProperties ) ;
124+ ActivityContext parentContext = TracingOptions . UsePublisherAsParent ? linkedContext : default ;
125+
118126 Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
119127 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicGet } { routingKey } " : MessagingOperationNameBasicGet , ActivityKind . Consumer ,
120- ContextExtractor ( readOnlyBasicProperties ) ) ;
128+ linkedContext , parentContext ) ;
129+
130+
121131 if ( activity != null && activity . IsAllDataRequested )
122132 {
123133 PopulateMessagingTags ( MessagingOperationTypeReceive , MessagingOperationNameBasicGet , routingKey , exchange , deliveryTag , readOnlyBasicProperties ,
@@ -128,21 +138,24 @@ public static class RabbitMQActivitySource
128138 }
129139
130140 internal static Activity ? Deliver ( string routingKey , string exchange , ulong deliveryTag ,
131- IReadOnlyBasicProperties basicProperties , int bodySize )
141+ IReadOnlyBasicProperties readOnlyBasicProperties , int bodySize )
132142 {
133143 if ( ! s_subscriberSource . HasListeners ( ) )
134144 {
135145 return null ;
136146 }
137147
138148 // Extract the PropagationContext of the upstream parent from the message headers.
149+ ActivityContext linkedContext = ContextExtractor ( readOnlyBasicProperties ) ;
150+ ActivityContext parentContext = TracingOptions . UsePublisherAsParent ? linkedContext : default ;
151+
139152 Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
140153 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicDeliver } { routingKey } " : MessagingOperationNameBasicDeliver ,
141- ActivityKind . Consumer , ContextExtractor ( basicProperties ) ) ;
154+ ActivityKind . Consumer , linkedContext , parentContext ) ;
142155 if ( activity != null && activity . IsAllDataRequested )
143156 {
144157 PopulateMessagingTags ( MessagingOperationTypeProcess , MessagingOperationNameBasicDeliver , routingKey , exchange ,
145- deliveryTag , basicProperties , bodySize , activity ) ;
158+ deliveryTag , readOnlyBasicProperties , bodySize , activity ) ;
146159 }
147160
148161 return activity ;
@@ -157,8 +170,14 @@ public static class RabbitMQActivitySource
157170 private static Activity ? StartLinkedRabbitMQActivity ( this ActivitySource source , string name , ActivityKind kind ,
158171 ActivityContext linkedContext = default , ActivityContext parentContext = default )
159172 {
173+ List < ActivityLink > ? links = null ;
174+ if ( linkedContext != default )
175+ {
176+ links = new List < ActivityLink > ( ) ;
177+ links . Add ( new ActivityLink ( linkedContext ) ) ;
178+ }
160179 return source . CreateActivity ( name , kind , parentContext : parentContext ,
161- links : new [ ] { new ActivityLink ( linkedContext ) } , idFormat : ActivityIdFormat . W3C ,
180+ links : links , idFormat : ActivityIdFormat . W3C ,
162181 tags : CreationTags )
163182 ? . Start ( ) ;
164183 }
0 commit comments