Skip to content
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

Add exchange/routingKey to RabbitMessageSenderContext #2816

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -159,6 +159,7 @@
* @author Mohammad Hewedy
* @author Alexey Platonov
* @author Leonardo Ferreira
* @author Ngoc Nhan
*
* @since 1.0
*/
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 @@ -37,6 +38,27 @@ public class RabbitMessageSenderContext extends SenderContext<Message> {

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}.
*
ngocnhan-tran1996 marked this conversation as resolved.
Show resolved Hide resolved
* @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) {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
ngocnhan-tran1996 marked this conversation as resolved.
Show resolved Hide resolved
super((carrier, key, value) -> message.getMessageProperties().setHeader(key, value));
setCarrier(message);
Expand All @@ -62,6 +84,7 @@ public String getDestination() {
/**
* Return the exchange.
* @return the exchange.
ngocnhan-tran1996 marked this conversation as resolved.
Show resolved Hide resolved
* @since 3.2
*/
public String getExchange() {
return this.exchange;
Expand All @@ -70,6 +93,7 @@ public String getExchange() {
/**
* Return the routingKey.
* @return the routingKey.
* @since 3.2
*/
public String getRoutingKey() {
return this.routingKey;
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 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this will go to TemplateLowCardinalityTags in the next contribution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now it's more easier to determine exchange or routing key for doing traces/metrics without splitting / in RabbitMessageSenderContext.

}

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