Exchange/routingKey as independet props in the RabbitMessageSenderContext

This commit is contained in:
Tran Ngoc Nhan
2024-09-16 22:09:40 +07:00
committed by GitHub
parent 5db0ee2508
commit 7d1e696d7f
3 changed files with 58 additions and 5 deletions

View File

@@ -159,6 +159,7 @@ import io.micrometer.observation.ObservationRegistry;
* @author Mohammad Hewedy
* @author Alexey Platonov
* @author Leonardo Ferreira
* @author Ngoc Nhan
*
* @since 1.0
*/
@@ -2486,7 +2487,7 @@ public class RabbitTemplate extends RabbitAccessor // NOSONAR type line count
ObservationRegistry registry = getObservationRegistry();
Observation observation = RabbitTemplateObservation.TEMPLATE_OBSERVATION.observation(this.observationConvention,
DefaultRabbitTemplateObservationConvention.INSTANCE,
() -> new RabbitMessageSenderContext(message, this.beanName, exch + "/" + rKey), registry);
() -> new RabbitMessageSenderContext(message, this.beanName, exch, rKey), registry);
observation.observe(() -> sendToRabbit(channel, exch, rKey, mandatory, message));
}

View File

@@ -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.
@@ -24,6 +24,7 @@ import io.micrometer.observation.transport.SenderContext;
* {@link SenderContext} for {@link Message}s.
*
* @author Gary Russell
* @author Ngoc Nhan
* @since 3.0
*
*/
@@ -33,14 +34,40 @@ public class RabbitMessageSenderContext extends SenderContext<Message> {
private final String destination;
private final String exchange;
private final String routingKey;
@Deprecated(since = "3.2")
public RabbitMessageSenderContext(Message message, String beanName, String destination) {
super((carrier, key, value) -> message.getMessageProperties().setHeader(key, value));
setCarrier(message);
this.beanName = beanName;
this.exchange = null;
this.routingKey = null;
this.destination = destination;
setRemoteServiceName("RabbitMQ");
}
/**
* Create an instance {@code RabbitMessageSenderContext}.
* @param message a message to send
* @param beanName the bean name
* @param exchange the name of the exchange
* @param routingKey the routing key
* @since 3.2
*/
public RabbitMessageSenderContext(Message message, String beanName, String exchange, String routingKey) {
super((carrier, key, value) -> message.getMessageProperties().setHeader(key, value));
setCarrier(message);
this.beanName = beanName;
this.exchange = exchange;
this.routingKey = routingKey;
this.destination = exchange + "/" + routingKey;
setRemoteServiceName("RabbitMQ");
}
public String getBeanName() {
return this.beanName;
}
@@ -53,4 +80,22 @@ public class RabbitMessageSenderContext extends SenderContext<Message> {
return this.destination;
}
/**
* Return the exchange.
* @return the exchange.
* @since 3.2
*/
public String getExchange() {
return this.exchange;
}
/**
* Return the routingKey.
* @return the routingKey.
* @since 3.2
*/
public String getRoutingKey() {
return this.routingKey;
}
}

View File

@@ -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.
@@ -66,6 +66,7 @@ import io.micrometer.tracing.test.simple.SimpleTracer;
/**
* @author Gary Russell
* @author Ngoc Nhan
* @since 3.0
*
*/
@@ -110,7 +111,9 @@ public class ObservationTests {
@Override
public KeyValues getLowCardinalityKeyValues(RabbitMessageSenderContext context) {
return super.getLowCardinalityKeyValues(context).and("foo", "bar");
return super.getLowCardinalityKeyValues(context).and("foo", "bar")
.and("messaging.destination.name", context.getExchange())
.and("messaging.rabbitmq.destination.routing_key", context.getRoutingKey());
}
});
@@ -135,6 +138,8 @@ public class ObservationTests {
span = spans.poll();
assertThat(span.getTags()).containsEntry("spring.rabbit.template.name", "template");
assertThat(span.getTags()).containsEntry("foo", "bar");
assertThat(span.getTags()).containsEntry("messaging.destination.name", "");
assertThat(span.getTags()).containsEntry("messaging.rabbitmq.destination.routing_key", "observation.testQ1");
assertThat(span.getName()).isEqualTo("/observation.testQ1 send");
await().until(() -> spans.peekFirst().getTags().size() == 4);
span = spans.poll();
@@ -142,10 +147,12 @@ public class ObservationTests {
.containsAllEntriesOf(Map.of("spring.rabbit.listener.id", "obs1", "foo", "some foo value", "bar",
"some bar value", "baz", "qux"));
assertThat(span.getName()).isEqualTo("observation.testQ1 receive");
await().until(() -> spans.peekFirst().getTags().size() == 2);
await().until(() -> spans.peekFirst().getTags().size() == 4);
span = spans.poll();
assertThat(span.getTags()).containsEntry("spring.rabbit.template.name", "template");
assertThat(span.getTags()).containsEntry("foo", "bar");
assertThat(span.getTags()).containsEntry("messaging.destination.name", "");
assertThat(span.getTags()).containsEntry("messaging.rabbitmq.destination.routing_key", "observation.testQ2");
assertThat(span.getName()).isEqualTo("/observation.testQ2 send");
await().until(() -> spans.peekFirst().getTags().size() == 3);
span = spans.poll();