@@ -43,16 +43,20 @@ public static class RabbitMQActivitySource
4343 private static readonly ActivitySource s_subscriberSource =
4444 new ActivitySource ( SubscriberSourceName , AssemblyVersion ) ;
4545
46+ private static readonly ActivitySource s_connectionSource =
47+ new ActivitySource ( ConnectionSourceName , AssemblyVersion ) ;
48+
4649 public const string PublisherSourceName = "RabbitMQ.Client.Publisher" ;
4750 public const string SubscriberSourceName = "RabbitMQ.Client.Subscriber" ;
51+ public const string ConnectionSourceName = "RabbitMQ.Client.Connection" ;
4852
49- public static Action < Activity , IDictionary < string , object ? > > ContextInjector { get ; set ; } = DefaultContextInjector ;
53+ public static Action < Activity , IDictionary < string , object ? > > ContextInjector { get ; set ; } =
54+ DefaultContextInjector ;
5055
5156 public static Func < IReadOnlyBasicProperties , ActivityContext > ContextExtractor { get ; set ; } =
5257 DefaultContextExtractor ;
5358
5459 public static bool UseRoutingKeyAsOperationName { get ; set ; } = true ;
55- internal static bool PublisherHasListeners => s_publisherSource . HasListeners ( ) ;
5660
5761 internal static readonly IEnumerable < KeyValuePair < string , object ? > > CreationTags = new [ ]
5862 {
@@ -61,14 +65,24 @@ public static class RabbitMQActivitySource
6165 new KeyValuePair < string , object ? > ( ProtocolVersion , "0.9.1" )
6266 } ;
6367
68+ internal static Activity ? OpenConnection ( bool isReconnection )
69+ {
70+ Activity ? connectionActivity =
71+ s_connectionSource . StartRabbitMQActivity ( "connection attempt" , ActivityKind . Client ) ;
72+ connectionActivity ? . SetTag ( "messaging.rabbitmq.connection.is_reconnection" , isReconnection ) ;
73+ return connectionActivity ;
74+ }
75+
76+ internal static Activity ? OpenTcpConnection ( )
77+ {
78+ Activity ? connectionActivity =
79+ s_connectionSource . StartRabbitMQActivity ( "tcp connection attempt" , ActivityKind . Client ) ;
80+ return connectionActivity ;
81+ }
82+
6483 internal static Activity ? BasicPublish ( string routingKey , string exchange , int bodySize ,
6584 ActivityContext linkedContext = default )
6685 {
67- if ( ! s_publisherSource . HasListeners ( ) )
68- {
69- return null ;
70- }
71-
7286 Activity ? activity = linkedContext == default
7387 ? s_publisherSource . StartRabbitMQActivity (
7488 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicPublish } { routingKey } " : MessagingOperationNameBasicPublish ,
@@ -82,16 +96,10 @@ public static class RabbitMQActivitySource
8296 }
8397
8498 return activity ;
85-
8699 }
87100
88101 internal static Activity ? BasicGetEmpty ( string queue )
89102 {
90- if ( ! s_subscriberSource . HasListeners ( ) )
91- {
92- return null ;
93- }
94-
95103 Activity ? activity = s_subscriberSource . StartRabbitMQActivity (
96104 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicGetEmpty } { queue } " : MessagingOperationNameBasicGetEmpty ,
97105 ActivityKind . Consumer ) ;
@@ -109,11 +117,6 @@ public static class RabbitMQActivitySource
109117 internal static Activity ? BasicGet ( string routingKey , string exchange , ulong deliveryTag ,
110118 IReadOnlyBasicProperties readOnlyBasicProperties , int bodySize )
111119 {
112- if ( ! s_subscriberSource . HasListeners ( ) )
113- {
114- return null ;
115- }
116-
117120 // Extract the PropagationContext of the upstream parent from the message headers.
118121 Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
119122 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicGet } { routingKey } " : MessagingOperationNameBasicGet , ActivityKind . Consumer ,
@@ -130,11 +133,6 @@ public static class RabbitMQActivitySource
130133 internal static Activity ? Deliver ( string routingKey , string exchange , ulong deliveryTag ,
131134 IReadOnlyBasicProperties basicProperties , int bodySize )
132135 {
133- if ( ! s_subscriberSource . HasListeners ( ) )
134- {
135- return null ;
136- }
137-
138136 // Extract the PropagationContext of the upstream parent from the message headers.
139137 Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
140138 UseRoutingKeyAsOperationName ? $ "{ MessagingOperationNameBasicDeliver } { routingKey } " : MessagingOperationNameBasicDeliver ,
@@ -197,15 +195,15 @@ private static void PopulateMessagingTags(string operationType, string operation
197195
198196 internal static void PopulateMessageEnvelopeSize ( Activity ? activity , int size )
199197 {
200- if ( activity != null && activity . IsAllDataRequested && PublisherHasListeners )
198+ if ( activity ? . IsAllDataRequested ?? false )
201199 {
202200 activity . SetTag ( MessagingEnvelopeSize , size ) ;
203201 }
204202 }
205203
206204 internal static void SetNetworkTags ( this Activity ? activity , IFrameHandler frameHandler )
207205 {
208- if ( PublisherHasListeners && activity != null && activity . IsAllDataRequested )
206+ if ( activity ? . IsAllDataRequested ?? false )
209207 {
210208 switch ( frameHandler . RemoteEndPoint . AddressFamily )
211209 {
@@ -216,15 +214,7 @@ internal static void SetNetworkTags(this Activity? activity, IFrameHandler frame
216214 activity . SetTag ( "network.type" , "ipv4" ) ;
217215 break ;
218216 }
219-
220- if ( ! string . IsNullOrEmpty ( frameHandler . Endpoint . HostName ) )
221- {
222- activity
223- . SetTag ( "server.address" , frameHandler . Endpoint . HostName ) ;
224- }
225-
226- activity
227- . SetTag ( "server.port" , frameHandler . Endpoint . Port ) ;
217+ activity . SetServerTags ( frameHandler . Endpoint ) ;
228218
229219 if ( frameHandler . RemoteEndPoint is IPEndPoint ipEndpoint )
230220 {
@@ -252,6 +242,18 @@ internal static void SetNetworkTags(this Activity? activity, IFrameHandler frame
252242 }
253243 }
254244
245+ internal static void SetServerTags ( this Activity activity , AmqpTcpEndpoint endpoint )
246+ {
247+ if ( ! string . IsNullOrEmpty ( endpoint . HostName ) )
248+ {
249+ activity
250+ . SetTag ( "server.address" , endpoint . HostName ) ;
251+ }
252+
253+ activity
254+ . SetTag ( "server.port" , endpoint . Port ) ;
255+ }
256+
255257 private static void DefaultContextInjector ( Activity sendActivity , IDictionary < string , object ? > props )
256258 {
257259 DistributedContextPropagator . Current . Inject ( sendActivity , props , DefaultContextSetter ) ;
0 commit comments