Migrate Finatra instrumentation to Instrumenter API
MODIFY
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraResponseListener.java
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraResponseListener.java
package io.opentelemetry.javaagent.instrumentation.finatra;
-import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraSingletons.instrumenter;
import com.twitter.finagle.http.Response;
import com.twitter.util.FutureEventListener;
import io.opentelemetry.context.Context;
-import io.opentelemetry.context.Scope;
-public class FinatraResponseListener implements FutureEventListener<Response> {
+public final class FinatraResponseListener implements FutureEventListener<Response> {
+
private final Context context;
- private final Scope scope;
+ private final Class<?> request;
- public FinatraResponseListener(Context context, Scope scope) {
+ public FinatraResponseListener(Context context, Class<?> request) {
this.context = context;
- this.scope = scope;
+ this.request = request;
}
@Override
public void onSuccess(Response response) {
- scope.close();
- tracer().end(context);
+ instrumenter().end(context, request, null, null);
}
@Override
public void onFailure(Throwable cause) {
- scope.close();
- tracer().endExceptionally(context, cause);
+ instrumenter().end(context, request, null, cause);
}
}

MODIFY
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraRouteInstrumentation.java
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraRouteInstrumentation.java
@@ -7,7 +7,8 @@ package io.opentelemetry.javaagent.instrumentation.finatra;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
-import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraSingletons.instrumenter;
+import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraSingletons.updateServerSpanName;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -17,10 +18,8 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import com.twitter.finagle.http.Response;
import com.twitter.finatra.http.contexts.RouteInfo;
import com.twitter.util.Future;
-import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
-import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
@@ -57,17 +56,18 @@ public class FinatraRouteInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void nameSpan(
@Advice.FieldValue("routeInfo") RouteInfo routeInfo,
- @Advice.FieldValue("clazz") Class<?> clazz,
+ @Advice.FieldValue("clazz") Class<?> request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = Java8BytecodeBridge.currentContext();
- Span serverSpan = ServerSpan.fromContextOrNull(parentContext);
- if (serverSpan != null) {
- serverSpan.updateName(routeInfo.path());
+ updateServerSpanName(parentContext, routeInfo);
+
+ if (!instrumenter().shouldStart(parentContext, request)) {
+ return;
}
- context = tracer().startSpan(parentContext, clazz);
+ context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}
@@ -75,20 +75,20 @@ public class FinatraRouteInstrumentation implements TypeInstrumentation {
public static void setupCallback(
@Advice.Thrown Throwable throwable,
@Advice.Return Some<Future<Response>> responseOption,
+ @Advice.FieldValue("clazz") Class<?> request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}
+ scope.close();
if (throwable != null) {
- scope.close();
- tracer().endExceptionally(context, throwable);
- return;
+ instrumenter().end(context, request, null, throwable);
+ } else {
+ responseOption.get().addEventListener(new FinatraResponseListener(context, request));
}
-
- responseOption.get().addEventListener(new FinatraResponseListener(context, scope));
}
}
}

ADD
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraSingletons.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.finatra;
+
+import com.twitter.finatra.http.contexts.RouteInfo;
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
+import io.opentelemetry.instrumentation.api.tracer.ClassNames;
+
+public final class FinatraSingletons {
+
+ private static final Instrumenter<Class<?>, Void> INSTRUMENTER =
+ Instrumenter.<Class<?>, Void>builder(
+ GlobalOpenTelemetry.get(), "io.opentelemetry.finatra-2.9", ClassNames::simpleName)
+ .newInstrumenter();
+
+ public static Instrumenter<Class<?>, Void> instrumenter() {
+ return INSTRUMENTER;
+ }
+
+ public static void updateServerSpanName(Context context, RouteInfo routeInfo) {
+ ServerSpanNaming.updateServerSpanName(
+ context, ServerSpanNaming.Source.CONTROLLER, (c, route) -> route.path(), routeInfo);
+ }
+
+ private FinatraSingletons() {}
+}

DELETE
instrumentation/finatra-2.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finatra/FinatraTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.finatra;
-
-import io.opentelemetry.api.trace.SpanKind;
-import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
-import io.opentelemetry.instrumentation.api.tracer.ClassNames;
-
-public class FinatraTracer extends BaseTracer {
- private static final FinatraTracer TRACER = new FinatraTracer();
-
- public static FinatraTracer tracer() {
- return TRACER;
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.finatra-2.9";
- }
-
- public Context startSpan(Context parentContext, Class<?> clazz) {
- return super.startSpan(parentContext, ClassNames.simpleName(clazz), SpanKind.INTERNAL);
- }
-}

Migrate Finatra instrumentation to Instrumenter API (#4495)