Simplify javaconcurrent instrumentation (#792)

* Remove unnecessary SpanWithScope

* Inline method
This commit is contained in:
Trask Stalnaker 2020-07-26 01:30:14 -07:00 committed by GitHub
parent 425ce53853
commit d1015a41af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 48 deletions

View File

@ -16,13 +16,11 @@
package io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent;
import static io.opentelemetry.trace.TracingContextUtils.getSpan;
import io.grpc.Context;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
import io.opentelemetry.context.ContextUtils;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Tracer;
/** Helper utils for Runnable/Callable instrumentation */
@ -39,22 +37,14 @@ public class AdviceUtils {
* @param <T> task's type
* @return scope if scope was started, or null
*/
public static <T> SpanWithScope startTaskScope(
final ContextStore<T, State> contextStore, final T task) {
public static <T> Scope startTaskScope(final ContextStore<T, State> contextStore, final T task) {
State state = contextStore.get(task);
if (state != null) {
Context parentContext = state.getAndResetParentContext();
if (parentContext != null) {
return new SpanWithScope(
getSpan(parentContext), ContextUtils.withScopedContext(parentContext));
return ContextUtils.withScopedContext(parentContext);
}
}
return null;
}
public static void endTaskScope(final SpanWithScope spanWithScope) {
if (spanWithScope != null) {
spanWithScope.closeScope();
}
}
}

View File

@ -31,8 +31,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
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.context.Scope;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -93,17 +93,17 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
* need to use that state.
*/
@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 =
InstrumentationContext.get(ForkJoinTask.class, State.class);
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
if (thiz instanceof Runnable) {
ContextStore<Runnable, State> runnableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -112,10 +112,10 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
if (thiz instanceof Callable) {
ContextStore<Callable, State> callableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -125,8 +125,10 @@ public final class AkkaForkJoinTaskInstrumentation extends Instrumenter.Default
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter final SpanWithScope scope) {
AdviceUtils.endTaskScope(scope);
public static void exit(@Advice.Enter final Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}

View File

@ -27,8 +27,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
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.context.Scope;
import java.util.Map;
import java.util.concurrent.Callable;
import net.bytebuddy.asm.Advice;
@ -64,15 +64,17 @@ public final class CallableInstrumentation extends Instrumenter.Default {
public static class CallableAdvice {
@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 =
InstrumentationContext.get(Callable.class, State.class);
return AdviceUtils.startTaskScope(contextStore, thiz);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter final SpanWithScope scope) {
AdviceUtils.endTaskScope(scope);
public static void exit(@Advice.Enter final Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}

View File

@ -28,8 +28,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
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.context.Scope;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -84,17 +84,17 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
* need to use that state.
*/
@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 =
InstrumentationContext.get(ForkJoinTask.class, State.class);
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
if (thiz instanceof Runnable) {
ContextStore<Runnable, State> runnableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -103,10 +103,10 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
if (thiz instanceof Callable) {
ContextStore<Callable, State> callableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -116,8 +116,10 @@ public final class JavaForkJoinTaskInstrumentation extends Instrumenter.Default
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter final SpanWithScope scope) {
AdviceUtils.endTaskScope(scope);
public static void exit(@Advice.Enter final Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}

View File

@ -27,8 +27,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
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.context.Scope;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
@ -63,15 +63,17 @@ public final class RunnableInstrumentation extends Instrumenter.Default {
public static class RunnableAdvice {
@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 =
InstrumentationContext.get(Runnable.class, State.class);
return AdviceUtils.startTaskScope(contextStore, thiz);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter final SpanWithScope scope) {
AdviceUtils.endTaskScope(scope);
public static void exit(@Advice.Enter final Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}

View File

@ -29,8 +29,8 @@ import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.bootstrap.instrumentation.java.concurrent.AdviceUtils;
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.context.Scope;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -93,17 +93,17 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
* need to use that state.
*/
@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 =
InstrumentationContext.get(ForkJoinTask.class, State.class);
SpanWithScope scope = AdviceUtils.startTaskScope(contextStore, thiz);
Scope scope = AdviceUtils.startTaskScope(contextStore, thiz);
if (thiz instanceof Runnable) {
ContextStore<Runnable, State> runnableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -112,10 +112,10 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
if (thiz instanceof Callable) {
ContextStore<Callable, State> callableContextStore =
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 != scope) {
newScope.closeScope();
newScope.close();
} else {
scope = newScope;
}
@ -125,8 +125,10 @@ public final class ScalaForkJoinTaskInstrumentation extends Instrumenter.Default
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter final SpanWithScope scope) {
AdviceUtils.endTaskScope(scope);
public static void exit(@Advice.Enter final Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}