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: 67288c840fc04d18470d31a5fe436bb2
span_id: 1863f40c25acb289

Getting Started

Add settings to the build.sbt:

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

Add directives to the *.scala file:

//> using lib "org.typelevel::otel4s-sdk::0.11.0" // <1>
//> using lib "org.typelevel::otel4s-sdk-exporter::0.11.0" // <2>
//> using lib "org.typelevel::otel4s-sdk-contrib-aws-xray::0.11.0" // <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] =
    ???
}