Skip to main content
The otel4s SDK provides a testkit module for testing pure Scala implementations without relying on the OpenTelemetry Java SDK. This is useful when you want to test your instrumentation in a lightweight manner.

Installation

// Add to build.sbt
libraryDependencies ++= Seq(
  "org.typelevel" %% "otel4s-sdk-testkit" % "0.15.0" % Test
)

Overview

The SDK testkit provides in-memory exporters and readers that allow you to:
  • Capture emitted spans and metrics in memory
  • Assert on the telemetry data structure
  • Test instrumentation without external dependencies
  • Run fast, isolated unit tests

Key Features

1
Framework Agnostic
2
The testkit works with any Scala test framework including MUnit, ScalaTest, Weaver, and others.
3
In-Memory Storage
4
All telemetry data is stored in memory, making tests fast and isolated from external systems.
5
Full Control
6
Access to all telemetry attributes, events, and metadata for comprehensive testing.

Usage Pattern

The general pattern for using the SDK testkit:
  1. Create an in-memory testkit instance
  2. Use the testkit’s tracer or meter provider in your code
  3. Execute your instrumented code
  4. Collect the emitted telemetry data
  5. Assert on the captured data
import cats.effect.IO
import org.typelevel.otel4s.sdk.testkit.trace.TracesTestkit

// Example test structure
def test: IO[Unit] = {
  TracesTestkit.inMemory[IO]().use { testkit =>
    for {
      tracer <- testkit.tracerProvider.get("test-service")
      // Use tracer to instrument code
      _ <- tracer.span("operation").surround(IO.unit)
      // Collect spans
      spans <- testkit.finishedSpans
      // Assert on spans
      _ <- IO(assert(spans.size == 1))
      _ <- IO(assert(spans.head.name == "operation"))
    } yield ()
  }
}

Benefits

  • Fast Tests: No external services or network calls required
  • Deterministic: Consistent results across test runs
  • Isolated: Each test gets its own testkit instance
  • Comprehensive: Access to all telemetry data for detailed assertions

When to Use

Use the SDK testkit when:
  • Testing custom instrumentation logic
  • Verifying span structure and attributes
  • Validating metric values and labels
  • Running CI/CD pipelines without external dependencies
  • Developing instrumentation libraries
For production use cases or testing with the OpenTelemetry Java SDK, use the OtelJava Testkit instead.

Build docs developers (and LLMs) love