Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -11,9 +11,9 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
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 @@ -41,36 +41,54 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class InvokeMethodAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(1) Class<?> componentClass,
@Advice.Argument(2) String methodName,
@Advice.Local("otelRequest") VaadinClientCallableRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static class InvokeMethodAdviceScope {
private final VaadinClientCallableRequest request;
private final Context context;
private final Scope scope;

Context parentContext = Java8BytecodeBridge.currentContext();
request = VaadinClientCallableRequest.create(componentClass, methodName);
if (!clientCallableInstrumenter().shouldStart(parentContext, request)) {
return;
private InvokeMethodAdviceScope(
VaadinClientCallableRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static InvokeMethodAdviceScope start(Class<?> componentClass, String methodName) {
Context parentContext = Context.current();
VaadinClientCallableRequest request =
VaadinClientCallableRequest.create(componentClass, methodName);
if (!clientCallableInstrumenter().shouldStart(parentContext, request)) {
return null;
}

Context context = clientCallableInstrumenter().start(parentContext, request);
Scope scope = context.makeCurrent();
return new InvokeMethodAdviceScope(request, context, scope);
}

context = clientCallableInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
public void end(@Nullable Throwable throwable) {
scope.close();

clientCallableInstrumenter().end(context, request, null, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static InvokeMethodAdviceScope onEnter(
@Advice.Argument(1) Class<?> componentClass, @Advice.Argument(2) String methodName) {
return InvokeMethodAdviceScope.start(componentClass, methodName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") VaadinClientCallableRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable InvokeMethodAdviceScope adviceScope) {
if (adviceScope == null) {
return;
}
scope.close();

clientCallableInstrumenter().end(context, request, null, throwable);
adviceScope.end(throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -45,35 +46,52 @@ public void transform(TypeTransformer transformer) {

@SuppressWarnings("unused")
public static class HandleRequestAdvice {
public static class HandleRequestAdviceScope {
private final VaadinHandlerRequest request;
private final Context context;
private final Scope scope;

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This RequestHandler requestHandler,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelRequest") VaadinHandlerRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
private HandleRequestAdviceScope(VaadinHandlerRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static HandleRequestAdviceScope start(Class<?> handlerClass, String methodName) {
VaadinHandlerRequest request = VaadinHandlerRequest.create(handlerClass, methodName);
Context context = helper().startRequestHandlerSpan(request);
if (context == null) {
return null;
}
Scope scope = context.makeCurrent();
return new HandleRequestAdviceScope(request, context, scope);
}

public void end(@Nullable Throwable throwable, boolean handled) {
scope.close();

request = VaadinHandlerRequest.create(requestHandler.getClass(), methodName);
context = helper().startRequestHandlerSpan(request);
if (context != null) {
scope = context.makeCurrent();
helper().endRequestHandlerSpan(context, request, throwable, handled);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static HandleRequestAdviceScope onEnter(
@Advice.This RequestHandler requestHandler, @Advice.Origin("#m") String methodName) {

return HandleRequestAdviceScope.start(requestHandler.getClass(), methodName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Return boolean handled,
@Advice.Local("otelRequest") VaadinHandlerRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Enter @Nullable HandleRequestAdviceScope adviceScope) {
if (adviceScope == null) {
return;
}
scope.close();

helper().endRequestHandlerSpan(context, request, throwable, handled);
adviceScope.end(throwable, handled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import elemental.json.JsonObject;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
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 @@ -48,37 +48,55 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class HandleAdvice {

public static class HandleAdviceScope {
private final VaadinRpcRequest request;
private final Context context;
private final Scope scope;

private HandleAdviceScope(VaadinRpcRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static HandleAdviceScope start(
RpcInvocationHandler handler, String methodName, JsonObject jsonObject) {
Context parentContext = Context.current();
VaadinRpcRequest request = VaadinRpcRequest.create(handler, methodName, jsonObject);
if (!rpcInstrumenter().shouldStart(parentContext, request)) {
return null;
}

Context context = rpcInstrumenter().start(parentContext, request);
Scope scope = context.makeCurrent();
return new HandleAdviceScope(request, context, scope);
}

public void end(@Nullable Throwable throwable) {
scope.close();

rpcInstrumenter().end(context, request, null, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
public static HandleAdviceScope onEnter(
@Advice.This RpcInvocationHandler rpcInvocationHandler,
@Advice.Origin("#m") String methodName,
@Advice.Argument(1) JsonObject jsonObject,
@Advice.Local("otelRequest") VaadinRpcRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Context parentContext = Java8BytecodeBridge.currentContext();
request = VaadinRpcRequest.create(rpcInvocationHandler, methodName, jsonObject);
if (!rpcInstrumenter().shouldStart(parentContext, request)) {
return;
}

context = rpcInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
@Advice.Argument(1) JsonObject jsonObject) {
return HandleAdviceScope.start(rpcInvocationHandler, methodName, jsonObject);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") VaadinRpcRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable HandleAdviceScope adviceScope) {
if (adviceScope == null) {
return;
}
scope.close();

rpcInstrumenter().end(context, request, null, throwable);
adviceScope.end(throwable);
}
}
}
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 VaadinInstrumentationModule extends InstrumentationModule {
public class VaadinInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public VaadinInstrumentationModule() {
super("vaadin", "vaadin-14.2");
Expand All @@ -38,4 +40,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new RpcInvocationHandlerInstrumentation(),
new ClientCallableRpcInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 All @@ -38,33 +39,51 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class HandleRequestAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This VaadinService vaadinService,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelRequest") VaadinServiceRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static class HandleRequestAdviceScope {
private final VaadinServiceRequest request;
private final Context context;
private final Scope scope;

private HandleRequestAdviceScope(VaadinServiceRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static HandleRequestAdviceScope start(Class<?> serviceClass, String methodName) {
VaadinServiceRequest request = VaadinServiceRequest.create(serviceClass, methodName);
Context context = helper().startVaadinServiceSpan(request);
if (context == null) {
return null;
}
Scope scope = context.makeCurrent();
return new HandleRequestAdviceScope(request, context, scope);
}

public void end(@Nullable Throwable throwable) {
scope.close();

request = VaadinServiceRequest.create(vaadinService.getClass(), methodName);
context = helper().startVaadinServiceSpan(request);
if (context != null) {
scope = context.makeCurrent();
helper().endVaadinServiceSpan(context, request, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static HandleRequestAdviceScope onEnter(
@Advice.This VaadinService vaadinService, @Advice.Origin("#m") String methodName) {

return HandleRequestAdviceScope.start(vaadinService.getClass(), methodName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") VaadinServiceRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable HandleRequestAdviceScope adviceScope) {
if (adviceScope == null) {
return;
}
scope.close();

helper().endVaadinServiceSpan(context, request, throwable);
adviceScope.end(throwable);
}
}
}