Update Grails to Instrumenter API
MODIFY
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/DefaultGrailsControllerClassInstrumentation.java
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/DefaultGrailsControllerClassInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.grails;
-import static io.opentelemetry.javaagent.instrumentation.grails.GrailsTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.grails.GrailsSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -16,6 +16,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
@@ -46,27 +47,31 @@ public class DefaultGrailsControllerClassInstrumentation implements TypeInstrume
@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) {
- context = tracer().startSpan(controller, action != null ? action : defaultActionName);
+ Context parentContext = Java8BytecodeBridge.currentContext();
+ handlerData = new HandlerData(controller, action != null ? action : defaultActionName);
+ if (!instrumenter().shouldStart(parentContext, handlerData)) {
+ return;
+ }
+
+ context = instrumenter().start(parentContext, handlerData);
scope = context.makeCurrent();
}
@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;
}
scope.close();
- if (throwable == null) {
- tracer().end(context);
- } else {
- tracer().endExceptionally(context, throwable);
- }
+ instrumenter().end(context, handlerData, null, throwable);
}
}
}

ADD
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/GrailsServerSpanNaming.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.grails;
+
+import io.opentelemetry.context.Context;
+import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
+import java.util.function.Supplier;
+import org.grails.web.mapping.mvc.GrailsControllerUrlMappingInfo;
+
+public class GrailsServerSpanNaming {
+
+ public static Supplier<String> getServerSpanNameSupplier(
+ Context context, GrailsControllerUrlMappingInfo info) {
+ return () -> getServerSpanName(context, info);
+ }
+
+ private static String getServerSpanName(Context context, GrailsControllerUrlMappingInfo info) {
+ String action =
+ info.getActionName() != null
+ ? info.getActionName()
+ : info.getControllerClass().getDefaultAction();
+ return ServletContextPath.prepend(context, "/" + info.getControllerName() + "/" + action);
+ }
+}

ADD
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/GrailsSingletons.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.grails;
+
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+
+public final class GrailsSingletons {
+ private static final String INSTRUMENTATION_NAME = "io.opentelemetry.grails-3.0";
+
+ private static final Instrumenter<HandlerData, Void> INSTRUMENTER;
+
+ static {
+ INSTRUMENTER =
+ Instrumenter.<HandlerData, Void>newBuilder(
+ GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, HandlerData::spanName)
+ .newInstrumenter();
+ }
+
+ public static Instrumenter<HandlerData, Void> instrumenter() {
+ return INSTRUMENTER;
+ }
+
+ private GrailsSingletons() {}
+}

DELETE
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/GrailsTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.grails;
-
-import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTROLLER;
-
-import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
-import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
-import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
-import io.opentelemetry.instrumentation.api.tracer.SpanNames;
-import org.grails.web.mapping.mvc.GrailsControllerUrlMappingInfo;
-
-public class GrailsTracer extends BaseTracer {
-
- private static final GrailsTracer TRACER = new GrailsTracer();
-
- public static GrailsTracer tracer() {
- return TRACER;
- }
-
- public Context startSpan(Object controller, String action) {
- return startSpan(SpanNames.fromMethod(controller.getClass(), action));
- }
-
- public void updateServerSpanName(Context context, GrailsControllerUrlMappingInfo info) {
- ServerSpanNaming.updateServerSpanName(
- context, CONTROLLER, () -> getServerSpanName(context, info));
- }
-
- private static String getServerSpanName(Context context, GrailsControllerUrlMappingInfo info) {
- String action =
- info.getActionName() != null
- ? info.getActionName()
- : info.getControllerClass().getDefaultAction();
- return ServletContextPath.prepend(context, "/" + info.getControllerName() + "/" + action);
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.grails-3.0";
- }
-}

ADD
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/HandlerData.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.grails;
+
+import io.opentelemetry.instrumentation.api.tracer.SpanNames;
+
+public class HandlerData {
+
+ private final Object controller;
+ private final String action;
+
+ public HandlerData(Object controller, String action) {
+ this.controller = controller;
+ this.action = action;
+ }
+
+ String spanName() {
+ return SpanNames.fromMethod(controller.getClass(), action);
+ }
+}

MODIFY
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/UrlMappingsInfoHandlerAdapterInstrumentation.java
instrumentation/grails-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/UrlMappingsInfoHandlerAdapterInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.grails;
-import static io.opentelemetry.javaagent.instrumentation.grails.GrailsTracer.tracer;
+import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTROLLER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -13,6 +13,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import io.opentelemetry.context.Context;
+import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
@@ -46,7 +47,12 @@ public class UrlMappingsInfoHandlerAdapterInstrumentation implements TypeInstrum
if (handler instanceof GrailsControllerUrlMappingInfo) {
Context parentContext = Java8BytecodeBridge.currentContext();
- tracer().updateServerSpanName(parentContext, (GrailsControllerUrlMappingInfo) handler);
+
+ ServerSpanNaming.updateServerSpanName(
+ parentContext,
+ CONTROLLER,
+ GrailsServerSpanNaming.getServerSpanNameSupplier(
+ parentContext, (GrailsControllerUrlMappingInfo) handler));
}
}
}

Update Grails to Instrumenter API (#3795)