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 @@ -14,9 +14,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 All @@ -42,36 +42,54 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class ControllerAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startSpan(
@Advice.Argument(0) Object controller,
@Advice.Argument(1) String action,
@Advice.FieldValue("defaultActionName") String defaultActionName,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static class AdviceScope {
private final HandlerData handlerData;
private final Context context;
private final Scope scope;

private AdviceScope(HandlerData handlerData, Context context, Scope scope) {
this.handlerData = handlerData;
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(
Object controller, @Nullable String action, String defaultActionName) {
Context parentContext = Context.current();
HandlerData handlerData =
new HandlerData(controller, action != null ? action : defaultActionName);
if (!instrumenter().shouldStart(parentContext, handlerData)) {
return null;
}

Context parentContext = Java8BytecodeBridge.currentContext();
handlerData = new HandlerData(controller, action != null ? action : defaultActionName);
if (!instrumenter().shouldStart(parentContext, handlerData)) {
return;
Context context = instrumenter().start(parentContext, handlerData);
return new AdviceScope(handlerData, context, context.makeCurrent());
}

context = instrumenter().start(parentContext, handlerData);
scope = context.makeCurrent();
public void end(Throwable throwable) {
scope.close();
instrumenter().end(context, handlerData, null, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope startSpan(
@Advice.Argument(0) Object controller,
@Advice.Argument(1) @Nullable String action,
@Advice.FieldValue("defaultActionName") String defaultActionName) {

return AdviceScope.start(controller, action, defaultActionName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable);
}
scope.close();
instrumenter().end(context, handlerData, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
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;

@AutoService(InstrumentationModule.class)
public class GrailsInstrumentationModule extends InstrumentationModule {
public class GrailsInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public GrailsInstrumentationModule() {
super("grails", "grails-3.0");
}
Expand All @@ -24,4 +26,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new DefaultGrailsControllerClassInstrumentation(),
new UrlMappingsInfoHandlerAdapterInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading