Class OpenTelemetryClassRule

java.lang.Object
org.junit.rules.ExternalResource
org.apache.hadoop.hbase.trace.OpenTelemetryClassRule
All Implemented Interfaces:
org.junit.rules.TestRule

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 List spans = otelClassRule.getSpans();
       // make assertions on them
     }
   }
 }
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final io.opentelemetry.sdk.OpenTelemetrySdk
     
    private final io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    OpenTelemetryClassRule(io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry, io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
    protected void
     
    void
    Clears the collected exported SpanData.
     
    io.opentelemetry.api.OpenTelemetry
    Returns the OpenTelemetry created by this Rule.
    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
  • Field Details

    • openTelemetry

      private final io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry
    • spanExporter

      private final io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter
  • Constructor Details

    • OpenTelemetryClassRule

      private OpenTelemetryClassRule(io.opentelemetry.sdk.OpenTelemetrySdk openTelemetry, io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter spanExporter)
  • Method Details

    • create

      public static OpenTelemetryClassRule create()
    • getOpenTelemetry

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

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

      public void clearSpans()
      Clears the collected exported SpanData.
    • before

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

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