Skip to content

Commit af7a26b

Browse files
committed
* Add custom observation convention support for the AbstractMessageHandler
* Use more meaningful prefix for Spring Integration tags
1 parent 93a9a87 commit af7a26b

File tree

5 files changed

+37
-62
lines changed

5 files changed

+37
-62
lines changed

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandler.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import org.springframework.integration.history.MessageHistory;
2222
import org.springframework.integration.support.management.metrics.MetricsCaptor;
2323
import org.springframework.integration.support.management.metrics.SampleFacade;
24-
import org.springframework.integration.support.management.observation.IntegrationObservations;
24+
import org.springframework.integration.support.management.observation.DefaultMessageReceiverObservationConvention;
25+
import org.springframework.integration.support.management.observation.IntegrationObservation;
26+
import org.springframework.integration.support.management.observation.MessageReceiverContext;
27+
import org.springframework.integration.support.management.observation.MessageReceiverObservationConvention;
2528
import org.springframework.integration.support.utils.IntegrationUtils;
29+
import org.springframework.lang.Nullable;
2630
import org.springframework.messaging.Message;
2731
import org.springframework.messaging.MessageHandler;
2832
import org.springframework.util.Assert;
@@ -39,6 +43,22 @@
3943
public abstract class AbstractMessageHandler extends MessageHandlerSupport
4044
implements MessageHandler, CoreSubscriber<Message<?>> {
4145

46+
private static final DefaultMessageReceiverObservationConvention DEFAULT_MESSAGE_RECEIVER_OBSERVATION_CONVENTION =
47+
new DefaultMessageReceiverObservationConvention();
48+
49+
@Nullable
50+
private MessageReceiverObservationConvention observationConvention;
51+
52+
/**
53+
* Set a custom {@link MessageReceiverObservationConvention} for {@link IntegrationObservation#HANDLER}.
54+
* Ignored if an {@link ObservationRegistry} is not configured for this component.
55+
* @param observationConvention the {@link MessageReceiverObservationConvention} to use.
56+
* @since 6.0
57+
*/
58+
public void setObservationConvention(@Nullable MessageReceiverObservationConvention observationConvention) {
59+
this.observationConvention = observationConvention;
60+
}
61+
4262
@Override // NOSONAR
4363
public void handleMessage(Message<?> message) {
4464
Assert.notNull(message, "Message must not be null");
@@ -61,7 +81,10 @@ public void handleMessage(Message<?> message) {
6181
}
6282

6383
private void handleWithObservation(Message<?> message, ObservationRegistry observationRegistry) {
64-
IntegrationObservations.handlerObservation(observationRegistry, message, getComponentName())
84+
IntegrationObservation.HANDLER.observation(this.observationConvention,
85+
DEFAULT_MESSAGE_RECEIVER_OBSERVATION_CONVENTION,
86+
new MessageReceiverContext(message, getComponentName()),
87+
observationRegistry)
6588
.observe(() -> doHandleMessage(message));
6689
}
6790

spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/DefaultMessageReceiverObservationConvention.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @since 6.0
2828
*/
29-
class DefaultMessageReceiverObservationConvention implements MessageReceiverObservationConvention {
29+
public class DefaultMessageReceiverObservationConvention implements MessageReceiverObservationConvention {
3030

3131
@Override
3232
public KeyValues getLowCardinalityKeyValues(MessageReceiverContext context) {

spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/IntegrationObservation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public String getName() {
3737
return "spring.integration.handler";
3838
}
3939

40+
@Override
41+
public String getPrefix() {
42+
return "spring.integration.";
43+
}
44+
4045
@Override
4146
public Class<DefaultMessageReceiverObservationConvention> getDefaultConvention() {
4247
return DefaultMessageReceiverObservationConvention.class;
@@ -60,7 +65,7 @@ enum HandlerTags implements KeyName {
6065
COMPONENT_NAME {
6166
@Override
6267
public String asString() {
63-
return "name";
68+
return "spring.integration.name";
6469
}
6570

6671
},
@@ -71,7 +76,7 @@ public String asString() {
7176
COMPONENT_TYPE {
7277
@Override
7378
public String asString() {
74-
return "type";
79+
return "spring.integration.type";
7580
}
7681

7782
}

spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/IntegrationObservations.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/ObservationPropagationChannelInterceptorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ void observationContextPropagatedOverExecutorChannel() {
242242
.assertThatASpanWithNameEqualTo("testBridge receive")
243243
.hasTag("foo", "some foo value")
244244
.hasTag("bar", "some bar value")
245-
.hasTag("type", "handler")
246-
.hasTag("name", "testBridge")
245+
.hasTag("spring.integration.type", "handler")
246+
.hasTag("spring.integration.name", "testBridge")
247247
.hasKindEqualTo(Span.Kind.CONSUMER));
248248

249249
assertThat(this.meterRegistry.get("spring.integration.handler")
250-
.tag("name", "testBridge")
251-
.tag("type", "handler")
250+
.tag("spring.integration.name", "testBridge")
251+
.tag("spring.integration.type", "handler")
252252
.tag("error", "none")
253253
.timer().count()).isEqualTo(1);
254254
}

0 commit comments

Comments
 (0)