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 Type
    Method
    Description
    protected void
     
    protected void
     
    void
    Clears the collected exported LogRecordData.
    void
    Clears all registered metric instruments, such that getMetrics() is empty.
    void
    Clears the collected exported SpanData.
    Returns a OpenTelemetryRule 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 exported LogRecordData so far.
    List<io.opentelemetry.sdk.metrics.data.MetricData>
    Returns the current MetricData in AggregationTemporality.CUMULATIVE format.
    io.opentelemetry.api.OpenTelemetry
    Returns the OpenTelemetrySdk created by this extension.
    List<io.opentelemetry.sdk.trace.data.SpanData>
    Returns all the exported SpanData so far.

    Methods inherited from class org.junit.rules.ExternalResource

    apply

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static OpenTelemetryRule create()
      Returns a OpenTelemetryRule with a default SDK initialized with an in-memory span exporter and W3C trace context propagation.
    • getOpenTelemetry

      public io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
      Returns the OpenTelemetrySdk created by this extension.
    • getSpans

      public List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
      Returns all the exported SpanData so far.
    • getMetrics

      public List<io.opentelemetry.sdk.metrics.data.MetricData> getMetrics()
      Returns the current MetricData in AggregationTemporality.CUMULATIVE format.
      Since:
      1.15.0
    • getLogRecords

      public List<io.opentelemetry.sdk.logs.data.LogRecordData> getLogRecords()
      Returns all the exported LogRecordData so far.
      Since:
      1.32.0
    • clearSpans

      public void clearSpans()
      Clears the collected exported SpanData. Consider making your test smaller instead of manually clearing state using this method.
    • clearMetrics

      public void clearMetrics()
      Clears all registered metric instruments, such that getMetrics() is empty.
      Since:
      1.15.0
    • clearLogRecords

      public void clearLogRecords()
      Clears the collected exported LogRecordData. Consider making your test smaller instead of manually clearing state using this method.
      Since:
      1.32.0
    • before

      protected void before()
      Overrides:
      before in class org.junit.rules.ExternalResource
    • after

      protected void after()
      Overrides:
      after in class org.junit.rules.ExternalResource