Skip to content

Commit 7e4264b

Browse files
author
Vincent
committed
RabbitTemplate add opentelemetry tags
1 parent 52ff8d1 commit 7e4264b

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/micrometer/RabbitTemplateObservation.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
* Spring RabbitMQ Observation for {@link org.springframework.amqp.rabbit.core.RabbitTemplate}.
2727
*
2828
* @author Gary Russell
29+
* @author Vincent Meunier
2930
* @since 3.0
3031
*
3132
*/
@@ -68,8 +69,33 @@ public String asString() {
6869
return "spring.rabbit.template.name";
6970
}
7071

72+
},
73+
74+
/**
75+
* The destination exchange (empty if default exchange)
76+
*/
77+
EXCHANGE {
78+
79+
@Override
80+
public String asString() {
81+
return "messaging.destination.name";
82+
}
83+
84+
},
85+
86+
/**
87+
* The destination routing key
88+
*/
89+
ROUTING_KEY {
90+
91+
@Override
92+
public String asString() {
93+
return "messaging.rabbitmq.destination.routing_key";
94+
}
95+
7196
}
7297

98+
7399
}
74100

75101
/**
@@ -85,8 +111,11 @@ public static class DefaultRabbitTemplateObservationConvention implements Rabbit
85111

86112
@Override
87113
public KeyValues getLowCardinalityKeyValues(RabbitMessageSenderContext context) {
88-
return KeyValues.of(RabbitTemplateObservation.TemplateLowCardinalityTags.BEAN_NAME.asString(),
89-
context.getBeanName());
114+
return KeyValues.of(
115+
TemplateLowCardinalityTags.BEAN_NAME.asString(), context.getBeanName(),
116+
TemplateLowCardinalityTags.EXCHANGE.asString(), context.getExchange(),
117+
TemplateLowCardinalityTags.ROUTING_KEY.asString(), context.getRoutingKey()
118+
);
90119
}
91120

92121
@Override

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/micrometer/ObservationIntegrationTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,16 +72,20 @@ public SampleTestRunnerConsumer yourCode() {
7272
.hasSize(4);
7373
List<FinishedSpan> producerSpans = finishedSpans.stream()
7474
.filter(span -> span.getKind().equals(Kind.PRODUCER))
75-
.collect(Collectors.toList());
75+
.toList();
7676
List<FinishedSpan> consumerSpans = finishedSpans.stream()
7777
.filter(span -> span.getKind().equals(Kind.CONSUMER))
78-
.collect(Collectors.toList());
78+
.toList();
7979
SpanAssert.assertThat(producerSpans.get(0))
80-
.hasTag("spring.rabbit.template.name", "template");
80+
.hasTag("spring.rabbit.template.name", "template")
81+
.hasTag("messaging.destination.name", "")
82+
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ1");
8183
SpanAssert.assertThat(producerSpans.get(0))
8284
.hasRemoteServiceNameEqualTo("RabbitMQ");
8385
SpanAssert.assertThat(producerSpans.get(1))
84-
.hasTag("spring.rabbit.template.name", "template");
86+
.hasTag("spring.rabbit.template.name", "template")
87+
.hasTag("messaging.destination.name", "")
88+
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ2");
8589
SpanAssert.assertThat(consumerSpans.get(0))
8690
.hasTagWithKey("spring.rabbit.listener.id");
8791
SpanAssert.assertThat(consumerSpans.get(0))

0 commit comments

Comments
 (0)