From b8ef938b84eec0e94261a712429b66f8acb27e4a Mon Sep 17 00:00:00 2001 From: Jakub Wach Date: Thu, 14 Jan 2021 16:00:50 +0100 Subject: [PATCH] camel instrumentation to set client context property (#2042) --- .../apachecamel/ActiveSpanManager.java | 23 ++++++------ .../apachecamel/CamelEventNotifier.java | 12 +++---- .../apachecamel/CamelRoutePolicy.java | 13 +++---- .../apachecamel/CamelTracer.java | 8 +++++ .../src/test/groovy/test/RestConfig.groovy | 1 + ...woServicesWithDirectClientCamelTest.groovy | 35 +++---------------- .../{logback.xml => logback-test.xml} | 0 7 files changed, 35 insertions(+), 57 deletions(-) rename instrumentation/apache-camel-2.20/javaagent/src/test/resources/{logback.xml => logback-test.xml} (100%) diff --git a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java index ed80d8e321..fe762f1570 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java +++ b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java @@ -54,14 +54,12 @@ class ActiveSpanManager { * @param exchange The exchange * @param span The span */ - public static void activate(Exchange exchange, Span span) { + public static void activate(Exchange exchange, Span span, CamelDirection direction) { SpanWithScope parent = exchange.getProperty(ACTIVE_SPAN_PROPERTY, SpanWithScope.class); - SpanWithScope spanWithScope = SpanWithScope.activate(span, parent); + SpanWithScope spanWithScope = SpanWithScope.activate(span, parent, direction); exchange.setProperty(ACTIVE_SPAN_PROPERTY, spanWithScope); - if (LOG.isTraceEnabled()) { - LOG.trace("Activated a span: " + spanWithScope); - } + LOG.debug("Activated a span: {}", spanWithScope); } /** @@ -78,9 +76,7 @@ class ActiveSpanManager { if (spanWithScope != null) { spanWithScope.deactivate(); exchange.setProperty(ACTIVE_SPAN_PROPERTY, spanWithScope.getParent()); - if (LOG.isTraceEnabled()) { - LOG.trace("Deactivated span: " + spanWithScope); - } + LOG.debug("Deactivated span: {}", spanWithScope); } } @@ -95,8 +91,15 @@ class ActiveSpanManager { this.scope = scope; } - public static SpanWithScope activate(Span span, SpanWithScope parent) { - Scope scope = CamelTracer.TRACER.startScope(span); + public static SpanWithScope activate( + Span span, SpanWithScope parent, CamelDirection direction) { + Scope scope = null; + if (CamelDirection.OUTBOUND.equals(direction)) { + scope = CamelTracer.TRACER.startClientScope(span); + } else { + scope = CamelTracer.TRACER.startScope(span); + } + return new SpanWithScope(parent, span, scope); } diff --git a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java index 668756d501..ffa7bbfeea 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java +++ b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java @@ -62,11 +62,9 @@ final class CamelEventNotifier extends EventNotifierSupport { Span span = CamelTracer.TRACER.startSpan(name, sd.getInitiatorSpanKind()); sd.pre(span, ese.getExchange(), ese.getEndpoint(), CamelDirection.OUTBOUND); CamelPropagationUtil.injectParent(Context.current(), ese.getExchange().getIn().getHeaders()); - ActiveSpanManager.activate(ese.getExchange(), span); + ActiveSpanManager.activate(ese.getExchange(), span, CamelDirection.OUTBOUND); - if (LOG.isTraceEnabled()) { - LOG.trace("[Exchange sending] Initiator span started " + span); - } + LOG.debug("[Exchange sending] Initiator span started: {}", span); } /** Camel finished sending (outbound). Finish span and remove it from CAMEL holder. */ @@ -79,13 +77,11 @@ final class CamelEventNotifier extends EventNotifierSupport { Span span = ActiveSpanManager.getSpan(ese.getExchange()); if (span != null) { - if (LOG.isTraceEnabled()) { - LOG.trace("[Exchange sent] Initiator span finished " + span); - } + LOG.debug("[Exchange sent] Initiator span finished: {}", span); sd.post(span, ese.getExchange(), ese.getEndpoint()); ActiveSpanManager.deactivate(ese.getExchange()); } else { - LOG.warn("Could not find managed span for exchange " + ese.getExchange()); + LOG.warn("Could not find managed span for exchange: {}", ese.getExchange()); } } diff --git a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java index 3870cb2900..a5f8cdddf1 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java +++ b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java @@ -61,10 +61,8 @@ final class CamelRoutePolicy extends RoutePolicySupport { SpanDecorator sd = CamelTracer.TRACER.getSpanDecorator(route.getEndpoint()); Span span = spanOnExchangeBegin(route, exchange, sd); sd.pre(span, exchange, route.getEndpoint(), CamelDirection.INBOUND); - ActiveSpanManager.activate(exchange, span); - if (LOG.isTraceEnabled()) { - LOG.trace("[Route start] Receiver span started " + span); - } + ActiveSpanManager.activate(exchange, span, CamelDirection.INBOUND); + LOG.debug("[Route start] Receiver span started {}", span); } catch (Throwable t) { LOG.warn("Failed to capture tracing data", t); } @@ -76,14 +74,13 @@ final class CamelRoutePolicy extends RoutePolicySupport { try { Span span = ActiveSpanManager.getSpan(exchange); if (span != null) { - if (LOG.isTraceEnabled()) { - LOG.trace("[Route finished] Receiver span finished " + span); - } + + LOG.debug("[Route finished] Receiver span finished {}", span); SpanDecorator sd = CamelTracer.TRACER.getSpanDecorator(route.getEndpoint()); sd.post(span, exchange, route.getEndpoint()); ActiveSpanManager.deactivate(exchange); } else { - LOG.warn("Could not find managed span for exchange=" + exchange); + LOG.warn("Could not find managed span for exchange={}", exchange); } } catch (Throwable t) { LOG.warn("Failed to capture tracing data", t); diff --git a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java index 37dd9b0578..23a5b94234 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java +++ b/instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java @@ -23,7 +23,10 @@ package io.opentelemetry.javaagent.instrumentation.apachecamel; +import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.javaagent.instrumentation.apachecamel.decorators.DecoratorRegistry; import org.apache.camel.Endpoint; @@ -44,6 +47,11 @@ class CamelTracer extends BaseTracer { return tracer.spanBuilder(name); } + public Scope startClientScope(Span span) { + Context current = super.withClientSpan(Context.current(), span); + return current.makeCurrent(); + } + public SpanDecorator getSpanDecorator(Endpoint endpoint) { String component = ""; diff --git a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/RestConfig.groovy b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/RestConfig.groovy index 0a95cf9921..814b735b73 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/RestConfig.groovy +++ b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/RestConfig.groovy @@ -27,6 +27,7 @@ class RestConfig { .bindingMode(RestBindingMode.auto) .host("localhost") .port("{{restServer.port}}") + .producerComponent("http") rest("/api") .get("/{module}/unit/{unitId}") diff --git a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/TwoServicesWithDirectClientCamelTest.groovy b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/TwoServicesWithDirectClientCamelTest.groovy index 34d3632cf9..39b259224d 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/TwoServicesWithDirectClientCamelTest.groovy +++ b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/TwoServicesWithDirectClientCamelTest.groovy @@ -76,7 +76,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner { then: assertTraces(1) { - trace(0, 8) { + trace(0, 6) { it.span(0) { name "input" kind INTERNAL @@ -95,19 +95,6 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner { } } it.span(2) { - name "HTTP POST" - kind CLIENT - attributes { - "$SemanticAttributes.HTTP_METHOD.key" "POST" - "$SemanticAttributes.HTTP_URL.key" "http://localhost:$portOne/serviceOne" - "$SemanticAttributes.HTTP_STATUS_CODE.key" 200 - "$SemanticAttributes.NET_PEER_NAME.key" "localhost" - "$SemanticAttributes.NET_PEER_PORT.key" portOne - "$SemanticAttributes.NET_TRANSPORT.key" "IP.TCP" - "$SemanticAttributes.HTTP_FLAVOR.key" "1.1" - } - } - it.span(3) { name "/serviceOne" kind SERVER attributes { @@ -117,7 +104,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner { "apache-camel.uri" "http://0.0.0.0:$portOne/serviceOne" } } - it.span(4) { + it.span(3) { name "POST" kind CLIENT attributes { @@ -127,21 +114,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner { "apache-camel.uri" "http://0.0.0.0:$portTwo/serviceTwo" } } - it.span(5) { - name "HTTP POST" - kind CLIENT - attributes { - "$SemanticAttributes.HTTP_METHOD.key" "POST" - "$SemanticAttributes.HTTP_URL.key" "http://0.0.0.0:$portTwo/serviceTwo" - "$SemanticAttributes.HTTP_STATUS_CODE.key" 200 - "$SemanticAttributes.NET_PEER_NAME.key" "0.0.0.0" - "$SemanticAttributes.NET_PEER_PORT.key" portTwo - "$SemanticAttributes.NET_TRANSPORT.key" "IP.TCP" - "$SemanticAttributes.HTTP_FLAVOR.key" "1.1" - "$SemanticAttributes.HTTP_USER_AGENT.key" "Jakarta Commons-HttpClient/3.1" - } - } - it.span(6) { + it.span(4) { name "/serviceTwo" kind SERVER attributes { @@ -156,7 +129,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner { } } - it.span(7) { + it.span(5) { name "/serviceTwo" kind INTERNAL attributes { diff --git a/instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback.xml b/instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback-test.xml similarity index 100% rename from instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback.xml rename to instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback-test.xml