Skip to content

Commit b61acff

Browse files
committed
other opentelemetry components
1 parent 3b47a85 commit b61acff

File tree

5 files changed

+245
-154
lines changed

5 files changed

+245
-154
lines changed

instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
import io.opentelemetry.context.Scope;
2222
import io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndSupport;
2323
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
24-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2524
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
2625
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2726
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2827
import io.opentelemetry.javaagent.tooling.config.MethodsConfigurationParser;
2928
import java.lang.reflect.Method;
3029
import java.util.Map;
3130
import java.util.Set;
31+
import javax.annotation.Nullable;
3232
import net.bytebuddy.asm.Advice;
33+
import net.bytebuddy.asm.Advice.AssignReturned;
3334
import net.bytebuddy.description.ByteCodeElement;
3435
import net.bytebuddy.description.annotation.AnnotationSource;
3536
import net.bytebuddy.description.method.MethodDescription;
@@ -119,89 +120,117 @@ static ElementMatcher.Junction<MethodDescription> configureExcludedMethods() {
119120
@SuppressWarnings("unused")
120121
public static class WithSpanAdvice {
121122

122-
@Advice.OnMethodEnter(suppress = Throwable.class)
123-
public static void onEnter(
124-
@Advice.Origin Method originMethod,
125-
@Advice.Local("otelMethod") Method method,
126-
@Advice.Local("otelContext") Context context,
127-
@Advice.Local("otelScope") Scope scope) {
128-
// Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it
129-
// to local variable so that there would be only one call to Class.getMethod.
130-
method = originMethod;
123+
public static class AdviceScope {
124+
private final Method method;
125+
private final Context context;
126+
private final Scope scope;
127+
128+
private AdviceScope(Method method, Context context, Scope scope) {
129+
this.method = method;
130+
this.context = context;
131+
this.scope = scope;
132+
}
131133

132-
Instrumenter<Method, Object> instrumenter = WithSpanSingletons.instrumenter();
133-
Context current = Java8BytecodeBridge.currentContext();
134+
@Nullable
135+
public static AdviceScope start(Method method) {
136+
Instrumenter<Method, Object> instrumenter = WithSpanSingletons.instrumenter();
137+
Context current = Context.current();
138+
if (!instrumenter.shouldStart(current, method)) {
139+
return null;
140+
}
141+
Context context = instrumenter.start(current, method);
142+
return new AdviceScope(method, context, context.makeCurrent());
143+
}
134144

135-
if (instrumenter.shouldStart(current, method)) {
136-
context = instrumenter.start(current, method);
137-
scope = context.makeCurrent();
145+
public Object end(Object returnValue, @Nullable Throwable throwable) {
146+
scope.close();
147+
AsyncOperationEndSupport<Method, Object> operationEndSupport =
148+
AsyncOperationEndSupport.create(
149+
WithSpanSingletons.instrumenter(), Object.class, method.getReturnType());
150+
return operationEndSupport.asyncEnd(context, method, returnValue, throwable);
138151
}
139152
}
140153

154+
@Nullable
155+
@Advice.OnMethodEnter(suppress = Throwable.class)
156+
public static AdviceScope onEnter(@Advice.Origin Method originMethod) {
157+
// Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it
158+
// to local variable so that there would be only one call to Class.getMethod.
159+
return AdviceScope.start(originMethod);
160+
}
161+
162+
@AssignReturned.ToReturned
141163
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
142-
public static void stopSpan(
143-
@Advice.Local("otelMethod") Method method,
144-
@Advice.Local("otelContext") Context context,
145-
@Advice.Local("otelScope") Scope scope,
146-
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue,
147-
@Advice.Thrown Throwable throwable) {
148-
if (scope == null) {
149-
return;
164+
public static Object stopSpan(
165+
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returnValue,
166+
@Advice.Thrown @Nullable Throwable throwable,
167+
@Advice.Enter @Nullable AdviceScope adviceScope) {
168+
if (adviceScope != null) {
169+
return adviceScope.end(returnValue, throwable);
150170
}
151-
scope.close();
152-
153-
AsyncOperationEndSupport<Method, Object> operationEndSupport =
154-
AsyncOperationEndSupport.create(
155-
WithSpanSingletons.instrumenter(), Object.class, method.getReturnType());
156-
returnValue = operationEndSupport.asyncEnd(context, method, returnValue, throwable);
171+
return returnValue;
157172
}
158173
}
159174

160175
@SuppressWarnings("unused")
161176
public static class WithSpanAttributesAdvice {
162177

178+
public static class AdviceScope {
179+
private final Method method;
180+
private final MethodRequest request;
181+
private final Context context;
182+
private final Scope scope;
183+
184+
private AdviceScope(Method method, MethodRequest request, Context context, Scope scope) {
185+
this.method = method;
186+
this.request = request;
187+
this.context = context;
188+
this.scope = scope;
189+
}
190+
191+
@Nullable
192+
public static AdviceScope start(Method method, MethodRequest request) {
193+
Instrumenter<MethodRequest, Object> instrumenter =
194+
WithSpanSingletons.instrumenterWithAttributes();
195+
Context current = Context.current();
196+
if (!instrumenter.shouldStart(current, request)) {
197+
return null;
198+
}
199+
Context context = instrumenter.start(current, request);
200+
return new AdviceScope(method, request, context, context.makeCurrent());
201+
}
202+
203+
public Object end(@Nullable Object returnValue, @Nullable Throwable throwable) {
204+
scope.close();
205+
AsyncOperationEndSupport<MethodRequest, Object> operationEndSupport =
206+
AsyncOperationEndSupport.create(
207+
WithSpanSingletons.instrumenterWithAttributes(),
208+
Object.class,
209+
method.getReturnType());
210+
return operationEndSupport.asyncEnd(context, request, returnValue, throwable);
211+
}
212+
}
213+
163214
@Advice.OnMethodEnter(suppress = Throwable.class)
164-
public static void onEnter(
215+
public static AdviceScope onEnter(
165216
@Advice.Origin Method originMethod,
166-
@Advice.Local("otelMethod") Method method,
167-
@Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args,
168-
@Advice.Local("otelRequest") MethodRequest request,
169-
@Advice.Local("otelContext") Context context,
170-
@Advice.Local("otelScope") Scope scope) {
171-
217+
@Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args) {
172218
// Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it
173219
// to local variable so that there would be only one call to Class.getMethod.
174-
method = originMethod;
175-
176-
Instrumenter<MethodRequest, Object> instrumenter =
177-
WithSpanSingletons.instrumenterWithAttributes();
178-
Context current = Java8BytecodeBridge.currentContext();
179-
request = new MethodRequest(method, args);
180-
181-
if (instrumenter.shouldStart(current, request)) {
182-
context = instrumenter.start(current, request);
183-
scope = context.makeCurrent();
184-
}
220+
MethodRequest request = new MethodRequest(originMethod, args);
221+
return AdviceScope.start(originMethod, request);
185222
}
186223

224+
@AssignReturned.ToReturned
187225
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
188-
public static void stopSpan(
189-
@Advice.Local("otelMethod") Method method,
190-
@Advice.Local("otelRequest") MethodRequest request,
191-
@Advice.Local("otelContext") Context context,
192-
@Advice.Local("otelScope") Scope scope,
193-
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue,
194-
@Advice.Thrown Throwable throwable) {
195-
if (scope == null) {
196-
return;
226+
public static Object stopSpan(
227+
@Advice.Return @Nullable Object returnValue,
228+
@Advice.Thrown Throwable throwable,
229+
@Advice.Enter AdviceScope adviceScope) {
230+
if (adviceScope != null) {
231+
return adviceScope.end(returnValue, throwable);
197232
}
198-
scope.close();
199-
AsyncOperationEndSupport<MethodRequest, Object> operationEndSupport =
200-
AsyncOperationEndSupport.create(
201-
WithSpanSingletons.instrumenterWithAttributes(),
202-
Object.class,
203-
method.getReturnType());
204-
returnValue = operationEndSupport.asyncEnd(context, request, returnValue, throwable);
233+
return returnValue;
205234
}
206235
}
207236
}

instrumentation/opentelemetry-extension-kotlin-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionkotlin/ContextExtensionInstrumentation.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
1616
import kotlin.coroutines.CoroutineContext;
1717
import net.bytebuddy.asm.Advice;
18+
import net.bytebuddy.asm.Advice.AssignReturned;
1819
import net.bytebuddy.description.type.TypeDescription;
1920
import net.bytebuddy.matcher.ElementMatcher;
2021

@@ -50,6 +51,7 @@ public static class ContextAdvice {
5051

5152
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
5253
public static CoroutineContext enter(@Advice.Argument(0) Context applicationContext) {
54+
5355
if (applicationContext != null) {
5456
io.opentelemetry.context.Context agentContext =
5557
AgentContextStorage.getAgentContext(applicationContext);
@@ -58,13 +60,16 @@ public static CoroutineContext enter(@Advice.Argument(0) Context applicationCont
5860
return null;
5961
}
6062

63+
@AssignReturned.ToReturned
6164
@Advice.OnMethodExit(onThrowable = Throwable.class)
62-
public static void onExit(
63-
@Advice.Return(readOnly = false) CoroutineContext result,
65+
public static CoroutineContext onExit(
66+
@Advice.Return CoroutineContext originalResult,
6467
@Advice.Enter CoroutineContext coroutineContext) {
68+
CoroutineContext result = originalResult;
6569
if (coroutineContext != null) {
6670
result = coroutineContext;
6771
}
72+
return result;
6873
}
6974
}
7075

@@ -75,22 +80,28 @@ public static class ImplicitContextKeyedAdvice {
7580
public static CoroutineContext enter(
7681
@Advice.Argument(0)
7782
application.io.opentelemetry.context.ImplicitContextKeyed implicitContextKeyed) {
83+
7884
if (implicitContextKeyed != null) {
7985
Context applicationContext = Context.current().with(implicitContextKeyed);
8086
io.opentelemetry.context.Context agentContext =
8187
AgentContextStorage.getAgentContext(applicationContext);
88+
8289
return ContextExtensionsKt.asContextElement(agentContext);
8390
}
91+
8492
return null;
8593
}
8694

95+
@AssignReturned.ToReturned
8796
@Advice.OnMethodExit(onThrowable = Throwable.class)
88-
public static void onExit(
89-
@Advice.Return(readOnly = false) CoroutineContext result,
97+
public static CoroutineContext onExit(
98+
@Advice.Return CoroutineContext originalResult,
9099
@Advice.Enter CoroutineContext coroutineContext) {
100+
CoroutineContext result = originalResult;
91101
if (coroutineContext != null) {
92102
result = coroutineContext;
93103
}
104+
return result;
94105
}
95106
}
96107

@@ -99,6 +110,7 @@ public static class GetOpenTelemetryContextAdvice {
99110

100111
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
101112
public static Context enter(@Advice.Argument(0) CoroutineContext coroutineContext) {
113+
102114
if (coroutineContext != null) {
103115
io.opentelemetry.context.Context agentContext =
104116
ContextExtensionsKt.getOpenTelemetryContext(coroutineContext);
@@ -107,12 +119,15 @@ public static Context enter(@Advice.Argument(0) CoroutineContext coroutineContex
107119
return null;
108120
}
109121

122+
@AssignReturned.ToReturned
110123
@Advice.OnMethodExit(onThrowable = Throwable.class)
111-
public static void onExit(
112-
@Advice.Return(readOnly = false) Context result, @Advice.Enter Context context) {
124+
public static Context onExit(
125+
@Advice.Return Context originalResult, @Advice.Enter Context context) {
126+
Context result = originalResult;
113127
if (context != null) {
114128
result = context;
115129
}
130+
return result;
116131
}
117132
}
118133
}

0 commit comments

Comments
 (0)