Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -8,11 +8,11 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

Expand Down Expand Up @@ -43,22 +43,20 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class ScheduleAdvice {

@Advice.AssignReturned.ToArguments(@ToArgument(2))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enterSchedule(
@Advice.Argument(value = 2, readOnly = false) Runnable runnable) {
Context context = Java8BytecodeBridge.currentContext();
runnable = context.wrap(runnable);
public static Runnable enterSchedule(@Advice.Argument(value = 2) Runnable runnable) {
return Java8BytecodeBridge.currentContext().wrap(runnable);
}
}

@SuppressWarnings("unused")
public static class ScheduleOnceAdvice {

@Advice.AssignReturned.ToArguments(@ToArgument(1))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enterScheduleOnce(
@Advice.Argument(value = 1, readOnly = false) Runnable runnable) {
Context context = Java8BytecodeBridge.currentContext();
runnable = context.wrap(runnable);
public static Runnable enterScheduleOnce(@Advice.Argument(value = 1) Runnable runnable) {
return Java8BytecodeBridge.currentContext().wrap(runnable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.pekko.http.scaladsl.HttpExt;
Expand All @@ -39,46 +40,55 @@ public void transform(TypeTransformer transformer) {

@SuppressWarnings("unused")
public static class SingleRequestAdvice {

@Advice.AssignReturned.ToArguments(@ToArgument(value = 0, index = 0))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(
@Advice.Argument(value = 0, readOnly = false) HttpRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static Object[] methodEnter(@Advice.Argument(value = 0) HttpRequest request) {
Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, request)) {
return;
return new Object[] {request, null, null};
}

context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
Context context = instrumenter().start(parentContext, request);
Scope scope = context.makeCurrent();

// Request is immutable, so we have to assign new value once we update headers
request = setter().inject(request);
HttpRequest modifiedRequest = setter().inject(request);

// Using array return form allows to provide at the same time
// - an argument value to override
// - propagate state from enter to exit advice
//
// As an array is already allocated we avoid creating another object
// by storing context and scope directly into the array.
return new Object[] {modifiedRequest, context, scope};
}

@Advice.AssignReturned.ToReturned
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
public static Future<HttpResponse> methodExit(
@Advice.Argument(0) HttpRequest request,
@Advice.This HttpExt thiz,
@Advice.Return(readOnly = false) Future<HttpResponse> responseFuture,
@Advice.Return Future<HttpResponse> responseFuture,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Enter Object[] enter) {

if (!(enter[1] instanceof Context) || !(enter[2] instanceof Scope)) {
return null;
}
((Scope) enter[2]).close();
Context context = (Context) enter[1];

scope.close();
if (throwable == null) {
responseFuture.onComplete(
new OnCompleteHandler(context, request), thiz.system().dispatcher());
} else {
if (throwable != null) {
instrumenter().end(context, request, null, throwable);
return responseFuture;
}
if (responseFuture != null) {
responseFuture =
FutureWrapper.wrap(responseFuture, thiz.system().dispatcher(), currentContext());
if (responseFuture == null) {
return null;
}
responseFuture.onComplete(
new OnCompleteHandler(context, request), thiz.system().dispatcher());

return FutureWrapper.wrap(responseFuture, thiz.system().dispatcher(), currentContext());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.pekko.http.scaladsl.model.HttpRequest;
Expand All @@ -34,10 +35,11 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class PekkoBindAndHandleAdvice {

@Advice.AssignReturned.ToArguments(@ToArgument(0))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrapHandler(
@Advice.Argument(value = 0, readOnly = false) Flow<HttpRequest, HttpResponse, ?> handler) {
handler = PekkoFlowWrapper.wrap(handler);
public static Flow<HttpRequest, HttpResponse, ?> wrapHandler(
@Advice.Argument(value = 0) Flow<HttpRequest, HttpResponse, ?> handler) {
return PekkoFlowWrapper.wrap(handler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class PekkoHttpServerInstrumentationModule extends InstrumentationModule {
public class PekkoHttpServerInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public PekkoHttpServerInstrumentationModule() {
super("pekko-http", "pekko-http-1.0", "pekko-http-server");
}
Expand All @@ -28,10 +30,8 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
}

@Override
public boolean isIndyModule() {
// PekkoHttpServerInstrumentationModule and PekkoHttpServerRouteInstrumentationModule share
// PekkoRouteHolder class
return false;
public String getModuleGroup() {
return "pekko-server";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.pekko.http.scaladsl.model.HttpRequest;
Expand All @@ -33,10 +34,11 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class PekkoBindAndHandleAdvice {

@Advice.AssignReturned.ToArguments(@ToArgument(0))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrapHandler(
@Advice.Argument(value = 0, readOnly = false) Flow<HttpRequest, HttpResponse, ?> handler) {
handler = PekkoFlowWrapper.wrap(handler);
public static Flow<HttpRequest, HttpResponse, ?> wrapHandler(
@Advice.Argument(value = 0) Flow<HttpRequest, HttpResponse, ?> handler) {
return PekkoFlowWrapper.wrap(handler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

/**
* This instrumentation applies to classes in pekko-http.jar while
* PekkoHttpServerInstrumentationModule applies to classes in pekko-http-core.jar
*/
@AutoService(InstrumentationModule.class)
public class PekkoHttpServerRouteInstrumentationModule extends InstrumentationModule {
public class PekkoHttpServerRouteInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public PekkoHttpServerRouteInstrumentationModule() {
super("pekko-http", "pekko-http-1.0", "pekko-http-server", "pekko-http-server-route");
}

@Override
public boolean isIndyModule() {
// PekkoHttpServerInstrumentationModule and PekkoHttpServerRouteInstrumentationModule share
// PekkoRouteHolder class
return false;
public String getModuleGroup() {
return "pekko-server";
}

@Override
Expand Down
Loading