Skip to content

Commit

Permalink
feat: define trace level for RPC handler instrumentations (#77)
Browse files Browse the repository at this point in the history
Updates RPC handler instrumentations to only create spans when a specific trace level is configured.

Part of #71
  • Loading branch information
sissbruecker authored Sep 1, 2022
1 parent 73bb951 commit 7e8279a
Show file tree
Hide file tree
Showing 17 changed files with 422 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import com.vaadin.extension.ElementInstrumentationInfo;
import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature;
import com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -48,12 +49,13 @@ public void transform(TypeTransformer transformer) {
public static class AttachElementAdvice {
@Advice.OnMethodEnter()
public static void onEnter(
@Advice.This AttachExistingElementRpcHandler attachExistingElementRpcHandler,
@Advice.Origin("#m") String methodName,
@Advice.Argument(0) AttachExistingElementFeature feature,
@Advice.Argument(3) StateNode node,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (!Configuration.isEnabled(TraceLevel.DEFAULT)) {
return;
}

// Info for the element that is being attached
ElementInstrumentationInfo elementInfo = new ElementInstrumentationInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.vaadin.extension.ElementInstrumentationInfo;
import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;
import com.vaadin.flow.internal.StateNode;

import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -48,13 +50,16 @@ public static class AttachElementAdvice {
public static void onEnter(@Advice.Argument(0) StateNode node,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (!Configuration.isEnabled(TraceLevel.DEFAULT)) {
return;
}

// Info for the element that is being attached
ElementInstrumentationInfo elementInfo = new ElementInstrumentationInfo(
node);

span = InstrumentationHelper.startSpan(
"AttachTemplateChild: " + elementInfo.getElementLabel());
"Attach template child: " + elementInfo.getElementLabel());
span.setAttribute("vaadin.element.tag",
elementInfo.getElement().getTag());
// If possible add active view class name as an attribute to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

import com.vaadin.extension.ElementInstrumentationInfo;
import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.server.communication.rpc.EventRpcHandler;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -55,22 +56,21 @@ public void transform(TypeTransformer transformer) {
public static class MethodAdvice {

@Advice.OnMethodEnter()
public static void onEnter(@Advice.This EventRpcHandler eventRpcHandler,
@Advice.Origin("#m") String methodName,
@Advice.Argument(0) StateNode node,
public static void onEnter(@Advice.Argument(0) StateNode node,
@Advice.Argument(1) JsonObject jsonObject,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {

String spanName = eventRpcHandler.getClass().getSimpleName() + "."
+ methodName;
span = InstrumentationHelper.startSpan(spanName);

String eventType = jsonObject.getString("event");
if (eventType != null) {
if (Configuration.isEnabled(TraceLevel.DEFAULT)) {
String eventType = jsonObject.getString("event");
ElementInstrumentationInfo elementInfo = new ElementInstrumentationInfo(
node);
Element element = elementInfo.getElement();
// append event type to make span name more descriptive
String spanName = "Event: " + elementInfo.getElementLabel()
+ " :: " + eventType;
span = InstrumentationHelper.startSpan(spanName);

span.setAttribute("vaadin.element.tag", element.getTag());
span.setAttribute("vaadin.event.type", eventType);

Expand All @@ -90,24 +90,17 @@ public static void onEnter(@Advice.This EventRpcHandler eventRpcHandler,
span.setAttribute("vaadin.view",
elementInfo.getViewLabel());
}
// append event type to make span name more descriptive
span.updateName("Event: " + elementInfo.getElementLabel()
+ " :: " + eventType);
// This will make for instance a click span `vaadin-button ::
// click` instead of `EventRpcHandler.handle` which leaves open
// that what was this about

// Set the root span name to be the event
final Optional<String> activeRouteTemplate = getActiveRouteTemplate(
((StateTree) node.getOwner()).getUI());
String routeName = activeRouteTemplate.orElse("");
String eventRootSpanName = String.format("/%s : event",
routeName);
LocalRootSpan.current().updateName(eventRootSpanName);

Context context = currentContext().with(span);
scope = context.makeCurrent();
}

Context context = currentContext().with(span);
scope = context.makeCurrent();
// Set the root span name to be the event
final Optional<String> activeRouteTemplate = getActiveRouteTemplate(
((StateTree) node.getOwner()).getUI());
String routeName = activeRouteTemplate.orElse("");
String eventRootSpanName = String.format("/%s : event", routeName);
LocalRootSpan.current().updateName(eventRootSpanName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import com.vaadin.extension.ElementInstrumentationInfo;
import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.server.communication.rpc.MapSyncRpcHandler;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -51,13 +52,14 @@ public void transform(TypeTransformer transformer) {
public static class MethodAdvice {

@Advice.OnMethodEnter()
public static void onEnter(
@Advice.This MapSyncRpcHandler mapSyncRpcHandler,
@Advice.Origin("#m") String methodName,
@Advice.Argument(0) StateNode node,
public static void onEnter(@Advice.Argument(0) StateNode node,
@Advice.Argument(1) JsonObject jsonObject,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (!Configuration.isEnabled(TraceLevel.DEFAULT)) {
return;
}

final ElementInstrumentationInfo elementInfo = new ElementInstrumentationInfo(
node);
final Element element = elementInfo.getElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.flow.server.communication.rpc.NavigationRpcHandler;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -46,14 +47,13 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class MethodAdvice {
@Advice.OnMethodEnter()
public static void onEnter(
@Advice.This NavigationRpcHandler navigationRpcHandler,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelSpan") Span span,
public static void onEnter(@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (!Configuration.isEnabled(TraceLevel.MAXIMUM)) {
return;
}

String spanName = navigationRpcHandler.getClass().getSimpleName()
+ "." + methodName;
String spanName = "Handle navigation";
span = InstrumentationHelper.startSpan(spanName);

Context context = currentContext().with(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.server.communication.rpc.PublishedServerEventHandlerRpcHandler;

Expand Down Expand Up @@ -70,26 +72,21 @@ public static void onEnter(
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {

String spanName = rpcHandler.getClass().getSimpleName() + "."
+ methodName;
span = InstrumentationHelper.startSpan(spanName);
if (Configuration.isEnabled(TraceLevel.MAXIMUM)) {
String spanName = rpcHandler.getClass().getSimpleName() + "."
+ methodName;
span = InstrumentationHelper.startSpan(spanName);

Context context = currentContext().with(span);
scope = context.makeCurrent();
Context context = currentContext().with(span);
scope = context.makeCurrent();
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Thrown Throwable throwable,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (scope != null) {
scope.close();
}
if (span == null) {
return;
}
InstrumentationHelper.handleException(span, throwable);
span.end();
InstrumentationHelper.endSpan(span, throwable, scope);
}
}

Expand All @@ -104,15 +101,17 @@ public static void onEnter(@Advice.Argument(0) Component component,
return;
}

String spanName = String.format("Invoke server method: %s.%s",
component.getClass().getSimpleName(), method.getName());
span = InstrumentationHelper.startSpan(spanName);
span.setAttribute("vaadin.component",
component.getClass().getName());
span.setAttribute("vaadin.callable.method", method.toString());
if (Configuration.isEnabled(TraceLevel.DEFAULT)) {
String spanName = String.format("Invoke server method: %s.%s",
component.getClass().getSimpleName(), method.getName());
span = InstrumentationHelper.startSpan(spanName);
span.setAttribute("vaadin.component",
component.getClass().getName());
span.setAttribute("vaadin.callable.method", method.toString());

Context context = currentContext().with(span);
scope = context.makeCurrent();
Context context = currentContext().with(span);
scope = context.makeCurrent();
}

// Set the root span name to be the event
LocalRootSpan.current().updateName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.vaadin.extension.InstrumentationHelper;
import com.vaadin.flow.server.communication.ReturnChannelHandler;
import com.vaadin.extension.conf.Configuration;
import com.vaadin.extension.conf.TraceLevel;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -45,13 +46,13 @@ public void transform(TypeTransformer transformer) {
public static class MethodAdvice {

@Advice.OnMethodEnter()
public static void onEnter(
@Advice.This ReturnChannelHandler returnChannelHandler,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelSpan") Span span,
public static void onEnter(@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
String spanName = returnChannelHandler.getClass().getSimpleName()
+ "." + methodName;
if (!Configuration.isEnabled(TraceLevel.DEFAULT)) {
return;
}

String spanName = "Handle return channel";
span = InstrumentationHelper.startSpan(spanName);

Context context = currentContext().with(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public static class SynchronizedHandleRequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (Configuration.isEnabled(TraceLevel.DEFAULT)) {
final String spanName = "Handle Client Request";
span = InstrumentationHelper.startSpan(spanName);

Context context = currentContext().with(span);
scope = context.makeCurrent();
if (!Configuration.isEnabled(TraceLevel.DEFAULT)) {
return;
}

final String spanName = "Handle Client Request";
span = InstrumentationHelper.startSpan(spanName);

Context context = currentContext().with(span);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ protected Span getCapturedSpan(int index) {
return OpenTelemetryTestTools.getSpanBuilderCapture().getSpan(index);
}

protected Span getCapturedSpanOrNull(int index) {
return OpenTelemetryTestTools.getSpanBuilderCapture()
.getSpanOrNull(index);
}

protected int getCapturedSpanCount() {
return OpenTelemetryTestTools.getSpanBuilderCapture().getSpans().size();
}
Expand Down
Loading

0 comments on commit 7e8279a

Please sign in to comment.