Class OpenTelemetryRule
java.lang.Object
org.junit.rules.ExternalResource
io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class OpenTelemetryRule
extends org.junit.rules.ExternalResource
A JUnit4 rule which sets up the
OpenTelemetrySdk
for testing, resetting state between
tests. This rule cannot be used with ClassRule
.
// public class CoolTest {
// @Rule public OpenTelemetryRule otelTesting = OpenTelemetryRule.create();
//
// private Tracer tracer;
// private Meter meter;
//
// @Before
// public void setUp() {
// tracer = otelTesting.getOpenTelemetry().getTracer("test");
// meter = otelTesting.getOpenTelemetry().getMeter("test");
// }
//
// @Test
// public 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 TypeMethodDescriptionprotected void
after()
protected void
before()
void
Clears the collected exportedLogRecordData
.void
Clears all registered metric instruments, such thatgetMetrics()
is empty.void
Clears the collected exportedSpanData
.static OpenTelemetryRule
create()
Returns aOpenTelemetryRule
with a default SDK initialized with an in-memory span exporter and W3C trace context propagation.List<io.opentelemetry.sdk.logs.data.LogRecordData>
Returns all the exportedLogRecordData
so far.List<io.opentelemetry.sdk.metrics.data.MetricData>
Returns the currentMetricData
inAggregationTemporality.CUMULATIVE
format.io.opentelemetry.api.OpenTelemetry
Returns theOpenTelemetrySdk
created by this extension.List<io.opentelemetry.sdk.trace.data.SpanData>
getSpans()
Returns all the exportedSpanData
so far.Methods inherited from class org.junit.rules.ExternalResource
apply
-
Method Details
-
create
Returns aOpenTelemetryRule
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
Returns all the exportedSpanData
so far. -
getMetrics
Returns the currentMetricData
inAggregationTemporality.CUMULATIVE
format.- Since:
- 1.15.0
-
getLogRecords
Returns all the exportedLogRecordData
so far.- Since:
- 1.32.0
-
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
-
clearLogRecords
public void clearLogRecords()Clears the collected exportedLogRecordData
. Consider making your test smaller instead of manually clearing state using this method.- Since:
- 1.32.0
-
before
protected void before()- Overrides:
before
in classorg.junit.rules.ExternalResource
-
after
protected void after()- Overrides:
after
in classorg.junit.rules.ExternalResource
-