From 48e42be821be27bb9d55da33edbf3e3ce5dd2762 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 28 Sep 2021 09:04:30 -0700 Subject: [PATCH] Support suppress-messaging-receive-span in JMS (#4204) * Support suppress-messaging-receive-span in JMS * Spotless --- .../jms-1.1/javaagent/build.gradle.kts | 32 +++++++++++++++- ...istenerJms2SuppressReceiveSpansTest.groovy | 33 +++++++++++++++++ .../instrumentation/jms/JmsSingletons.java | 2 + ...istenerJms1SuppressReceiveSpansTest.groovy | 37 +++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 instrumentation/jms-1.1/javaagent/src/jms2TestReceiveSpansDisabled/groovy/SpringListenerJms2SuppressReceiveSpansTest.groovy create mode 100644 instrumentation/jms-1.1/javaagent/src/test/groovy/SpringListenerJms1SuppressReceiveSpansTest.groovy diff --git a/instrumentation/jms-1.1/javaagent/build.gradle.kts b/instrumentation/jms-1.1/javaagent/build.gradle.kts index dfe4cee558c9..4ac36d858955 100644 --- a/instrumentation/jms-1.1/javaagent/build.gradle.kts +++ b/instrumentation/jms-1.1/javaagent/build.gradle.kts @@ -18,20 +18,45 @@ muzzle { testSets { create("jms2Test") + create("jms2TestReceiveSpansDisabled") { + extendsFrom("jms2Test") + } } tasks { + val testReceiveSpansDisabled by registering(Test::class) { + filter { + includeTestsMatching("SpringListenerJms1SuppressReceiveSpansTest") + isFailOnNoMatchingTests = false + } + include("**/SpringListenerJms1SuppressReceiveSpansTest.*") + jvmArgs("-Dotel.instrumentation.common.experimental.suppress-messaging-receive-spans=true") + } + val jms2Test by existing(Test::class) { filter { // this is needed because "test.dependsOn jms2Test", and so without this, // running a single test in the default test set will fail - setFailOnNoMatchingTests(false) + isFailOnNoMatchingTests = false + } + } + + val jms2TestReceiveSpansDisabled by existing(Test::class) { + filter { + isFailOnNoMatchingTests = false } + jvmArgs("-Dotel.instrumentation.common.experimental.suppress-messaging-receive-spans=true") } - named("test") { + test { + dependsOn(testReceiveSpansDisabled) dependsOn(jms2Test) + dependsOn(jms2TestReceiveSpansDisabled) usesService(gradle.sharedServices.registrations["testcontainersBuildService"].getService()) + filter { + excludeTestsMatching("SpringListenerJms1SuppressReceiveSpansTest") + isFailOnNoMatchingTests = false + } } } @@ -54,4 +79,7 @@ dependencies { // this doesn't exist in maven central, and doesn't seem to be needed anyways exclude("org.jboss.naming", "jnpserver") } + + // this is just to avoid a bit more copy-pasting + add("jms2TestReceiveSpansDisabledImplementation", sourceSets["jms2Test"].output) } diff --git a/instrumentation/jms-1.1/javaagent/src/jms2TestReceiveSpansDisabled/groovy/SpringListenerJms2SuppressReceiveSpansTest.groovy b/instrumentation/jms-1.1/javaagent/src/jms2TestReceiveSpansDisabled/groovy/SpringListenerJms2SuppressReceiveSpansTest.groovy new file mode 100644 index 000000000000..790193e6540a --- /dev/null +++ b/instrumentation/jms-1.1/javaagent/src/jms2TestReceiveSpansDisabled/groovy/SpringListenerJms2SuppressReceiveSpansTest.groovy @@ -0,0 +1,33 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification +import listener.Config +import org.springframework.context.annotation.AnnotationConfigApplicationContext +import org.springframework.jms.core.JmsTemplate + +import javax.jms.ConnectionFactory + +class SpringListenerJms2SuppressReceiveSpansTest extends AgentInstrumentationSpecification { + def "receiving message in spring listener generates spans"() { + setup: + def context = new AnnotationConfigApplicationContext(Config) + def factory = context.getBean(ConnectionFactory) + def template = new JmsTemplate(factory) + + template.convertAndSend("SpringListenerJms2", "a message") + + expect: + assertTraces(1) { + trace(0, 2) { + Jms2Test.producerSpan(it, 0, "queue", "SpringListenerJms2") + Jms2Test.consumerSpan(it, 1, "queue", "SpringListenerJms2", "", span(0), "process") + } + } + + cleanup: + context.close() + } +} diff --git a/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsSingletons.java b/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsSingletons.java index 8c73d06097fb..cad56de42896 100644 --- a/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsSingletons.java +++ b/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsSingletons.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.jms; import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.api.config.ExperimentalConfig; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; @@ -46,6 +47,7 @@ private static Instrumenter buildConsumerInstrumen .addAttributesExtractor(attributesExtractor) .setTimeExtractors( MessageWithDestination::startTime, (request, response, error) -> request.endTime()) + .setDisabled(ExperimentalConfig.get().suppressMessagingReceiveSpans()) .newInstrumenter(SpanKindExtractor.alwaysConsumer()); } diff --git a/instrumentation/jms-1.1/javaagent/src/test/groovy/SpringListenerJms1SuppressReceiveSpansTest.groovy b/instrumentation/jms-1.1/javaagent/src/test/groovy/SpringListenerJms1SuppressReceiveSpansTest.groovy new file mode 100644 index 000000000000..7597a34738d3 --- /dev/null +++ b/instrumentation/jms-1.1/javaagent/src/test/groovy/SpringListenerJms1SuppressReceiveSpansTest.groovy @@ -0,0 +1,37 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification +import listener.Config +import org.springframework.context.annotation.AnnotationConfigApplicationContext +import org.springframework.jms.core.JmsTemplate + +import javax.jms.ConnectionFactory + +import static Jms1Test.consumerSpan +import static Jms1Test.producerSpan + +class SpringListenerJms1SuppressReceiveSpansTest extends AgentInstrumentationSpecification { + + def "receiving message in spring listener generates spans"() { + setup: + def context = new AnnotationConfigApplicationContext(Config) + def factory = context.getBean(ConnectionFactory) + def template = new JmsTemplate(factory) + + template.convertAndSend("SpringListenerJms1", "a message") + + expect: + assertTraces(1) { + trace(0, 2) { + producerSpan(it, 0, "queue", "SpringListenerJms1") + consumerSpan(it, 1, "queue", "SpringListenerJms1", "", span(0), "process") + } + } + + cleanup: + context.stop() + } +}