AWS | X-Ray ID Generator

The AWS X-Ray ID generator generates trace IDs that are compatible with AWS X-Ray tracing spec. An example of the AWS X-Ray trace and span IDs:

trace_id: 6740a65c2feafac27b679af181018cb7
span_id: 16854f40471a3107

Getting Started

Add settings to the build.sbt:

libraryDependencies ++= Seq(
  "org.typelevel" %%% "otel4s-sdk" % "0.11.1", // <1>
  "org.typelevel" %%% "otel4s-sdk-exporter" % "0.11.1", // <2>
  "org.typelevel" %%% "otel4s-sdk-contrib-aws-xray" % "0.11.1" // <3>
)

Add directives to the *.scala file:

//> using dep "org.typelevel::otel4s-sdk::0.11.1" // <1>
//> using dep "org.typelevel::otel4s-sdk-exporter::0.11.1" // <2>
//> using dep "org.typelevel::otel4s-sdk-contrib-aws-xray::0.11.1" // <3>
  1. Add the otel4s-sdk library
  2. Add the otel4s-sdk-exporter library. Without the exporter, the application will crash
  3. Add the otel4s-sdk-contrib-aws-xray library

Then autoconfigure the SDK:

OpenTelemetrySdk.autoConfigured configures both MeterProvider and TracerProvider:

import cats.effect.{IO, IOApp}
import cats.effect.std.Random
import org.typelevel.otel4s.metrics.MeterProvider
import org.typelevel.otel4s.sdk.OpenTelemetrySdk
import org.typelevel.otel4s.sdk.exporter.otlp.autoconfigure.OtlpExportersAutoConfigure
import org.typelevel.otel4s.sdk.trace.contrib.aws.xray._
import org.typelevel.otel4s.trace.TracerProvider

object TelemetryApp extends IOApp.Simple {

  def run: IO[Unit] = 
    Random.scalaUtilRandom[IO].flatMap { implicit random =>
      OpenTelemetrySdk
        .autoConfigured[IO](
          // register OTLP exporters configurer
          _.addExportersConfigurer(OtlpExportersAutoConfigure[IO])
            // add AWS X-Ray ID generator
            .addTracerProviderCustomizer((b, _) =>
              b.withIdGenerator(AwsXRayIdGenerator[IO])
            )
        )
        .use { autoConfigured =>
          val sdk = autoConfigured.sdk
          program(sdk.meterProvider, sdk.tracerProvider)
        } 
    }

  def program(
      meterProvider: MeterProvider[IO], 
      tracerProvider: TracerProvider[IO]
  ): IO[Unit] =
    ???
}

SdkTraces configures only TracerProvider:

import cats.effect.{IO, IOApp}
import cats.effect.std.Random
import org.typelevel.otel4s.sdk.exporter.otlp.trace.autoconfigure.OtlpSpanExporterAutoConfigure
import org.typelevel.otel4s.sdk.trace.SdkTraces
import org.typelevel.otel4s.sdk.trace.contrib.aws.xray._
import org.typelevel.otel4s.trace.TracerProvider

object TelemetryApp extends IOApp.Simple {

  def run: IO[Unit] =
    Random.scalaUtilRandom[IO].flatMap { implicit random =>
      SdkTraces
        .autoConfigured[IO](
          // register OTLP exporters configurer
          _.addExporterConfigurer(OtlpSpanExporterAutoConfigure[IO])
            // add AWS X-Ray ID generator
            .addTracerProviderCustomizer((b, _) =>
              b.withIdGenerator(AwsXRayIdGenerator[IO])
            )
        )
        .use { autoConfigured =>
          program(autoConfigured.tracerProvider)
        }
    }

  def program(
      tracerProvider: TracerProvider[IO]
  ): IO[Unit] =
    ???
}