Skip to content

Commit

Permalink
Merge branch 'spring-projects:main' into rabbitadmin-behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocnhan-tran1996 authored Sep 16, 2024
2 parents 52980df + 7d1e696 commit d3574f5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
* @author Mohammad Hewedy
* @author Alexey Platonov
* @author Leonardo Ferreira
* @author Ngoc Nhan
*
* @since 1.0
*/
Expand Down Expand Up @@ -2486,7 +2487,7 @@ protected void observeTheSend(Channel channel, Message message, boolean mandator
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));
}
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 @@ -24,6 +24,7 @@
* {@link SenderContext} for {@link Message}s.
*
* @author Gary Russell
* @author Ngoc Nhan
* @since 3.0
*
*/
Expand All @@ -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;
}
Expand All @@ -53,4 +80,22 @@ public String getDestination() {
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;
}

}
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 Down Expand Up @@ -66,6 +66,7 @@

/**
* @author Gary Russell
* @author Ngoc Nhan
* @since 3.0
*
*/
Expand Down Expand Up @@ -110,7 +111,9 @@ void endToEnd(@Autowired Listener listener, @Autowired RabbitTemplate template,

@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());
}

});
Expand All @@ -135,17 +138,21 @@ public KeyValues getLowCardinalityKeyValues(RabbitMessageReceiverContext context
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();
assertThat(span.getTags())
.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();
Expand Down

0 comments on commit d3574f5

Please sign in to comment.