11
11
import io .opentelemetry .api .common .AttributesBuilder ;
12
12
import io .opentelemetry .context .Context ;
13
13
import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
14
+ import io .opentelemetry .instrumentation .api .internal .SemconvStability ;
14
15
import io .opentelemetry .instrumentation .api .internal .SpanKey ;
15
16
import io .opentelemetry .instrumentation .api .internal .SpanKeyProvider ;
16
17
import java .util .List ;
27
28
public final class MessagingAttributesExtractor <REQUEST , RESPONSE >
28
29
implements AttributesExtractor <REQUEST , RESPONSE >, SpanKeyProvider {
29
30
30
- // copied from MessagingIncubatingAttributes
31
+ // copied from io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes (stable
32
+ // attributes)
33
+ private static final AttributeKey <String > MESSAGING_OPERATION_NAME =
34
+ AttributeKey .stringKey ("messaging.operation.name" );
35
+ private static final AttributeKey <String > MESSAGING_SYSTEM =
36
+ AttributeKey .stringKey ("messaging.system" );
31
37
private static final AttributeKey <Long > MESSAGING_BATCH_MESSAGE_COUNT =
32
38
AttributeKey .longKey ("messaging.batch.message_count" );
33
- private static final AttributeKey <String > MESSAGING_CLIENT_ID =
34
- AttributeKey .stringKey ("messaging.client_id" );
39
+ // Messaging specific
40
+ private static final AttributeKey <String > MESSAGING_CONSUMER_GROUP_NAME =
41
+ AttributeKey .stringKey ("messaging.consumer.group.name" );
35
42
private static final AttributeKey <Boolean > MESSAGING_DESTINATION_ANONYMOUS =
36
43
AttributeKey .booleanKey ("messaging.destination.anonymous" );
37
44
private static final AttributeKey <String > MESSAGING_DESTINATION_NAME =
38
45
AttributeKey .stringKey ("messaging.destination.name" );
39
- private static final AttributeKey <String > MESSAGING_DESTINATION_PARTITION_ID =
40
- AttributeKey .stringKey ("messaging.destination.partition.id" );
46
+ // Messaging specific
47
+ private static final AttributeKey <String > MESSAGING_DESTINATION_SUBSCRIPTION_NAME =
48
+ AttributeKey .stringKey ("messaging.destination.subscription.name" );
41
49
private static final AttributeKey <String > MESSAGING_DESTINATION_TEMPLATE =
42
50
AttributeKey .stringKey ("messaging.destination.template" );
43
51
private static final AttributeKey <Boolean > MESSAGING_DESTINATION_TEMPORARY =
44
52
AttributeKey .booleanKey ("messaging.destination.temporary" );
45
- private static final AttributeKey <Long > MESSAGING_MESSAGE_BODY_SIZE =
46
- AttributeKey .longKey ("messaging.message.body.size" );
53
+ private static final AttributeKey <String > MESSAGING_OPERATION_TYPE =
54
+ AttributeKey .stringKey ("messaging.operation.type" );
55
+ private static final AttributeKey <String > MESSAGING_CLIENT_ID_STABLE =
56
+ AttributeKey .stringKey ("messaging.client.id" );
57
+ private static final AttributeKey <String > MESSAGING_DESTINATION_PARTITION_ID =
58
+ AttributeKey .stringKey ("messaging.destination.partition.id" );
47
59
private static final AttributeKey <String > MESSAGING_MESSAGE_CONVERSATION_ID =
48
60
AttributeKey .stringKey ("messaging.message.conversation_id" );
49
- private static final AttributeKey <Long > MESSAGING_MESSAGE_ENVELOPE_SIZE =
50
- AttributeKey .longKey ("messaging.message.envelope.size" );
51
61
private static final AttributeKey <String > MESSAGING_MESSAGE_ID =
52
62
AttributeKey .stringKey ("messaging.message.id" );
63
+ private static final AttributeKey <Long > MESSAGING_MESSAGE_BODY_SIZE =
64
+ AttributeKey .longKey ("messaging.message.body.size" );
65
+ private static final AttributeKey <Long > MESSAGING_MESSAGE_ENVELOPE_SIZE =
66
+ AttributeKey .longKey ("messaging.message.envelope.size" );
67
+
68
+ // copied from MessagingIncubatingAttributes (old attributes)
69
+ @ Deprecated
70
+ private static final AttributeKey <String > MESSAGING_CLIENT_ID =
71
+ AttributeKey .stringKey ("messaging.client_id" );
72
+
73
+ @ Deprecated
53
74
private static final AttributeKey <String > MESSAGING_OPERATION =
54
75
AttributeKey .stringKey ("messaging.operation" );
55
- private static final AttributeKey <String > MESSAGING_SYSTEM =
56
- AttributeKey .stringKey ("messaging.system" );
57
76
58
77
static final String TEMP_DESTINATION_NAME = "(temporary)" ;
78
+ static final String ANONYMOUS_DESTINATION_NAME = "(anonymous)" ;
59
79
60
80
/**
61
81
* Creates the messaging attributes extractor for the given {@link MessageOperation operation}
@@ -89,31 +109,56 @@ public static <REQUEST, RESPONSE> MessagingAttributesExtractorBuilder<REQUEST, R
89
109
}
90
110
91
111
@ Override
112
+ @ SuppressWarnings ("deprecation" ) // using deprecated semconv
92
113
public void onStart (AttributesBuilder attributes , Context parentContext , REQUEST request ) {
93
114
internalSet (attributes , MESSAGING_SYSTEM , getter .getSystem (request ));
115
+
116
+ // Old messaging attributes
117
+ if (SemconvStability .emitOldMessagingSemconv ()) {
118
+ internalSet (attributes , MESSAGING_CLIENT_ID , getter .getClientId (request ));
119
+ if (operation != null ) { // in old implementation operation could be null
120
+ internalSet (attributes , MESSAGING_OPERATION , operation .operationName ());
121
+ }
122
+ }
123
+
124
+ // New, stable attributes
125
+ if (SemconvStability .emitStableMessagingSemconv ()) {
126
+ internalSet (attributes , MESSAGING_CLIENT_ID_STABLE , getter .getClientId (request ));
127
+ internalSet (attributes , MESSAGING_OPERATION_TYPE , operation .operationType ());
128
+ internalSet (attributes , MESSAGING_OPERATION_NAME , getter .getOperationName (request ));
129
+ internalSet (attributes , MESSAGING_CONSUMER_GROUP_NAME , getter .getConsumerGroupName (request ));
130
+ internalSet (
131
+ attributes ,
132
+ MESSAGING_DESTINATION_SUBSCRIPTION_NAME ,
133
+ getter .getDestinationSubscriptionName (request ));
134
+ }
135
+
136
+ // Unchanged attributes from 1.25.0 to stable
94
137
boolean isTemporaryDestination = getter .isTemporaryDestination (request );
138
+ boolean isAnonymousDestination = getter .isAnonymousDestination (request );
139
+
140
+ String destination =
141
+ isTemporaryDestination
142
+ ? TEMP_DESTINATION_NAME
143
+ : (isAnonymousDestination
144
+ ? ANONYMOUS_DESTINATION_NAME
145
+ : getter .getDestination (request ));
146
+ internalSet (attributes , MESSAGING_DESTINATION_NAME , destination );
147
+
95
148
if (isTemporaryDestination ) {
96
149
internalSet (attributes , MESSAGING_DESTINATION_TEMPORARY , true );
97
- internalSet (attributes , MESSAGING_DESTINATION_NAME , TEMP_DESTINATION_NAME );
98
150
} else {
99
- internalSet (attributes , MESSAGING_DESTINATION_NAME , getter .getDestination (request ));
100
151
internalSet (
101
152
attributes , MESSAGING_DESTINATION_TEMPLATE , getter .getDestinationTemplate (request ));
102
153
}
103
- internalSet (
104
- attributes , MESSAGING_DESTINATION_PARTITION_ID , getter .getDestinationPartitionId (request ));
105
- boolean isAnonymousDestination = getter .isAnonymousDestination (request );
106
154
if (isAnonymousDestination ) {
107
155
internalSet (attributes , MESSAGING_DESTINATION_ANONYMOUS , true );
108
156
}
109
- internalSet (attributes , MESSAGING_MESSAGE_CONVERSATION_ID , getter .getConversationId (request ));
110
- internalSet (attributes , MESSAGING_MESSAGE_BODY_SIZE , getter .getMessageBodySize (request ));
157
+
111
158
internalSet (
112
- attributes , MESSAGING_MESSAGE_ENVELOPE_SIZE , getter .getMessageEnvelopeSize (request ));
113
- internalSet (attributes , MESSAGING_CLIENT_ID , getter .getClientId (request ));
114
- if (operation != null ) {
115
- internalSet (attributes , MESSAGING_OPERATION , operation .operationName ());
116
- }
159
+ attributes , MESSAGING_DESTINATION_PARTITION_ID , getter .getDestinationPartitionId (request ));
160
+
161
+ internalSet (attributes , MESSAGING_MESSAGE_CONVERSATION_ID , getter .getConversationId (request ));
117
162
}
118
163
119
164
@ Override
@@ -127,6 +172,10 @@ public void onEnd(
127
172
internalSet (
128
173
attributes , MESSAGING_BATCH_MESSAGE_COUNT , getter .getBatchMessageCount (request , response ));
129
174
175
+ internalSet (attributes , MESSAGING_MESSAGE_BODY_SIZE , getter .getMessageBodySize (request ));
176
+ internalSet (
177
+ attributes , MESSAGING_MESSAGE_ENVELOPE_SIZE , getter .getMessageEnvelopeSize (request ));
178
+
130
179
for (String name : capturedHeaders ) {
131
180
List <String > values = getter .getMessageHeader (request , name );
132
181
if (!values .isEmpty ()) {
@@ -146,12 +195,18 @@ public SpanKey internalGetSpanKey() {
146
195
}
147
196
148
197
switch (operation ) {
198
+ case CREATE :
199
+ return SpanKey .PRODUCER ;
200
+ case SEND :
201
+ return SpanKey .PRODUCER ;
149
202
case PUBLISH :
150
203
return SpanKey .PRODUCER ;
151
204
case RECEIVE :
152
205
return SpanKey .CONSUMER_RECEIVE ;
153
206
case PROCESS :
154
207
return SpanKey .CONSUMER_PROCESS ;
208
+ case SETTLE :
209
+ return SpanKey .CONSUMER_SETTLE ;
155
210
}
156
211
throw new IllegalStateException ("Can't possibly happen" );
157
212
}
0 commit comments