-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bridge span keys defined in instrumentation api (#3911)
- Loading branch information
Showing
10 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
instrumentation/opentelemetry-api-1.0/testing/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
plugins { | ||
id("otel.javaagent-testing") | ||
} |
40 changes: 40 additions & 0 deletions
40
instrumentation/opentelemetry-api-1.0/testing/src/main/java/AgentSpanTesting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
public class AgentSpanTesting { | ||
|
||
/** | ||
* Runs the provided {@code runnable} inside the scope of an SERVER span with name {@code | ||
* spanName}. | ||
*/ | ||
public static void runWithServerSpan(String spanName, Runnable runnable) { | ||
runnable.run(); | ||
} | ||
|
||
/** | ||
* Runs the provided {@code runnable} inside the scope of an CONSUMER span with name {@code | ||
* spanName}. | ||
*/ | ||
public static void runWithConsumerSpan(String spanName, Runnable runnable) { | ||
runnable.run(); | ||
} | ||
|
||
/** | ||
* Runs the provided {@code runnable} inside the scope of an CLIENT span with name {@code | ||
* spanName}. | ||
*/ | ||
public static void runWithClientSpan(String spanName, Runnable runnable) { | ||
runnable.run(); | ||
} | ||
|
||
/** | ||
* Runs the provided {@code runnable} inside the scope of an INTERNAL span with name {@code | ||
* spanName}. Span is added into context under all possible keys from {@link | ||
* io.opentelemetry.instrumentation.api.instrumenter.SpanKey} | ||
*/ | ||
public static void runWithAllSpanKeys(String spanName, Runnable runnable) { | ||
runnable.run(); | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...entation/opentelemetry-api-1.0/testing/src/main/java/AgentSpanTestingInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
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 net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class AgentSpanTestingInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("AgentSpanTesting"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("runWithServerSpan"), this.getClass().getName() + "$RunWithServerSpanAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("runWithConsumerSpan"), this.getClass().getName() + "$RunWithConsumerSpanAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("runWithClientSpan"), this.getClass().getName() + "$RunWithClientSpanAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("runWithAllSpanKeys"), this.getClass().getName() + "$RunWithAllSpanKeysAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RunWithServerSpanAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) String spanName, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
context = AgentSpanTestingTracer.tracer().startServerSpan(spanName); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
scope.close(); | ||
if (throwable != null) { | ||
AgentSpanTestingTracer.tracer().endExceptionally(context, throwable); | ||
} else { | ||
AgentSpanTestingTracer.tracer().end(context); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RunWithConsumerSpanAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) String spanName, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
context = AgentSpanTestingTracer.tracer().startConsumerSpan(spanName); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
scope.close(); | ||
if (throwable != null) { | ||
AgentSpanTestingTracer.tracer().endExceptionally(context, throwable); | ||
} else { | ||
AgentSpanTestingTracer.tracer().end(context); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RunWithClientSpanAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) String spanName, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
context = AgentSpanTestingTracer.tracer().startClientSpan(spanName); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
scope.close(); | ||
if (throwable != null) { | ||
AgentSpanTestingTracer.tracer().endExceptionally(context, throwable); | ||
} else { | ||
AgentSpanTestingTracer.tracer().end(context); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RunWithAllSpanKeysAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) String spanName, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
context = AgentSpanTestingTracer.tracer().startSpanWithAllKeys(spanName); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
scope.close(); | ||
if (throwable != null) { | ||
AgentSpanTestingTracer.tracer().endExceptionally(context, throwable); | ||
} else { | ||
AgentSpanTestingTracer.tracer().end(context); | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...on/opentelemetry-api-1.0/testing/src/main/java/AgentSpanTestingInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class AgentSpanTestingInstrumentationModule extends InstrumentationModule { | ||
public AgentSpanTestingInstrumentationModule() { | ||
super(AgentSpanTestingInstrumentationModule.class.getName()); | ||
} | ||
|
||
@Override | ||
public boolean isHelperClass(String className) { | ||
return className.startsWith("AgentSpanTestingTracer"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new AgentSpanTestingInstrumentation()); | ||
} | ||
} |
Oops, something went wrong.