Class 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 OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
     //
     //   private Tracer tracer;
     //
     //   @Before
     //   public void setUp() {
     //       tracer = otelTesting.getOpenTelemetry().getTracer("test");
     //   }
     //
     //   @Test
     //   public 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
      protected void after()  
      protected void before()  
      void clearSpans()
      Clears the collected exported SpanData.
      static OpenTelemetryRule create()
      Returns a OpenTelemetryRule with a default SDK initialized with an in-memory span exporter and W3C trace context propagation.
      io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
      Returns the OpenTelemetrySdk created by this extension.
      java.util.List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
      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 Detail

      • 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 java.util.List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
        Returns all the exported SpanData so far.
      • clearSpans

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

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

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