camel instrumentation to set client context property
MODIFY
instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java
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);
}

MODIFY
instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java
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());
}
}

MODIFY
instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java
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);

MODIFY
instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java
instrumentation/apache-camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelTracer.java
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 = "";

MODIFY
instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/RestConfig.groovy
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}")

MODIFY
instrumentation/apache-camel-2.20/javaagent/src/test/groovy/test/TwoServicesWithDirectClientCamelTest.groovy
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 {

RENAME
instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback.xml
instrumentation/apache-camel-2.20/javaagent/src/test/resources/logback-test.xml
camel instrumentation to set client context property (#2042)