1
- using System . Diagnostics ;
1
+ using System . Diagnostics ;
2
2
using System . Text ;
3
3
4
4
namespace Confluent . Kafka . Extensions . Diagnostics ;
5
5
6
6
internal static class ActivityDiagnosticsHelper
7
7
{
8
8
private const string ActivitySourceName = "Confluent.Kafka.Extensions.Diagnostics" ;
9
+ private const string TraceParentHeaderName = "traceparent" ;
10
+ private const string TraceStateHeaderName = "tracestate" ;
9
11
10
12
private static ActivitySource ActivitySource { get ; } = new ( ActivitySourceName ) ;
11
13
12
- internal static Activity ? Start < TKey , TValue > ( TopicPartition partition , Message < TKey , TValue > message )
14
+ internal static Activity ? StartProduceActivity < TKey , TValue > ( TopicPartition partition ,
15
+ Message < TKey , TValue > message )
13
16
{
14
17
try
15
18
{
16
- Activity ? activity = ActivitySource . StartActivity ( "Confluent.Kafka.Produce" , ActivityKind . Client ,
17
- default ( ActivityContext ) ,
18
- new [ ]
19
- {
20
- new KeyValuePair < string , object > ( "messaging.system" , "kafka" ) ,
21
- new KeyValuePair < string , object > ( "messaging.destination" , partition . Topic ) ,
22
- new KeyValuePair < string , object > ( "messaging.destination_kind" , "topic" ) ,
23
- new KeyValuePair < string , object > ( "messaging.kafka.partition" , partition . Partition . ToString ( ) )
24
- } ! ) ;
25
-
26
- if ( activity == null ) return null ;
19
+ Activity ? activity = ActivitySource . StartActivity ( "Confluent.Kafka.Produce" , ActivityKind . Producer ,
20
+ default ( ActivityContext ) , ActivityTags ( partition ) ! ) ;
21
+
22
+ if ( activity == null )
23
+ return null ;
27
24
28
25
if ( activity . IsAllDataRequested )
29
26
{
30
- if ( message . Key != null )
31
- {
32
- activity . SetTag ( "messaging.kafka.message_key" , message . Key . ToString ( ) ) ;
33
- }
34
-
35
- if ( message . Value != null )
36
- {
37
- int messagePayloadBytes = Encoding . UTF8 . GetByteCount ( message . Value . ToString ( ) ! ) ;
38
- activity . AddTag ( "messaging.message_payload_size_bytes" , messagePayloadBytes . ToString ( ) ) ;
39
- }
27
+ SetActivityTags ( activity , message ) ;
40
28
}
41
29
42
30
if ( message . Headers == null )
@@ -45,12 +33,12 @@ internal static class ActivityDiagnosticsHelper
45
33
}
46
34
47
35
if ( activity . Id != null )
48
- message . Headers . Add ( "traceparent" , Encoding . UTF8 . GetBytes ( activity . Id ) ) ;
36
+ message . Headers . Add ( TraceParentHeaderName , Encoding . UTF8 . GetBytes ( activity . Id ) ) ;
49
37
50
38
var tracestateStr = activity . Context . TraceState ;
51
39
if ( tracestateStr ? . Length > 0 )
52
40
{
53
- message . Headers . Add ( "tracestate" , Encoding . UTF8 . GetBytes ( tracestateStr ) ) ;
41
+ message . Headers . Add ( TraceStateHeaderName , Encoding . UTF8 . GetBytes ( tracestateStr ) ) ;
54
42
}
55
43
56
44
return activity ;
@@ -61,4 +49,60 @@ internal static class ActivityDiagnosticsHelper
61
49
return null ;
62
50
}
63
51
}
52
+
53
+ internal static Activity ? StartConsumeActivity < TKey , TValue > ( TopicPartition partition ,
54
+ Message < TKey , TValue > message )
55
+ {
56
+ var activity = ActivitySource . CreateActivity ( "Confluent.Kafka.Consume" , ActivityKind . Consumer ,
57
+ default ( ActivityContext ) , ActivityTags ( partition ) ! ) ;
58
+
59
+ if ( activity != null )
60
+ {
61
+ var traceParentHeader = message . Headers ? . FirstOrDefault ( x => x . Key == TraceParentHeaderName ) ;
62
+ var traceStateHeader = message . Headers ? . FirstOrDefault ( x => x . Key == TraceStateHeaderName ) ;
63
+
64
+ var traceParent = traceParentHeader != null
65
+ ? Encoding . UTF8 . GetString ( traceParentHeader . GetValueBytes ( ) )
66
+ : null ;
67
+ var traceState = traceStateHeader != null
68
+ ? Encoding . UTF8 . GetString ( traceStateHeader . GetValueBytes ( ) )
69
+ : null ;
70
+
71
+ if ( ActivityContext . TryParse ( traceParent , traceState , out var activityContext ) )
72
+ {
73
+ activity . SetParentId ( activityContext . TraceId , activityContext . SpanId , activityContext . TraceFlags ) ;
74
+ activity . TraceStateString = activityContext . TraceState ;
75
+ }
76
+
77
+ if ( activity . IsAllDataRequested )
78
+ {
79
+ SetActivityTags ( activity , message ) ;
80
+ }
81
+
82
+ activity . Start ( ) ;
83
+ }
84
+
85
+
86
+ return activity ;
87
+ }
88
+
89
+ private static void SetActivityTags < TKey , TValue > ( Activity activity , Message < TKey , TValue > message )
90
+ {
91
+ if ( message . Key != null )
92
+ {
93
+ activity . SetTag ( "messaging.kafka.message_key" , message . Key . ToString ( ) ) ;
94
+ }
95
+ }
96
+
97
+ private static IEnumerable < KeyValuePair < string , object > > ActivityTags ( TopicPartition partition )
98
+ {
99
+ return new [ ]
100
+ {
101
+ new KeyValuePair < string , object > ( "messaging.system" , "kafka" ) ,
102
+ new KeyValuePair < string , object > ( "messaging.destination" , partition . Topic ) ,
103
+ new KeyValuePair < string , object > ( "messaging.destination_kind" , "topic" ) , new KeyValuePair < string , object > (
104
+ "messaging.kafka.partition" ,
105
+ partition . Partition . ToString ( ) )
106
+ } ;
107
+ }
64
108
}
0 commit comments