Simplify javaconcurrent instrumentation (#792)
* Remove unnecessary SpanWithScope * Inline method
This commit is contained in:
parent
425ce53853
commit
d1015a41af
|
@ -16,13 +16,11 @@
|
||||||
|
|
||||||
package io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent;
|
package io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent;
|
||||||
|
|
||||||
import static io.opentelemetry.trace.TracingContextUtils.getSpan;
|
|
||||||
|
|
||||||
import io.grpc.Context;
|
import io.grpc.Context;
|
||||||
import io.opentelemetry.OpenTelemetry;
|
import io.opentelemetry.OpenTelemetry;
|
||||||
import io.opentelemetry.auto.bootstrap.ContextStore;
|
import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.context.ContextUtils;
|
import io.opentelemetry.context.ContextUtils;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import io.opentelemetry.trace.Tracer;
|
import io.opentelemetry.trace.Tracer;
|
||||||
|
|
||||||
/** Helper utils for Runnable/Callable instrumentation */
|
/** Helper utils for Runnable/Callable instrumentation */
|
||||||
|
@ -39,22 +37,14 @@ public class AdviceUtils {
|
||||||
* @param <T> task's type
|
* @param <T> task's type
|
||||||
* @return scope if scope was started, or null
|
* @return scope if scope was started, or null
|
||||||
*/
|
*/
|
||||||
public static <T> SpanWithScope startTaskScope(
|
public static <T> Scope startTaskScope(final ContextStore<T, State> contextStore, final T task) {
|
||||||
final ContextStore<T, State> contextStore, final T task) {
|
|
||||||
State state = contextStore.get(task);
|
State state = contextStore.get(task);
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
Context parentContext = state.getAndResetParentContext();
|
Context parentContext = state.getAndResetParentContext();
|
||||||
if (parentContext != null) {
|
if (parentContext != null) {
|
||||||
return new SpanWithScope(
|
return ContextUtils.withScopedContext(parentContext);
|
||||||
getSpan(parentContext), ContextUtils.withScopedContext(parentContext));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void endTaskScope(final SpanWithScope spanWithScope) {
|
|
||||||
if (spanWithScope != null) {
|
|
||||||
spanWithScope.closeScope();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.auto.tooling.Instrumenter;
|
import io.opentelemetry.auto.tooling.Instrumenter;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -93,17 +93,17 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
* need to use that state.
|
* need to use that state.
|
||||||
*/
|
*/
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static SpanWithScope enter(@Advice.This final ForkJoinTask thiz) {
|
public static Scope enter(@Advice.This final ForkJoinTask thiz) {
|
||||||
ContextStore<ForkJoinTask, State> contextStore =
|
ContextStore<ForkJoinTask, State> contextStore =
|
||||||
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
||||||
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
||||||
if (thiz instanceof Runnable) {
|
if (thiz instanceof Runnable) {
|
||||||
ContextStore<Runnable, State> runnableContextStore =
|
ContextStore<Runnable, State> runnableContextStore =
|
||||||
InstrumentationContext.get(Runnable.class, State.class);
|
InstrumentationContext.get(Runnable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,10 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
if (thiz instanceof Callable) {
|
if (thiz instanceof Callable) {
|
||||||
ContextStore<Callable, State> callableContextStore =
|
ContextStore<Callable, State> callableContextStore =
|
||||||
InstrumentationContext.get(Callable.class, State.class);
|
InstrumentationContext.get(Callable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,10 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
}
|
}
|
||||||
|
|
||||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||||
public static void exit(@Advice.Enter final SpanWithScope scope) {
|
public static void exit(@Advice.Enter final Scope scope) {
|
||||||
AdviceUtils.endTaskScope(scope);
|
if (scope != null) {
|
||||||
|
scope.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.auto.tooling.Instrumenter;
|
import io.opentelemetry.auto.tooling.Instrumenter;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import net.bytebuddy.asm.Advice;
|
import net.bytebuddy.asm.Advice;
|
||||||
|
@ -64,15 +64,17 @@ public final class CallableInstrumentation extends Instrumenter.Default {
|
||||||
public static class CallableAdvice {
|
public static class CallableAdvice {
|
||||||
|
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static SpanWithScope enter(@Advice.This final Callable thiz) {
|
public static Scope enter(@Advice.This final Callable thiz) {
|
||||||
ContextStore<Callable, State> contextStore =
|
ContextStore<Callable, State> contextStore =
|
||||||
InstrumentationContext.get(Callable.class, State.class);
|
InstrumentationContext.get(Callable.class, State.class);
|
||||||
return AdviceUtils.startTaskScope(contextStore, thiz);
|
return AdviceUtils.startTaskScope(contextStore, thiz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||||
public static void exit(@Advice.Enter final SpanWithScope scope) {
|
public static void exit(@Advice.Enter final Scope scope) {
|
||||||
AdviceUtils.endTaskScope(scope);
|
if (scope != null) {
|
||||||
|
scope.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.auto.tooling.Instrumenter;
|
import io.opentelemetry.auto.tooling.Instrumenter;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -84,17 +84,17 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
* need to use that state.
|
* need to use that state.
|
||||||
*/
|
*/
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static SpanWithScope enter(@Advice.This final ForkJoinTask thiz) {
|
public static Scope enter(@Advice.This final ForkJoinTask thiz) {
|
||||||
ContextStore<ForkJoinTask, State> contextStore =
|
ContextStore<ForkJoinTask, State> contextStore =
|
||||||
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
||||||
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
||||||
if (thiz instanceof Runnable) {
|
if (thiz instanceof Runnable) {
|
||||||
ContextStore<Runnable, State> runnableContextStore =
|
ContextStore<Runnable, State> runnableContextStore =
|
||||||
InstrumentationContext.get(Runnable.class, State.class);
|
InstrumentationContext.get(Runnable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -103,10 +103,10 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
if (thiz instanceof Callable) {
|
if (thiz instanceof Callable) {
|
||||||
ContextStore<Callable, State> callableContextStore =
|
ContextStore<Callable, State> callableContextStore =
|
||||||
InstrumentationContext.get(Callable.class, State.class);
|
InstrumentationContext.get(Callable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -116,8 +116,10 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
}
|
}
|
||||||
|
|
||||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||||
public static void exit(@Advice.Enter final SpanWithScope scope) {
|
public static void exit(@Advice.Enter final Scope scope) {
|
||||||
AdviceUtils.endTaskScope(scope);
|
if (scope != null) {
|
||||||
|
scope.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.auto.tooling.Instrumenter;
|
import io.opentelemetry.auto.tooling.Instrumenter;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import net.bytebuddy.asm.Advice;
|
import net.bytebuddy.asm.Advice;
|
||||||
import net.bytebuddy.description.method.MethodDescription;
|
import net.bytebuddy.description.method.MethodDescription;
|
||||||
|
@ -63,15 +63,17 @@ public final class RunnableInstrumentation extends Instrumenter.Default {
|
||||||
public static class RunnableAdvice {
|
public static class RunnableAdvice {
|
||||||
|
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static SpanWithScope enter(@Advice.This final Runnable thiz) {
|
public static Scope enter(@Advice.This final Runnable thiz) {
|
||||||
ContextStore<Runnable, State> contextStore =
|
ContextStore<Runnable, State> contextStore =
|
||||||
InstrumentationContext.get(Runnable.class, State.class);
|
InstrumentationContext.get(Runnable.class, State.class);
|
||||||
return AdviceUtils.startTaskScope(contextStore, thiz);
|
return AdviceUtils.startTaskScope(contextStore, thiz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||||
public static void exit(@Advice.Enter final SpanWithScope scope) {
|
public static void exit(@Advice.Enter final Scope scope) {
|
||||||
AdviceUtils.endTaskScope(scope);
|
if (scope != null) {
|
||||||
|
scope.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
|
||||||
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
||||||
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.State;
|
||||||
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
|
|
||||||
import io.opentelemetry.auto.tooling.Instrumenter;
|
import io.opentelemetry.auto.tooling.Instrumenter;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -93,17 +93,17 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
* need to use that state.
|
* need to use that state.
|
||||||
*/
|
*/
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static SpanWithScope enter(@Advice.This final ForkJoinTask thiz) {
|
public static Scope enter(@Advice.This final ForkJoinTask thiz) {
|
||||||
ContextStore<ForkJoinTask, State> contextStore =
|
ContextStore<ForkJoinTask, State> contextStore =
|
||||||
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
InstrumentationContext.get(ForkJoinTask.class, State.class);
|
||||||
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
|
||||||
if (thiz instanceof Runnable) {
|
if (thiz instanceof Runnable) {
|
||||||
ContextStore<Runnable, State> runnableContextStore =
|
ContextStore<Runnable, State> runnableContextStore =
|
||||||
InstrumentationContext.get(Runnable.class, State.class);
|
InstrumentationContext.get(Runnable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(runnableContextStore, (Runnable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,10 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
if (thiz instanceof Callable) {
|
if (thiz instanceof Callable) {
|
||||||
ContextStore<Callable, State> callableContextStore =
|
ContextStore<Callable, State> callableContextStore =
|
||||||
InstrumentationContext.get(Callable.class, State.class);
|
InstrumentationContext.get(Callable.class, State.class);
|
||||||
SpanWithScope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
Scope newScope = AdviceUtils.startTaskScope(callableContextStore, (Callable) thiz);
|
||||||
if (null != newScope) {
|
if (null != newScope) {
|
||||||
if (null != scope) {
|
if (null != scope) {
|
||||||
newScope.closeScope();
|
newScope.close();
|
||||||
} else {
|
} else {
|
||||||
scope = newScope;
|
scope = newScope;
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,10 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
|
||||||
}
|
}
|
||||||
|
|
||||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||||
public static void exit(@Advice.Enter final SpanWithScope scope) {
|
public static void exit(@Advice.Enter final Scope scope) {
|
||||||
AdviceUtils.endTaskScope(scope);
|
if (scope != null) {
|
||||||
|
scope.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue