Metrics | JVM Runtime

See the semantic conventions for JVM metrics.

Java 8 and newer

The OpenTelemetry runtime-telemetry-java8 module provides the JVM runtime metrics for Java 8 and newer. The module uses JMX to produce metrics.

Add the following configuration to the favorite build tool:

Add settings to the build.sbt:

libraryDependencies ++= Seq(
  "org.typelevel" %% "otel4s-oteljava" % "0.11.1", // <1>
  "io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java8" % "2.5.0-alpha" // <2>
)

Add directives to the *.scala file:

//> using dep "org.typelevel::otel4s-oteljava:0.11.1" // <1>
//> using dep "io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java8:2.5.0-alpha" // <2>
  1. Add the otel4s-oteljava library
  2. Add the OpenTelemetry runtime metrics library

The producers can be registered manually:

import cats.effect.{IO, IOApp, Resource, Sync}
import cats.syntax.flatMap._
import cats.syntax.functor._
import io.opentelemetry.api.{OpenTelemetry => JOpenTelemetry}
import io.opentelemetry.instrumentation.runtimemetrics.java8._
import org.typelevel.otel4s.oteljava.OtelJava
import scala.jdk.CollectionConverters._

object Service extends IOApp.Simple {
  
  def run: IO[Unit] =
    OtelJava
      .autoConfigured[IO]()
      .flatTap(otel4s => registerRuntimeMetrics(otel4s.underlying))
      .use { otel4s =>
        val _ = otel4s
        ???
      }
  
  private def registerRuntimeMetrics[F[_]: Sync](
      openTelemetry: JOpenTelemetry
  ): Resource[F, Unit] = {
    val acquire = Sync[F].delay {
      List
        .newBuilder[AutoCloseable]
        .addAll(MemoryPools.registerObservers(openTelemetry).asScala)
        .addAll(Classes.registerObservers(openTelemetry).asScala)
        .addAll(Cpu.registerObservers(openTelemetry).asScala)
        .addAll(Threads.registerObservers(openTelemetry).asScala)
        .addAll(GarbageCollector.registerObservers(openTelemetry).asScala)
        .result()
    }
  
    Resource.make(acquire)(r => Sync[F].delay(r.foreach(_.close()))).void
  }

}

Java 17 and newer

The OpenTelemetry runtime-telemetry-java17 module provides JVM runtime metrics for Java 17 and newer. The module uses JMX and JFR to produce metrics.

Add the following configuration to the favorite build tool:

Add settings to the build.sbt:

libraryDependencies ++= Seq(
  "org.typelevel" %% "otel4s-oteljava" % "0.11.1", // <1>
  "io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java17" % "2.5.0-alpha" // <2>
)

Add directives to the *.scala file:

//> using dep "org.typelevel::otel4s-oteljava:0.11.1" // <1>
//> using dep "io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java17:2.5.0-alpha" // <2>
  1. Add the otel4s-oteljava library
  2. Add the OpenTelemetry runtime metrics library

The producers can be registered manually:

import cats.effect.{IO, IOApp, Resource, Sync}
import cats.syntax.flatMap._
import cats.syntax.functor._
import io.opentelemetry.api.{OpenTelemetry => JOpenTelemetry}
import io.opentelemetry.instrumentation.runtimemetrics.java17._
import org.typelevel.otel4s.oteljava.OtelJava

object Service extends IOApp.Simple {
  
  def run: IO[Unit] = 
    OtelJava
      .autoConfigured[IO]()
      .flatTap(otel4s => registerRuntimeMetrics(otel4s.underlying))
      .use { otel4s =>
        val _ = otel4s
        ???
      }

  private def registerRuntimeMetrics[F[_]: Sync](
      openTelemetry: JOpenTelemetry
  ): Resource[F, Unit] = {
    val acquire = Sync[F].delay(RuntimeMetrics.create(openTelemetry))

    Resource.make(acquire)(r => Sync[F].delay(r.close())).void
  }

}