Skip to content

RabbitTemplate and RabbitListener add opentelemetry tags #2833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,8 +26,8 @@
* Spring Rabbit Observation for listeners.
*
* @author Gary Russell
* @author Vincent Meunier
* @since 3.0
*
*/
public enum RabbitListenerObservation implements ObservationDocumentation {

Expand All @@ -36,7 +36,6 @@ public enum RabbitListenerObservation implements ObservationDocumentation {
*/
LISTENER_OBSERVATION {


@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultRabbitListenerObservationConvention.class;
Expand Down Expand Up @@ -69,6 +68,34 @@ public String asString() {
return "spring.rabbit.listener.id";
}

},

/**
* The queue the listener is plugged to.
*
* @since 3.2
*/
DESTINATION_NAME {

@Override
public String asString() {
return "messaging.destination.name";
}

},

/**
* The delivery tag.
*
* @since 3.2
*/
DELIVERY_TAG {

@Override
public String asString() {
return "messaging.rabbitmq.message.delivery_tag";
}

}

}
Expand All @@ -86,8 +113,14 @@ public static class DefaultRabbitListenerObservationConvention implements Rabbit

@Override
public KeyValues getLowCardinalityKeyValues(RabbitMessageReceiverContext context) {
return KeyValues.of(RabbitListenerObservation.ListenerLowCardinalityTags.LISTENER_ID.asString(),
context.getListenerId());
final var messageProperties = context.getCarrier().getMessageProperties();
return KeyValues.of(
RabbitListenerObservation.ListenerLowCardinalityTags.LISTENER_ID.asString(), context.getListenerId(),
RabbitListenerObservation.ListenerLowCardinalityTags.DESTINATION_NAME.asString(),
messageProperties.getConsumerQueue(),
RabbitListenerObservation.ListenerLowCardinalityTags.DELIVERY_TAG.asString(),
String.valueOf(messageProperties.getDeliveryTag())
);
}

@Override
Expand Down
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 @@ -26,6 +26,7 @@
* Spring RabbitMQ Observation for {@link org.springframework.amqp.rabbit.core.RabbitTemplate}.
*
* @author Gary Russell
* @author Vincent Meunier
* @since 3.0
*
*/
Expand Down Expand Up @@ -68,8 +69,35 @@ public String asString() {
return "spring.rabbit.template.name";
}

},

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

@Override
public String asString() {
return "messaging.destination.name";
}

},

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

@Override
public String asString() {
return "messaging.rabbitmq.destination.routing_key";
}

}


}

/**
Expand All @@ -85,8 +113,11 @@ public static class DefaultRabbitTemplateObservationConvention implements Rabbit

@Override
public KeyValues getLowCardinalityKeyValues(RabbitMessageSenderContext context) {
return KeyValues.of(RabbitTemplateObservation.TemplateLowCardinalityTags.BEAN_NAME.asString(),
context.getBeanName());
return KeyValues.of(
TemplateLowCardinalityTags.BEAN_NAME.asString(), context.getBeanName(),
TemplateLowCardinalityTags.EXCHANGE.asString(), context.getExchange(),
TemplateLowCardinalityTags.ROUTING_KEY.asString(), context.getRoutingKey()
);
}

@Override
Expand Down
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 @@ -21,7 +21,6 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
Expand All @@ -35,6 +34,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 +47,6 @@
/**
* @author Artem Bilan
* @author Gary Russell
*
* @since 3.0
*/
@RabbitAvailable(queues = { "int.observation.testQ1", "int.observation.testQ2" })
Expand All @@ -72,40 +71,69 @@ public SampleTestRunnerConsumer yourCode() {
.hasSize(4);
List<FinishedSpan> producerSpans = finishedSpans.stream()
.filter(span -> span.getKind().equals(Kind.PRODUCER))
.collect(Collectors.toList());
.toList();
List<FinishedSpan> consumerSpans = finishedSpans.stream()
.filter(span -> span.getKind().equals(Kind.CONSUMER))
.collect(Collectors.toList());
.toList();
SpanAssert.assertThat(producerSpans.get(0))
.hasTag("spring.rabbit.template.name", "template");
.hasTag("spring.rabbit.template.name", "template")
.hasTag("messaging.destination.name", "")
.hasTag("messaging.rabbitmq.destination.routing_key", "int.observation.testQ1");
SpanAssert.assertThat(producerSpans.get(0))
.hasRemoteServiceNameEqualTo("RabbitMQ");
SpanAssert.assertThat(producerSpans.get(1))
.hasTag("spring.rabbit.template.name", "template");
.hasTag("spring.rabbit.template.name", "template")
.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", "int.observation.testQ1")
.hasTag("messaging.rabbitmq.message.delivery_tag", "1");
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", "int.observation.testQ2")
.hasTag("messaging.rabbitmq.message.delivery_tag", "1");
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", "int.observation.testQ1"),
KeyValue.of("messaging.rabbitmq.message.delivery_tag", "1")
)
)
.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", "int.observation.testQ2"),
KeyValue.of("messaging.rabbitmq.message.delivery_tag", "1")
)
);
};
}


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

}


}