32
32
* @author Gary Russell
33
33
* @author Christian Mergenthaler
34
34
* @author Wang Zhiyang
35
+ * @author Christian Fredriksson
35
36
*
36
37
* @since 3.0
37
38
*
@@ -224,33 +225,45 @@ public static class DefaultKafkaListenerObservationConvention implements KafkaLi
224
225
new DefaultKafkaListenerObservationConvention ();
225
226
226
227
@ Override
228
+ @ NonNull
227
229
public KeyValues getLowCardinalityKeyValues (KafkaRecordReceiverContext context ) {
228
-
229
- return KeyValues .of (
230
+ String groupId = context . getGroupId ();
231
+ KeyValues keyValues = KeyValues .of (
230
232
ListenerLowCardinalityTags .LISTENER_ID .withValue (context .getListenerId ()),
231
233
ListenerLowCardinalityTags .MESSAGING_SYSTEM .withValue ("kafka" ),
232
234
ListenerLowCardinalityTags .MESSAGING_OPERATION .withValue ("receive" ),
233
235
ListenerLowCardinalityTags .MESSAGING_SOURCE_NAME .withValue (context .getSource ()),
234
- ListenerLowCardinalityTags .MESSAGING_SOURCE_KIND .withValue ("topic" ),
235
- ListenerLowCardinalityTags .MESSAGING_CONSUMER_GROUP .withValue (context .getGroupId ())
236
+ ListenerLowCardinalityTags .MESSAGING_SOURCE_KIND .withValue ("topic" )
236
237
);
238
+
239
+ if (StringUtils .hasText (groupId )) {
240
+ keyValues = keyValues
241
+ .and (ListenerLowCardinalityTags .MESSAGING_CONSUMER_GROUP .withValue (groupId ));
242
+ }
243
+
244
+ return keyValues ;
237
245
}
238
246
239
247
@ Override
240
248
@ NonNull
241
249
public KeyValues getHighCardinalityKeyValues (KafkaRecordReceiverContext context ) {
242
250
String clientId = context .getClientId ();
251
+ String consumerId = getConsumerId (context .getGroupId (), clientId );
243
252
KeyValues keyValues = KeyValues .of (
244
253
ListenerHighCardinalityTags .MESSAGING_PARTITION .withValue (context .getPartition ()),
245
- ListenerHighCardinalityTags .MESSAGING_OFFSET .withValue (context .getOffset ()),
246
- ListenerHighCardinalityTags .MESSAGING_CONSUMER_ID .withValue (getConsumerId (context , clientId ))
254
+ ListenerHighCardinalityTags .MESSAGING_OFFSET .withValue (context .getOffset ())
247
255
);
248
256
249
257
if (StringUtils .hasText (clientId )) {
250
258
keyValues = keyValues
251
259
.and (ListenerHighCardinalityTags .MESSAGING_CLIENT_ID .withValue (clientId ));
252
260
}
253
261
262
+ if (StringUtils .hasText (consumerId )) {
263
+ keyValues = keyValues
264
+ .and (ListenerHighCardinalityTags .MESSAGING_CONSUMER_ID .withValue (consumerId ));
265
+ }
266
+
254
267
return keyValues ;
255
268
}
256
269
@@ -259,11 +272,14 @@ public String getContextualName(KafkaRecordReceiverContext context) {
259
272
return context .getSource () + " receive" ;
260
273
}
261
274
262
- private static String getConsumerId (KafkaRecordReceiverContext context , @ Nullable String clientId ) {
263
- if (StringUtils .hasText (clientId )) {
264
- return context .getGroupId () + " - " + clientId ;
275
+ private static @ Nullable String getConsumerId (@ Nullable String groupId , @ Nullable String clientId ) {
276
+ if (StringUtils .hasText (groupId )) {
277
+ if (StringUtils .hasText (clientId )) {
278
+ return groupId + " - " + clientId ;
279
+ }
280
+ return groupId ;
265
281
}
266
- return context . getGroupId () ;
282
+ return clientId ;
267
283
}
268
284
269
285
}
0 commit comments