Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -49,41 +50,60 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class ApplyAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) Request<?> req,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, null)) {
return;
public static class AdviceScope {
private final Context context;
private final Scope scope;

public AdviceScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(Context parentContext) {
if (!instrumenter().shouldStart(parentContext, null)) {
return null;
}

Context context = instrumenter().start(parentContext, null);
return new AdviceScope(context, context.makeCurrent());
}

context = instrumenter().start(parentContext, null);
scope = context.makeCurrent();
public void end(
@Nullable Throwable throwable,
Future<Result> responseFuture,
Action<?> thisAction,
Request<?> req) {
scope.close();
updateSpan(context, req);

if (throwable == null) {
// span finished in RequestCompleteCallback
responseFuture.onComplete(
new RequestCompleteCallback(context), thisAction.executionContext());
} else {
instrumenter().end(context, null, null, throwable);
}
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) Request<?> req) {
return AdviceScope.start(currentContext());
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopTraceOnResponse(
@Advice.This Object thisAction,
@Advice.This Action<?> thisAction,
@Advice.Thrown Throwable throwable,
@Advice.Argument(0) Request<?> req,
@Advice.Return(readOnly = false) Future<Result> responseFuture,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Enter @Nullable AdviceScope actionScope) {
if (actionScope == null) {
return;
}
scope.close();

updateSpan(context, req);
// span finished in RequestCompleteCallback
if (throwable == null) {
responseFuture.onComplete(
new RequestCompleteCallback(context), ((Action<?>) thisAction).executionContext());
} else {
instrumenter().end(context, null, null, throwable);
}

actionScope.end(throwable, responseFuture, thisAction, req);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.play.v2_6.Play26Singletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.play.v2_6.Play26Singletons.updateSpan;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Expand All @@ -18,6 +17,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -49,44 +49,62 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class ApplyAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) Request<?> req,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, null)) {
return;
public static class AdviceScope {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a failure in play 2.6 tests scans.gradle.com/s/36fci3go5n3se

Strangely I haven't been able to reproduce the test failure locally. I am still investigating.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @laurit !

private final Context context;
private final Scope scope;

public AdviceScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(Context parentContext) {
if (!instrumenter().shouldStart(parentContext, null)) {
return null;
}

Context context = instrumenter().start(parentContext, null);
return new AdviceScope(context, context.makeCurrent());
}

context = instrumenter().start(parentContext, null);
scope = context.makeCurrent();
public void end(
@Nullable Throwable throwable,
Future<Result> responseFuture,
Action<?> thisAction,
Request<?> req) {
scope.close();

if (throwable == null) {
// span is finished when future completes
// not using responseFuture.onComplete() because that doesn't guarantee this handler span
// will be completed before the server span completes
responseFuture =
ResponseFutureWrapper.wrap(
responseFuture, context, thisAction.executionContext(), req);
} else {
instrumenter().end(context, null, null, throwable);
}
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) Request<?> req) {
return AdviceScope.start(currentContext());
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopTraceOnResponse(
@Advice.This Object thisAction,
@Advice.This Action<?> thisAction,
@Advice.Thrown Throwable throwable,
@Advice.Argument(0) Request<?> req,
@Advice.Return(readOnly = false) Future<Result> responseFuture,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Enter @Nullable AdviceScope actionScope) {
if (actionScope == null) {
return;
}
scope.close();

updateSpan(context, req);
if (throwable == null) {
// span is finished when future completes
// not using responseFuture.onComplete() because that doesn't guarantee this handler span
// will be completed before the server span completes
responseFuture =
ResponseFutureWrapper.wrap(
responseFuture, context, ((Action<?>) thisAction).executionContext());
} else {
instrumenter().end(context, null, null, throwable);
}

actionScope.end(throwable, responseFuture, thisAction, req);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package io.opentelemetry.javaagent.instrumentation.play.v2_6;

import static io.opentelemetry.javaagent.instrumentation.play.v2_6.Play26Singletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.play.v2_6.Play26Singletons.updateSpan;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.context.Context;
import play.api.mvc.Request;
import play.api.mvc.Result;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
Expand All @@ -17,13 +19,14 @@
public final class ResponseFutureWrapper {

public static Future<Result> wrap(
Future<Result> future, Context context, ExecutionContext executionContext) {
Future<Result> future, Context context, ExecutionContext executionContext, Request<?> req) {

return future.transform(
new AbstractFunction1<Result, Result>() {
@Override
@CanIgnoreReturnValue
public Result apply(Result result) {
updateSpan(context, req);
instrumenter().end(context, null, null, null);
return result;
}
Expand All @@ -32,6 +35,7 @@ public Result apply(Result result) {
@Override
@CanIgnoreReturnValue
public Throwable apply(Throwable throwable) {
updateSpan(context, req);
instrumenter().end(context, null, null, throwable);
return throwable;
}
Expand Down
Loading