Skip to content

Commit 7640875

Browse files
committed
Refactor out camel ContextWithScope into a bootstrap package so it can be loaded by different ClassLoaders
1 parent 4b184e7 commit 7640875

File tree

7 files changed

+72
-50
lines changed

7 files changed

+72
-50
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
id("otel.javaagent-bootstrap")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.opentelemetry.javaagent.bootstrap.apachecamel;
2+
3+
import javax.annotation.Nullable;
4+
import io.opentelemetry.context.Context;
5+
import io.opentelemetry.context.Scope;
6+
7+
public class ContextWithScope {
8+
@Nullable private final ContextWithScope parent;
9+
@Nullable private final Context context;
10+
@Nullable private final Scope scope;
11+
12+
public ContextWithScope(
13+
ContextWithScope parent, Context context, Scope scope) {
14+
this.parent = parent;
15+
this.context = context;
16+
this.scope = scope;
17+
}
18+
19+
public static ContextWithScope activate(ContextWithScope parent, Context context) {
20+
Scope scope = context != null ? context.makeCurrent() : null;
21+
return new ContextWithScope(parent, context, scope);
22+
}
23+
24+
public Context getContext() {
25+
return context;
26+
}
27+
28+
public ContextWithScope getParent() {
29+
return parent;
30+
}
31+
32+
public void deactivate() {
33+
if (scope == null) {
34+
return;
35+
}
36+
scope.close();
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "ContextWithScope [context=" + context + ", scope=" + scope + "]";
42+
}
43+
}

instrumentation/camel-2.20/javaagent/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030

3131
compileOnly("com.google.auto.value:auto-value-annotations")
3232
annotationProcessor("com.google.auto.value:auto-value")
33+
bootstrap(project(":instrumentation:camel-2.20:bootstrap"))
3334

3435
testInstrumentation(project(":instrumentation:apache-httpclient:apache-httpclient-2.0:javaagent"))
3536
testInstrumentation(project(":instrumentation:servlet:servlet-3.0:javaagent"))

instrumentation/camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveContextManager.java

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323

2424
package io.opentelemetry.javaagent.instrumentation.apachecamel;
2525

26-
import static io.opentelemetry.javaagent.instrumentation.apachecamel.CamelSingletons.instrumenter;
2726
import static java.util.logging.Level.FINE;
2827

28+
import io.opentelemetry.javaagent.bootstrap.apachecamel.ContextWithScope;
2929
import io.opentelemetry.context.Context;
30-
import io.opentelemetry.context.Scope;
3130
import java.util.logging.Logger;
32-
import javax.annotation.Nullable;
3331
import org.apache.camel.Exchange;
3432

3533
/** Utility class for managing active contexts as a stack associated with an exchange. */
@@ -45,13 +43,12 @@ private ActiveContextManager() {}
4543
* This method activates the supplied context for the supplied exchange. If an existing context is
4644
* found for the exchange it will be pushed onto a stack.
4745
*
48-
* @param context The exchange
49-
* @param request The context
46+
* @param context The context
47+
* @param exchange The exchange
5048
*/
51-
public static void activate(Context context, CamelRequest request) {
52-
Exchange exchange = request.getExchange();
49+
public static void activate(Context context, Exchange exchange) {
5350
ContextWithScope parent = exchange.getProperty(ACTIVE_CONTEXT_PROPERTY, ContextWithScope.class);
54-
ContextWithScope contextWithScope = ContextWithScope.activate(parent, context, request);
51+
ContextWithScope contextWithScope = ContextWithScope.activate(parent, context);
5552
exchange.setProperty(ACTIVE_CONTEXT_PROPERTY, contextWithScope);
5653
logger.log(FINE, "Activated a span: {0}", contextWithScope);
5754
}
@@ -68,50 +65,13 @@ public static Context deactivate(Exchange exchange) {
6865
exchange.getProperty(ACTIVE_CONTEXT_PROPERTY, ContextWithScope.class);
6966

7067
if (contextWithScope != null) {
71-
contextWithScope.deactivate(exchange.getException());
68+
contextWithScope.deactivate();
7269
exchange.setProperty(ACTIVE_CONTEXT_PROPERTY, contextWithScope.getParent());
7370
logger.log(FINE, "Deactivated span: {0}", contextWithScope);
74-
return contextWithScope.context;
71+
return contextWithScope.getContext();
7572
}
7673

7774
return null;
7875
}
7976

80-
private static class ContextWithScope {
81-
@Nullable private final ContextWithScope parent;
82-
@Nullable private final Context context;
83-
private final CamelRequest request;
84-
@Nullable private final Scope scope;
85-
86-
public ContextWithScope(
87-
ContextWithScope parent, Context context, CamelRequest request, Scope scope) {
88-
this.parent = parent;
89-
this.context = context;
90-
this.request = request;
91-
this.scope = scope;
92-
}
93-
94-
public static ContextWithScope activate(
95-
ContextWithScope parent, Context context, CamelRequest request) {
96-
Scope scope = context != null ? context.makeCurrent() : null;
97-
return new ContextWithScope(parent, context, request, scope);
98-
}
99-
100-
public ContextWithScope getParent() {
101-
return parent;
102-
}
103-
104-
public void deactivate(Exception exception) {
105-
if (scope == null) {
106-
return;
107-
}
108-
scope.close();
109-
instrumenter().end(context, request, null, exception);
110-
}
111-
112-
@Override
113-
public String toString() {
114-
return "ContextWithScope [context=" + context + ", scope=" + scope + "]";
115-
}
116-
}
11777
}

instrumentation/camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelEventNotifier.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.context.Context;
3131
import java.util.EventObject;
3232
import java.util.logging.Logger;
33+
import org.apache.camel.Exchange;
3334
import org.apache.camel.management.event.ExchangeSendingEvent;
3435
import org.apache.camel.management.event.ExchangeSentEvent;
3536
import org.apache.camel.support.EventNotifierSupport;
@@ -63,7 +64,7 @@ private static void onExchangeSending(ExchangeSendingEvent ese) {
6364
sd.getInitiatorSpanKind());
6465
Context context = startOnExchangeSending(request);
6566

66-
ActiveContextManager.activate(context, request);
67+
ActiveContextManager.activate(context, request.getExchange());
6768
CamelPropagationUtil.injectParent(context, ese.getExchange().getIn().getHeaders());
6869

6970
logger.log(FINE, "[Exchange sending] Initiator span started: {0}", context);
@@ -84,7 +85,16 @@ private static void onExchangeSent(ExchangeSentEvent event) {
8485
return;
8586
}
8687

87-
Context context = ActiveContextManager.deactivate(event.getExchange());
88+
Exchange exchange = event.getExchange();
89+
Context context = ActiveContextManager.deactivate(exchange);
90+
CamelRequest request =
91+
CamelRequest.create(
92+
sd,
93+
exchange,
94+
event.getEndpoint(),
95+
CamelDirection.OUTBOUND,
96+
sd.getInitiatorSpanKind());
97+
instrumenter().end(context, request, null, exchange.getException());
8898
logger.log(FINE, "[Exchange sent] Initiator span finished: {0}", context);
8999
}
90100

instrumentation/camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRoutePolicy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static Context spanOnExchangeBegin(
5656
return null;
5757
}
5858
Context context = instrumenter().start(parentContext, request);
59-
ActiveContextManager.activate(context, request);
59+
ActiveContextManager.activate(context, exchange);
6060
return context;
6161
}
6262

@@ -80,7 +80,11 @@ public void onExchangeBegin(Route route, Exchange exchange) {
8080
/** Route exchange done. Get active CAMEL span, finish, remove from CAMEL holder. */
8181
@Override
8282
public void onExchangeDone(Route route, Exchange exchange) {
83+
SpanDecorator sd = getSpanDecorator(route.getEndpoint());
8384
Context context = ActiveContextManager.deactivate(exchange);
85+
CamelRequest request =
86+
CamelRequest.create(sd, exchange, route.getEndpoint(), CamelDirection.INBOUND, SpanKind.INTERNAL);
87+
instrumenter().end(context, request, null, exchange.getException());
8488
logger.log(FINE, "[Route finished] Receiver span finished {0}", context);
8589
}
8690
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ include(":instrumentation:azure-core:azure-core-1.36:library-instrumentation-sha
180180
include(":instrumentation:c3p0-0.9:javaagent")
181181
include(":instrumentation:c3p0-0.9:library")
182182
include(":instrumentation:c3p0-0.9:testing")
183+
include(":instrumentation:camel-2.20:bootstrap")
183184
include(":instrumentation:camel-2.20:javaagent")
184185
include(":instrumentation:camel-2.20:javaagent-unit-tests")
185186
include(":instrumentation:cassandra:cassandra-3.0:javaagent")

0 commit comments

Comments
 (0)