Skip to content

Commit 63c3813

Browse files
authored
make dropwizard indy-ready (#15014)
1 parent 2f5e43b commit 63c3813

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

instrumentation/dropwizard/dropwizard-metrics-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardmetrics/DropwizardMetricsInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import com.google.auto.service.AutoService;
1313
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1516
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1617
import java.util.List;
1718
import net.bytebuddy.matcher.ElementMatcher;
1819

1920
@AutoService(InstrumentationModule.class)
20-
public class DropwizardMetricsInstrumentationModule extends InstrumentationModule {
21+
public class DropwizardMetricsInstrumentationModule extends InstrumentationModule
22+
implements ExperimentalInstrumentationModule {
2123

2224
public DropwizardMetricsInstrumentationModule() {
2325
super("dropwizard-metrics", "dropwizard-metrics-4.0");
@@ -46,4 +48,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
4648
new MeterInstrumentation(),
4749
new TimerInstrumentation());
4850
}
51+
52+
@Override
53+
public boolean isIndyReady() {
54+
return true;
55+
}
4956
}

instrumentation/dropwizard/dropwizard-views-0.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardviews/DropwizardInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class DropwizardInstrumentationModule extends InstrumentationModule {
17+
public class DropwizardInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719
public DropwizardInstrumentationModule() {
1820
super("dropwizard-views", "dropwizard-views-0.7");
1921
}
@@ -22,4 +24,9 @@ public DropwizardInstrumentationModule() {
2224
public List<TypeInstrumentation> typeInstrumentations() {
2325
return singletonList(new DropwizardRendererInstrumentation());
2426
}
27+
28+
@Override
29+
public boolean isIndyReady() {
30+
return true;
31+
}
2532
}

instrumentation/dropwizard/dropwizard-views-0.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/dropwizardviews/DropwizardRendererInstrumentation.java

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
1515

1616
import io.dropwizard.views.View;
17+
import io.opentelemetry.api.trace.Span;
1718
import io.opentelemetry.context.Context;
1819
import io.opentelemetry.context.Scope;
19-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -47,37 +48,50 @@ public void transform(TypeTransformer transformer) {
4748
@SuppressWarnings("unused")
4849
public static class RenderAdvice {
4950

50-
@Advice.OnMethodEnter(suppress = Throwable.class)
51-
public static void onEnter(
52-
@Advice.Argument(0) View view,
53-
@Advice.Local("otelContext") Context context,
54-
@Advice.Local("otelScope") Scope scope) {
51+
public static class AdviceScope {
52+
private final Context context;
53+
private final Scope scope;
54+
55+
private AdviceScope(Context context, Scope scope) {
56+
this.context = context;
57+
this.scope = scope;
58+
}
5559

56-
Context parentContext = Java8BytecodeBridge.currentContext();
60+
@Nullable
61+
public static AdviceScope start(View view) {
62+
Context parentContext = Context.current();
5763

58-
// don't start a new top-level span
59-
if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
60-
return;
64+
// don't start a new top-level span
65+
if (!Span.fromContext(parentContext).getSpanContext().isValid()) {
66+
return null;
67+
}
68+
if (!instrumenter().shouldStart(parentContext, view)) {
69+
return null;
70+
}
71+
72+
Context context = instrumenter().start(parentContext, view);
73+
return new AdviceScope(context, context.makeCurrent());
6174
}
62-
if (!instrumenter().shouldStart(parentContext, view)) {
63-
return;
75+
76+
public void end(View view, @Nullable Throwable throwable) {
77+
scope.close();
78+
instrumenter().end(context, view, null, throwable);
6479
}
80+
}
6581

66-
context = instrumenter().start(parentContext, view);
67-
scope = context.makeCurrent();
82+
@Advice.OnMethodEnter(suppress = Throwable.class)
83+
public static AdviceScope onEnter(@Advice.Argument(0) View view) {
84+
return AdviceScope.start(view);
6885
}
6986

7087
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7188
public static void stopSpan(
7289
@Advice.Argument(0) View view,
73-
@Advice.Thrown Throwable throwable,
74-
@Advice.Local("otelContext") Context context,
75-
@Advice.Local("otelScope") Scope scope) {
76-
if (scope == null) {
77-
return;
90+
@Advice.Thrown @Nullable Throwable throwable,
91+
@Advice.Enter @Nullable AdviceScope adviceScope) {
92+
if (adviceScope != null) {
93+
adviceScope.end(view, throwable);
7894
}
79-
scope.close();
80-
instrumenter().end(context, view, null, throwable);
8195
}
8296
}
8397
}

0 commit comments

Comments
 (0)