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 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 the
OpenTelemetrySdk for testing, resetting state
between tests.
// class CoolTest {
// @RegisterExtension
// static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
//
// private final Tracer tracer = otelTesting.getOpenTelemetry().getTracer("test");
// private final Meter meter = otelTesting.getOpenTelemetry().getMeter("test");
//
// @Test
// void test() {
// tracer.spanBuilder("name").startSpan().end();
// assertThat(otelTesting.getSpans()).containsExactly(expected);
//
// LongCounter counter = meter.counterBuilder("counter-name").build();
// counter.add(1);
// assertThat(otelTesting.getMetrics()).satisfiesExactlyInAnyOrder(metricData -> {});
// }
// }
-
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext context) Returns aTracesAssertfor asserting on the currently exported traces.voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) voidClears all registered metric instruments, such thatgetMetrics()is empty.voidClears the collected exportedSpanData.static OpenTelemetryExtensioncreate()Returns aOpenTelemetryExtensionwith a default SDK initialized with an in-memory span exporter and W3C trace context propagation.List<io.opentelemetry.sdk.metrics.data.MetricData>Returns the currentMetricDatainAggregationTemporality.CUMULATIVEformat.io.opentelemetry.api.OpenTelemetryReturns theOpenTelemetrySdkcreated by this extension.List<io.opentelemetry.sdk.trace.data.SpanData>getSpans()Returns all the exportedSpanDataso far.
-
Method Details
-
create
Returns aOpenTelemetryExtensionwith a default SDK initialized with an in-memory span exporter and W3C trace context propagation. -
getOpenTelemetry
public io.opentelemetry.api.OpenTelemetry getOpenTelemetry()Returns theOpenTelemetrySdkcreated by this extension. -
getSpans
Returns all the exportedSpanDataso far. -
getMetrics
Returns the currentMetricDatainAggregationTemporality.CUMULATIVEformat.- Since:
- 1.15.0
-
assertTraces
Returns aTracesAssertfor 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. -
clearMetrics
public void clearMetrics()Clears all registered metric instruments, such thatgetMetrics()is empty.- Since:
- 1.15.0
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-