@@ -16,8 +16,9 @@ internal static class ActivityDiagnosticsHelper
16
16
{
17
17
try
18
18
{
19
- Activity ? activity = ActivitySource . StartActivity ( "Confluent.Kafka.Produce" , ActivityKind . Producer ,
20
- default ( ActivityContext ) , ActivityTags ( partition ) ! ) ;
19
+ Activity ? activity = ActivitySource . StartActivity (
20
+ $ "{ partition . Topic } publish", ActivityKind . Producer ,
21
+ default ( ActivityContext ) , ProducerActivityTags ( partition ) ) ;
21
22
22
23
if ( activity == null )
23
24
return null ;
@@ -50,13 +51,40 @@ internal static class ActivityDiagnosticsHelper
50
51
}
51
52
}
52
53
53
- internal static Activity ? StartConsumeActivity < TKey , TValue > ( TopicPartition partition ,
54
- Message < TKey , TValue > message )
54
+ internal static void UpdateActivityTags < TKey , TValue > ( DeliveryResult < TKey , TValue > deliveryResult ,
55
+ Activity activity )
56
+ {
57
+ try
58
+ {
59
+ var activityStatus = deliveryResult . Status switch
60
+ {
61
+ PersistenceStatus . Persisted => ActivityStatusCode . Ok ,
62
+ PersistenceStatus . NotPersisted => ActivityStatusCode . Error ,
63
+ _ => ActivityStatusCode . Unset
64
+ } ;
65
+
66
+ activity . SetStatus ( activityStatus ) ;
67
+ if ( activityStatus == ActivityStatusCode . Ok )
68
+ {
69
+ activity . SetTag ( "messaging.kafka.destination.partition" , deliveryResult . Partition . Value . ToString ( ) ) ;
70
+ activity . SetTag ( "messaging.kafka.message.offset" , deliveryResult . Offset . Value . ToString ( ) ) ;
71
+ }
72
+ }
73
+ catch
74
+ {
75
+ // ignore
76
+ }
77
+ }
78
+
79
+ internal static Activity ? StartConsumeActivity < TKey , TValue > ( ConsumeResult < TKey , TValue > consumerResult ,
80
+ string memberId )
55
81
{
56
82
try
57
83
{
58
- var activity = ActivitySource . CreateActivity ( "Confluent.Kafka.Consume" , ActivityKind . Consumer ,
59
- default ( ActivityContext ) , ActivityTags ( partition ) ! ) ;
84
+ var message = consumerResult . Message ;
85
+ var activity = ActivitySource . CreateActivity (
86
+ $ "{ consumerResult . Topic } process", ActivityKind . Consumer ,
87
+ default ( ActivityContext ) , ConsumerActivityTags ( consumerResult , memberId ) ) ;
60
88
61
89
if ( activity != null )
62
90
{
@@ -98,19 +126,43 @@ private static void SetActivityTags<TKey, TValue>(Activity activity, Message<TKe
98
126
{
99
127
if ( message . Key != null )
100
128
{
101
- activity . SetTag ( "messaging.kafka.message_key " , message . Key . ToString ( ) ) ;
129
+ activity . SetTag ( "messaging.kafka.message.key " , message . Key . ToString ( ) ) ;
102
130
}
103
131
}
104
132
105
- private static IEnumerable < KeyValuePair < string , object > > ActivityTags ( TopicPartition partition )
133
+ private static IEnumerable < KeyValuePair < string , object ? > > ProducerActivityTags ( TopicPartition partition )
134
+ {
135
+ var list = ActivityTags ( partition , "publish" ) ;
136
+
137
+ list . Add ( new KeyValuePair < string , object ? > ( "messaging.destination.kind" , "topic" ) ) ;
138
+ list . Add ( new KeyValuePair < string , object ? > ( "messaging.destination.name" , partition . Topic ) ) ;
139
+
140
+ return list ;
141
+ }
142
+
143
+ private static IEnumerable < KeyValuePair < string , object ? > > ConsumerActivityTags < TKey , TValue > (
144
+ ConsumeResult < TKey , TValue > consumerResult , string memberId )
145
+ {
146
+ IList < KeyValuePair < string , object ? > > list = ActivityTags ( consumerResult . TopicPartition , "process" ) ;
147
+
148
+ // messaging.consumer.id - For Kafka, set it to {messaging.kafka.consumer.group} - {messaging.kafka.client_id},
149
+ // if both are present, or only messaging.kafka.consumer.group
150
+ list . Add ( new ( "messaging.source.kind" , "topic" ) ) ;
151
+ list . Add ( new ( "messaging.source.name" , consumerResult . Topic ) ) ;
152
+ list . Add ( new ( "messaging.kafka.source.partition" , consumerResult . Partition . Value . ToString ( ) ) ) ;
153
+ list . Add ( new ( "messaging.kafka.message.offset" , consumerResult . Offset . Value . ToString ( ) ) ) ;
154
+ list . Add ( new ( "messaging.kafka.client_id" , memberId ) ) ;
155
+
156
+ // messaging.kafka.consumer.group - there is no way to access this information from the consumer
157
+
158
+ return list ;
159
+ }
160
+
161
+ private static IList < KeyValuePair < string , object ? > > ActivityTags ( TopicPartition partition , string operation )
106
162
{
107
- return new [ ]
163
+ return new List < KeyValuePair < string , object ? > > ( )
108
164
{
109
- new KeyValuePair < string , object > ( "messaging.system" , "kafka" ) ,
110
- new KeyValuePair < string , object > ( "messaging.destination" , partition . Topic ) ,
111
- new KeyValuePair < string , object > ( "messaging.destination_kind" , "topic" ) , new KeyValuePair < string , object > (
112
- "messaging.kafka.partition" ,
113
- partition . Partition . ToString ( ) )
165
+ new ( "messaging.system" , "kafka" ) , new ( "messaging.operation" , operation )
114
166
} ;
115
167
}
116
168
}
0 commit comments