Convert dropwizard-views instrumentation to Instrumenter API
MODIFY
instrumentation/dropwizard-views-0.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardviews/DropwizardRendererInstrumentation.java
instrumentation/dropwizard-views-0.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardviews/DropwizardRendererInstrumentation.java
@@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.dropwizardviews;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
-import static io.opentelemetry.javaagent.instrumentation.dropwizardviews.DropwizardTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.dropwizardviews.DropwizardSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -52,14 +52,24 @@ public class DropwizardRendererInstrumentation implements TypeInstrumentation {
@Advice.Argument(0) View view,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
- if (Java8BytecodeBridge.currentSpan().getSpanContext().isValid()) {
- context = tracer().startSpan("Render " + view.getTemplateName());
- scope = context.makeCurrent();
+
+ Context parentContext = Java8BytecodeBridge.currentContext();
+
+ // don't start a new top-level span
+ if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
+ return;
+ }
+ if (!instrumenter().shouldStart(parentContext, view)) {
+ return;
}
+
+ context = instrumenter().start(parentContext, view);
+ scope = context.makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
+ @Advice.Argument(0) View view,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
@@ -67,11 +77,7 @@ public class DropwizardRendererInstrumentation implements TypeInstrumentation {
return;
}
scope.close();
- if (throwable == null) {
- tracer().end(context);
- } else {
- tracer().endExceptionally(context, throwable);
- }
+ instrumenter().end(context, view, null, throwable);
}
}
}

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

DELETE
instrumentation/dropwizard-views-0.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardviews/DropwizardTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.dropwizardviews;
-
-import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
-
-public class DropwizardTracer extends BaseTracer {
- private static final DropwizardTracer TRACER = new DropwizardTracer();
-
- public static DropwizardTracer tracer() {
- return TRACER;
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.dropwizard-views-0.7";
- }
-}

MODIFY
instrumentation/dropwizard-views-0.7/javaagent/src/test/groovy/ViewRenderTest.groovy
instrumentation/dropwizard-views-0.7/javaagent/src/test/groovy/ViewRenderTest.groovy
@@ -46,4 +46,17 @@ class ViewRenderTest extends AgentInstrumentationSpecification {
view = new View(template, StandardCharsets.UTF_8) {}
}
+
+ def "do not create span when there's no parent"() {
+ setup:
+ def outputStream = new ByteArrayOutputStream()
+ def view = new View("/views/ftl/utf8.ftl", StandardCharsets.UTF_8) {}
+
+ when:
+ new FreemarkerViewRenderer().render(view, Locale.ENGLISH, outputStream)
+
+ then:
+ Thread.sleep(500)
+ assert traces.isEmpty()
+ }
}

Convert dropwizard-views instrumentation to Instrumenter API (#3784)