Skip to content

Commit

Permalink
code review and RabbitListenerObservationConvention modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Meunier committed Sep 19, 2024
1 parent 415a763 commit fd416aa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,19 +24,16 @@

/**
* Spring Rabbit Observation for listeners.
*
* @author Gary Russell
* @author Vincent Meunier
* @since 3.0
*
*/
public enum RabbitListenerObservation implements ObservationDocumentation {

/**
* Observation for Rabbit listeners.
*/
LISTENER_OBSERVATION {


@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultRabbitListenerObservationConvention.class;
Expand All @@ -63,14 +60,36 @@ public enum ListenerLowCardinalityTags implements KeyName {
* Listener id.
*/
LISTENER_ID {

@Override
public String asString() {
return "spring.rabbit.listener.id";
}

}
},

/**
* The exchange the listener is plugged to (empty if default exchange)
* @since 3.2
*/
EXCHANGE {
@Override
public String asString() {
return "messaging.destination.name";
}

},

/**
* The routing key the listener is plugged to
* @since 3.2
*/
ROUTING_KEY {
@Override
public String asString() {
return "messaging.rabbitmq.destination.routing_key";
}

}
}

/**
Expand All @@ -86,8 +105,13 @@ public static class DefaultRabbitListenerObservationConvention implements Rabbit

@Override
public KeyValues getLowCardinalityKeyValues(RabbitMessageReceiverContext context) {
return KeyValues.of(RabbitListenerObservation.ListenerLowCardinalityTags.LISTENER_ID.asString(),
context.getListenerId());
return KeyValues.of(
RabbitListenerObservation.ListenerLowCardinalityTags.LISTENER_ID.asString(), context.getListenerId(),
RabbitListenerObservation.ListenerLowCardinalityTags.EXCHANGE.asString(),
context.getCarrier().getMessageProperties().getReceivedExchange(),
RabbitListenerObservation.ListenerLowCardinalityTags.ROUTING_KEY.asString(),
context.getCarrier().getMessageProperties().getReceivedRoutingKey()
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public String asString() {

/**
* The destination exchange (empty if default exchange)
* @since 3.2
*/
EXCHANGE {

Expand All @@ -85,6 +86,7 @@ public String asString() {

/**
* The destination routing key
* @since 3.2
*/
ROUTING_KEY {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.core.tck.MeterRegistryAssert;
import io.micrometer.observation.ObservationRegistry;
Expand All @@ -47,7 +48,6 @@
/**
* @author Artem Bilan
* @author Gary Russell
*
* @since 3.0
*/
@RabbitAvailable(queues = { "int.observation.testQ1", "int.observation.testQ2" })
Expand Down Expand Up @@ -87,29 +87,54 @@ public SampleTestRunnerConsumer yourCode() {
.hasTag("messaging.destination.name", "")
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ2");
SpanAssert.assertThat(consumerSpans.get(0))
.hasTagWithKey("spring.rabbit.listener.id");
.hasTagWithKey("spring.rabbit.listener.id")
.hasTag("messaging.destination.name", "")
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ1");
SpanAssert.assertThat(consumerSpans.get(0))
.hasRemoteServiceNameEqualTo("RabbitMQ");
assertThat(consumerSpans.get(0).getTags().get("spring.rabbit.listener.id")).isIn("obs1", "obs2");
SpanAssert.assertThat(consumerSpans.get(1))
.hasTagWithKey("spring.rabbit.listener.id");
assertThat(consumerSpans.get(1).getTags().get("spring.rabbit.listener.id")).isIn("obs1", "obs2");
SpanAssert.assertThat(consumerSpans.get(1))
.hasTagWithKey("spring.rabbit.listener.id")
.hasTag("messaging.destination.name", "")
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ2");
assertThat(consumerSpans.get(0).getTags().get("spring.rabbit.listener.id"))
.isNotEqualTo(consumerSpans.get(1).getTags().get("spring.rabbit.listener.id"));

MeterRegistryAssert.assertThat(getMeterRegistry())
.hasTimerWithNameAndTags("spring.rabbit.template",
KeyValues.of("spring.rabbit.template.name", "template"))
KeyValues.of(
KeyValue.of("spring.rabbit.template.name", "template"),
KeyValue.of("messaging.destination.name", ""),
KeyValue.of("messaging.rabbitmq.destination.routing_key", "int.observation.testQ1")
)
)
.hasTimerWithNameAndTags("spring.rabbit.template",
KeyValues.of("spring.rabbit.template.name", "template"))
KeyValues.of(
KeyValue.of("spring.rabbit.template.name", "template"),
KeyValue.of("messaging.destination.name", ""),
KeyValue.of("messaging.rabbitmq.destination.routing_key", "int.observation.testQ2")
)
)
.hasTimerWithNameAndTags("spring.rabbit.listener",
KeyValues.of("spring.rabbit.listener.id", "obs1"))
KeyValues.of(
KeyValue.of("spring.rabbit.listener.id", "obs1"),
KeyValue.of("messaging.destination.name", ""),
KeyValue.of("messaging.rabbitmq.destination.routing_key", "int.observation.testQ1")
)
)
.hasTimerWithNameAndTags("spring.rabbit.listener",
KeyValues.of("spring.rabbit.listener.id", "obs2"));
KeyValues.of(
KeyValue.of("spring.rabbit.listener.id", "obs2"),
KeyValue.of("messaging.destination.name", ""),
KeyValue.of("messaging.rabbitmq.destination.routing_key", "int.observation.testQ2")
)
);
};
}


@Configuration
@EnableRabbit
public static class Config {
Expand Down Expand Up @@ -163,5 +188,4 @@ void listen2(Message in) {

}


}

0 comments on commit fd416aa

Please sign in to comment.