Class OpenTelemetryExtension
- java.lang.Object
-
- io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback
,org.junit.jupiter.api.extension.BeforeAllCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
public final class OpenTelemetryExtension extends java.lang.Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback
A JUnit5 extension which sets up theOpenTelemetrySdk
for testing, resetting state between tests.// class CoolTest { // @RegisterExtension // static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); // // private final Tracer tracer = otelTesting.getOpenTelemetry().getTracer("test"); // // @Test // void test() { // tracer.spanBuilder("name").startSpan().end(); // assertThat(otelTesting.getSpans()).containsExactly(expected); // } // }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
TracesAssert
assertTraces()
Returns aTracesAssert
for asserting on the currently exported traces.void
beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
void
clearSpans()
Clears the collected exportedSpanData
.static OpenTelemetryExtension
create()
Returns aOpenTelemetryExtension
with a default SDK initialized with an in-memory span exporter and W3C trace context propagation.io.opentelemetry.api.OpenTelemetry
getOpenTelemetry()
Returns theOpenTelemetrySdk
created by this extension.java.util.List<io.opentelemetry.sdk.trace.data.SpanData>
getSpans()
Returns all the exportedSpanData
so far.
-
-
-
Method Detail
-
create
public static OpenTelemetryExtension create()
Returns aOpenTelemetryExtension
with a default SDK initialized with an in-memory span exporter and W3C trace context propagation.
-
getOpenTelemetry
public io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
Returns theOpenTelemetrySdk
created by this extension.
-
getSpans
public java.util.List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
Returns all the exportedSpanData
so far.
-
assertTraces
public TracesAssert assertTraces()
Returns aTracesAssert
for asserting on the currently exported traces. This method requires AssertJ to be on the classpath.
-
clearSpans
public void clearSpans()
Clears the collected exportedSpanData
. Consider making your test smaller instead of manually clearing state using this method.
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
-