@@ -38,10 +38,15 @@ public static class RabbitMQActivitySource
38
38
private static readonly ActivitySource s_subscriberSource =
39
39
new ActivitySource ( SubscriberSourceName , AssemblyVersion ) ;
40
40
41
+ private static readonly ActivitySource s_connectionSource =
42
+ new ActivitySource ( ConnectionSourceName , AssemblyVersion ) ;
43
+
41
44
public const string PublisherSourceName = "RabbitMQ.Client.Publisher" ;
42
45
public const string SubscriberSourceName = "RabbitMQ.Client.Subscriber" ;
46
+ public const string ConnectionSourceName = "RabbitMQ.Client.Connection" ;
43
47
44
- public static Action < Activity , IDictionary < string , object ? > > ContextInjector { get ; set ; } = DefaultContextInjector ;
48
+ public static Action < Activity , IDictionary < string , object ? > > ContextInjector { get ; set ; } =
49
+ DefaultContextInjector ;
45
50
46
51
public static Func < IReadOnlyBasicProperties , ActivityContext > ContextExtractor { get ; set ; } =
47
52
DefaultContextExtractor ;
@@ -56,6 +61,19 @@ public static class RabbitMQActivitySource
56
61
new KeyValuePair < string , object ? > ( ProtocolVersion , "0.9.1" )
57
62
} ;
58
63
64
+ internal static Activity ? OpenConnection ( IFrameHandler frameHandler )
65
+ {
66
+ if ( ! s_connectionSource . HasListeners ( ) )
67
+ {
68
+ return null ;
69
+ }
70
+ Activity ? connectionActivity =
71
+ s_connectionSource . StartRabbitMQActivity ( "rabbitmq connect" , ActivityKind . Client ) ;
72
+ connectionActivity ?
73
+ . SetNetworkTags ( frameHandler ) ;
74
+ return connectionActivity ;
75
+ }
76
+
59
77
internal static Activity ? Send ( string routingKey , string exchange , int bodySize ,
60
78
ActivityContext linkedContext = default )
61
79
{
@@ -66,18 +84,21 @@ public static class RabbitMQActivitySource
66
84
67
85
Activity ? activity = linkedContext == default
68
86
? s_publisherSource . StartRabbitMQActivity (
69
- UseRoutingKeyAsOperationName ? $ "{ routingKey } { MessagingOperationTypeSend } " : MessagingOperationTypeSend ,
87
+ UseRoutingKeyAsOperationName
88
+ ? $ "{ routingKey } { MessagingOperationTypeSend } "
89
+ : MessagingOperationTypeSend ,
70
90
ActivityKind . Producer )
71
91
: s_publisherSource . StartLinkedRabbitMQActivity (
72
- UseRoutingKeyAsOperationName ? $ "{ routingKey } { MessagingOperationTypeSend } " : MessagingOperationTypeSend ,
92
+ UseRoutingKeyAsOperationName
93
+ ? $ "{ routingKey } { MessagingOperationTypeSend } "
94
+ : MessagingOperationTypeSend ,
73
95
ActivityKind . Producer , linkedContext ) ;
74
96
if ( activity != null && activity . IsAllDataRequested )
75
97
{
76
98
PopulateMessagingTags ( MessagingOperationTypeSend , routingKey , exchange , 0 , bodySize , activity ) ;
77
99
}
78
100
79
101
return activity ;
80
-
81
102
}
82
103
83
104
internal static Activity ? ReceiveEmpty ( string queue )
@@ -88,7 +109,9 @@ public static class RabbitMQActivitySource
88
109
}
89
110
90
111
Activity ? activity = s_subscriberSource . StartRabbitMQActivity (
91
- UseRoutingKeyAsOperationName ? $ "{ queue } { MessagingOperationTypeReceive } " : MessagingOperationTypeReceive ,
112
+ UseRoutingKeyAsOperationName
113
+ ? $ "{ queue } { MessagingOperationTypeReceive } "
114
+ : MessagingOperationTypeReceive ,
92
115
ActivityKind . Consumer ) ;
93
116
if ( activity != null && activity . IsAllDataRequested )
94
117
{
@@ -110,11 +133,14 @@ public static class RabbitMQActivitySource
110
133
111
134
// Extract the PropagationContext of the upstream parent from the message headers.
112
135
Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
113
- UseRoutingKeyAsOperationName ? $ "{ routingKey } { MessagingOperationTypeReceive } " : MessagingOperationTypeReceive , ActivityKind . Consumer ,
136
+ UseRoutingKeyAsOperationName
137
+ ? $ "{ routingKey } { MessagingOperationTypeReceive } "
138
+ : MessagingOperationTypeReceive , ActivityKind . Consumer ,
114
139
ContextExtractor ( readOnlyBasicProperties ) ) ;
115
140
if ( activity != null && activity . IsAllDataRequested )
116
141
{
117
- PopulateMessagingTags ( MessagingOperationTypeReceive , routingKey , exchange , deliveryTag , readOnlyBasicProperties ,
142
+ PopulateMessagingTags ( MessagingOperationTypeReceive , routingKey , exchange , deliveryTag ,
143
+ readOnlyBasicProperties ,
118
144
bodySize , activity ) ;
119
145
}
120
146
@@ -131,7 +157,9 @@ public static class RabbitMQActivitySource
131
157
132
158
// Extract the PropagationContext of the upstream parent from the message headers.
133
159
Activity ? activity = s_subscriberSource . StartLinkedRabbitMQActivity (
134
- UseRoutingKeyAsOperationName ? $ "{ routingKey } { MessagingOperationTypeProcess } " : MessagingOperationTypeProcess ,
160
+ UseRoutingKeyAsOperationName
161
+ ? $ "{ routingKey } { MessagingOperationTypeProcess } "
162
+ : MessagingOperationTypeProcess ,
135
163
ActivityKind . Consumer , ContextExtractor ( basicProperties ) ) ;
136
164
if ( activity != null && activity . IsAllDataRequested )
137
165
{
@@ -142,10 +170,19 @@ public static class RabbitMQActivitySource
142
170
return activity ;
143
171
}
144
172
173
+ internal static void ReportException ( this Activity ? activity , Exception exception )
174
+ {
175
+ activity ? . AddTag ( "exception.message" , exception . Message ) ;
176
+ activity ? . AddTag ( "exception.stacktrace" , exception . ToString ( ) ) ;
177
+ activity ? . AddTag ( "exception.type" , exception . GetType ( ) . FullName ) ;
178
+ activity ? . SetStatus ( ActivityStatusCode . Error ) ;
179
+ }
180
+
145
181
private static Activity ? StartRabbitMQActivity ( this ActivitySource source , string name , ActivityKind kind ,
146
182
ActivityContext parentContext = default )
147
183
{
148
- return source . CreateActivity ( name , kind , parentContext , idFormat : ActivityIdFormat . W3C , tags : CreationTags ) ? . Start ( ) ;
184
+ return source . CreateActivity ( name , kind , parentContext , idFormat : ActivityIdFormat . W3C , tags : CreationTags )
185
+ ? . Start ( ) ;
149
186
}
150
187
151
188
private static Activity ? StartLinkedRabbitMQActivity ( this ActivitySource source , string name , ActivityKind kind ,
@@ -273,7 +310,8 @@ private static ActivityContext DefaultContextExtractor(IReadOnlyBasicProperties
273
310
return default ;
274
311
}
275
312
276
- DistributedContextPropagator . Current . ExtractTraceIdAndState ( props . Headers , DefaultContextGetter , out string ? traceParent , out string ? traceState ) ;
313
+ DistributedContextPropagator . Current . ExtractTraceIdAndState ( props . Headers , DefaultContextGetter ,
314
+ out string ? traceParent , out string ? traceState ) ;
277
315
return ActivityContext . TryParse ( traceParent , traceState , out ActivityContext context ) ? context : default ;
278
316
}
279
317
@@ -288,7 +326,8 @@ private static void DefaultContextSetter(object? carrier, string name, string va
288
326
carrierDictionary [ name ] = value ;
289
327
}
290
328
291
- private static void DefaultContextGetter ( object ? carrier , string name , out string ? value , out IEnumerable < string > ? values )
329
+ private static void DefaultContextGetter ( object ? carrier , string name , out string ? value ,
330
+ out IEnumerable < string > ? values )
292
331
{
293
332
if ( carrier is IDictionary < string , object > carrierDict &&
294
333
carrierDict . TryGetValue ( name , out object ? propsVal ) && propsVal is byte [ ] bytes )
0 commit comments