camel instrumentation to set client context property (#2042)

This commit is contained in:
Jakub Wach 2021-01-14 16:00:50 +01:00 committed by GitHub
parent 1070996653
commit b8ef938b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 57 deletions

View File

@ -54,14 +54,12 @@ class ActiveSpanManager {
* @param exchange The exchange * @param exchange The exchange
* @param span The span * @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 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); exchange.setProperty(ACTIVE_SPAN_PROPERTY, spanWithScope);
if (LOG.isTraceEnabled()) { LOG.debug("Activated a span: {}", spanWithScope);
LOG.trace("Activated a span: " + spanWithScope);
}
} }
/** /**
@ -78,9 +76,7 @@ class ActiveSpanManager {
if (spanWithScope != null) { if (spanWithScope != null) {
spanWithScope.deactivate(); spanWithScope.deactivate();
exchange.setProperty(ACTIVE_SPAN_PROPERTY, spanWithScope.getParent()); exchange.setProperty(ACTIVE_SPAN_PROPERTY, spanWithScope.getParent());
if (LOG.isTraceEnabled()) { LOG.debug("Deactivated span: {}", spanWithScope);
LOG.trace("Deactivated span: " + spanWithScope);
}
} }
} }
@ -95,8 +91,15 @@ class ActiveSpanManager {
this.scope = scope; this.scope = scope;
} }
public static SpanWithScope activate(Span span, SpanWithScope parent) { public static SpanWithScope activate(
Scope scope = CamelTracer.TRACER.startScope(span); 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); return new SpanWithScope(parent, span, scope);
} }

View File

@ -62,11 +62,9 @@ final class CamelEventNotifier extends EventNotifierSupport {
Span span = CamelTracer.TRACER.startSpan(name, sd.getInitiatorSpanKind()); Span span = CamelTracer.TRACER.startSpan(name, sd.getInitiatorSpanKind());
sd.pre(span, ese.getExchange(), ese.getEndpoint(), CamelDirection.OUTBOUND); sd.pre(span, ese.getExchange(), ese.getEndpoint(), CamelDirection.OUTBOUND);
CamelPropagationUtil.injectParent(Context.current(), ese.getExchange().getIn().getHeaders()); CamelPropagationUtil.injectParent(Context.current(), ese.getExchange().getIn().getHeaders());
ActiveSpanManager.activate(ese.getExchange(), span); ActiveSpanManager.activate(ese.getExchange(), span, CamelDirection.OUTBOUND);
if (LOG.isTraceEnabled()) { LOG.debug("[Exchange sending] Initiator span started: {}", span);
LOG.trace("[Exchange sending] Initiator span started " + span);
}
} }
/** Camel finished sending (outbound). Finish span and remove it from CAMEL holder. */ /** 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()); Span span = ActiveSpanManager.getSpan(ese.getExchange());
if (span != null) { if (span != null) {
if (LOG.isTraceEnabled()) { LOG.debug("[Exchange sent] Initiator span finished: {}", span);
LOG.trace("[Exchange sent] Initiator span finished " + span);
}
sd.post(span, ese.getExchange(), ese.getEndpoint()); sd.post(span, ese.getExchange(), ese.getEndpoint());
ActiveSpanManager.deactivate(ese.getExchange()); ActiveSpanManager.deactivate(ese.getExchange());
} else { } else {
LOG.warn("Could not find managed span for exchange " + ese.getExchange()); LOG.warn("Could not find managed span for exchange: {}", ese.getExchange());
} }
} }

View File

@ -61,10 +61,8 @@ final class CamelRoutePolicy extends RoutePolicySupport {
SpanDecorator sd = CamelTracer.TRACER.getSpanDecorator(route.getEndpoint()); SpanDecorator sd = CamelTracer.TRACER.getSpanDecorator(route.getEndpoint());
Span span = spanOnExchangeBegin(route, exchange, sd); Span span = spanOnExchangeBegin(route, exchange, sd);
sd.pre(span, exchange, route.getEndpoint(), CamelDirection.INBOUND); sd.pre(span, exchange, route.getEndpoint(), CamelDirection.INBOUND);
ActiveSpanManager.activate(exchange, span); ActiveSpanManager.activate(exchange, span, CamelDirection.INBOUND);
if (LOG.isTraceEnabled()) { LOG.debug("[Route start] Receiver span started {}", span);
LOG.trace("[Route start] Receiver span started " + span);
}
} catch (Throwable t) { } catch (Throwable t) {
LOG.warn("Failed to capture tracing data", t); LOG.warn("Failed to capture tracing data", t);
} }
@ -76,14 +74,13 @@ final class CamelRoutePolicy extends RoutePolicySupport {
try { try {
Span span = ActiveSpanManager.getSpan(exchange); Span span = ActiveSpanManager.getSpan(exchange);
if (span != null) { 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()); SpanDecorator sd = CamelTracer.TRACER.getSpanDecorator(route.getEndpoint());
sd.post(span, exchange, route.getEndpoint()); sd.post(span, exchange, route.getEndpoint());
ActiveSpanManager.deactivate(exchange); ActiveSpanManager.deactivate(exchange);
} else { } else {
LOG.warn("Could not find managed span for exchange=" + exchange); LOG.warn("Could not find managed span for exchange={}", exchange);
} }
} catch (Throwable t) { } catch (Throwable t) {
LOG.warn("Failed to capture tracing data", t); LOG.warn("Failed to capture tracing data", t);

View File

@ -23,7 +23,10 @@
package io.opentelemetry.javaagent.instrumentation.apachecamel; package io.opentelemetry.javaagent.instrumentation.apachecamel;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder; 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.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.javaagent.instrumentation.apachecamel.decorators.DecoratorRegistry; import io.opentelemetry.javaagent.instrumentation.apachecamel.decorators.DecoratorRegistry;
import org.apache.camel.Endpoint; import org.apache.camel.Endpoint;
@ -44,6 +47,11 @@ class CamelTracer extends BaseTracer {
return tracer.spanBuilder(name); return tracer.spanBuilder(name);
} }
public Scope startClientScope(Span span) {
Context current = super.withClientSpan(Context.current(), span);
return current.makeCurrent();
}
public SpanDecorator getSpanDecorator(Endpoint endpoint) { public SpanDecorator getSpanDecorator(Endpoint endpoint) {
String component = ""; String component = "";

View File

@ -27,6 +27,7 @@ class RestConfig {
.bindingMode(RestBindingMode.auto) .bindingMode(RestBindingMode.auto)
.host("localhost") .host("localhost")
.port("{{restServer.port}}") .port("{{restServer.port}}")
.producerComponent("http")
rest("/api") rest("/api")
.get("/{module}/unit/{unitId}") .get("/{module}/unit/{unitId}")

View File

@ -76,7 +76,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 8) { trace(0, 6) {
it.span(0) { it.span(0) {
name "input" name "input"
kind INTERNAL kind INTERNAL
@ -95,19 +95,6 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner {
} }
} }
it.span(2) { 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" name "/serviceOne"
kind SERVER kind SERVER
attributes { attributes {
@ -117,7 +104,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner {
"apache-camel.uri" "http://0.0.0.0:$portOne/serviceOne" "apache-camel.uri" "http://0.0.0.0:$portOne/serviceOne"
} }
} }
it.span(4) { it.span(3) {
name "POST" name "POST"
kind CLIENT kind CLIENT
attributes { attributes {
@ -127,21 +114,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner {
"apache-camel.uri" "http://0.0.0.0:$portTwo/serviceTwo" "apache-camel.uri" "http://0.0.0.0:$portTwo/serviceTwo"
} }
} }
it.span(5) { it.span(4) {
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) {
name "/serviceTwo" name "/serviceTwo"
kind SERVER kind SERVER
attributes { attributes {
@ -156,7 +129,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentTestRunner {
} }
} }
it.span(7) { it.span(5) {
name "/serviceTwo" name "/serviceTwo"
kind INTERNAL kind INTERNAL
attributes { attributes {