From 7d1e696d7f80d934999a11136e7812acfecee9db Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Mon, 16 Sep 2024 22:09:40 +0700 Subject: [PATCH] Exchange/routingKey as independet props in the RabbitMessageSenderContext --- .../amqp/rabbit/core/RabbitTemplate.java | 3 +- .../RabbitMessageSenderContext.java | 47 ++++++++++++++++++- .../support/micrometer/ObservationTests.java | 13 +++-- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java index dc33e807d8..5d2894c96c 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java @@ -159,6 +159,7 @@ * @author Mohammad Hewedy * @author Alexey Platonov * @author Leonardo Ferreira + * @author Ngoc Nhan * * @since 1.0 */ @@ -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)); } diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/micrometer/RabbitMessageSenderContext.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/micrometer/RabbitMessageSenderContext.java index e327f6ebc6..b7994ad645 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/micrometer/RabbitMessageSenderContext.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/micrometer/RabbitMessageSenderContext.java @@ -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 @@ * {@link SenderContext} for {@link Message}s. * * @author Gary Russell + * @author Ngoc Nhan * @since 3.0 * */ @@ -33,14 +34,40 @@ public class RabbitMessageSenderContext extends SenderContext { 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 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; + } + } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/micrometer/ObservationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/micrometer/ObservationTests.java index e1787df31f..4c0b6fa056 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/micrometer/ObservationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/micrometer/ObservationTests.java @@ -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 @@ /** * @author Gary Russell + * @author Ngoc Nhan * @since 3.0 * */ @@ -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()); } }); @@ -135,6 +138,8 @@ 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(); @@ -142,10 +147,12 @@ public KeyValues getLowCardinalityKeyValues(RabbitMessageReceiverContext context .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();