public final class OpenTelemetryClassRule extends org.junit.rules.ExternalResource
Like OpenTelemetryRule
, except modeled after the junit5 implementation
OpenTelemetryExtension
. Use this class when you need to make asserts on SpanData
created on a MiniCluster. Make sure this rule initialized before the MiniCluster so that it can
register its instance of OpenTelemetry
as the global instance before any server-side
component can call TraceUtil.getGlobalTracer()
.
For example:
{ @code public class TestMyClass { private static final OpenTelemetryClassRule otelClassRule = OpenTelemetryClassRule.create(); private static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build(); protected static final ConnectionRule connectionRule = ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection); @ClassRule public static final TestRule classRule = RuleChain.outerRule(otelClassRule).around(miniClusterRule).around(connectionRule); @Rule public final OpenTelemetryTestRule otelTestRule = new OpenTelemetryTestRule(otelClassRule); @Test public void myTest() { // ... // do something that makes spans final Listspans = otelClassRule.getSpans(); // make assertions on them } } }
Modifier and Type | Field and Description |
---|---|
private io.opentelemetry.sdk.OpenTelemetrySdk |
openTelemetry |
private io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter |
spanExporter |
Modifier | Constructor and Description |
---|---|
private |
OpenTelemetryClassRule(io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry,
io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter) |
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
void |
clearSpans()
Clears the collected exported
SpanData . |
static OpenTelemetryClassRule |
create() |
io.opentelemetry.api.OpenTelemetry |
getOpenTelemetry()
Returns the
OpenTelemetry created by this Rule. |
List<io.opentelemetry.sdk.trace.data.SpanData> |
getSpans()
Returns all the exported
SpanData so far. |
private final io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry
private final io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter
private OpenTelemetryClassRule(io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry, io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter)
public static OpenTelemetryClassRule create()
public io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
OpenTelemetry
created by this Rule.public List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
SpanData
so far.public void clearSpans()
SpanData
.protected void before() throws Throwable
before
in class org.junit.rules.ExternalResource
Throwable
protected void after()
after
in class org.junit.rules.ExternalResource
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.