Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add Metrics | JVM Runtime doc to the instrumentation section #582

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ val MUnitDisciplineVersion = "2.0.0-M3"
val MUnitScalaCheckEffectVersion = "2.0.0-M2"
val OpenTelemetryVersion = "1.36.0"
val OpenTelemetryInstrumentationVersion = "2.2.0"
val OpenTelemetryInstrumentationAlphaVersion = "2.2.0-alpha"
val OpenTelemetrySemConvVersion = "1.24.0-alpha"
val OpenTelemetryProtoVersion = "1.1.0-alpha"
val PekkoStreamVersion = "1.0.2"
Expand Down Expand Up @@ -583,10 +584,13 @@ lazy val docs = project
libraryDependencies ++= Seq(
"org.apache.pekko" %% "pekko-http" % PekkoHttpVersion,
"io.opentelemetry" % "opentelemetry-sdk-extension-autoconfigure" % OpenTelemetryVersion,
"io.opentelemetry.instrumentation" % "opentelemetry-instrumentation-annotations" % OpenTelemetryInstrumentationVersion
"io.opentelemetry.instrumentation" % "opentelemetry-instrumentation-annotations" % OpenTelemetryInstrumentationVersion,
"io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java8" % OpenTelemetryInstrumentationAlphaVersion,
"io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java17" % OpenTelemetryInstrumentationAlphaVersion
),
mdocVariables ++= Map(
"OPEN_TELEMETRY_VERSION" -> OpenTelemetryVersion
"OPEN_TELEMETRY_VERSION" -> OpenTelemetryVersion,
"OPEN_TELEMETRY_INSTRUMENTATION_ALPHA_VERSION" -> OpenTelemetryInstrumentationAlphaVersion
),
laikaConfig := {
import laika.config.{ChoiceConfig, Selections, SelectionConfig}
Expand Down
1 change: 1 addition & 0 deletions docs/instrumentation/directory.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ laika.title = Instrumentation

laika.navigationOrder = [
metrics.md
metrics-jvm-runtime.md
tracing.md
tracing-java-interop.md
]
149 changes: 149 additions & 0 deletions docs/instrumentation/metrics-jvm-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Metrics | JVM Runtime

See the [semantic conventions][semantic-conventions] for JVM metrics.

## Java 8 and newer

The OpenTelemetry [runtime-telemetry-java8][otel-jvm-metrics-8] 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:

@:select(build-tool)

@:choice(sbt)

Add settings to the `build.sbt`:

```scala
libraryDependencies ++= Seq(
"org.typelevel" %% "otel4s-oteljava" % "@VERSION@", // <1>
"io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java8" % "@OPEN_TELEMETRY_INSTRUMENTATION_ALPHA_VERSION@" // <2>
)
```

@:choice(scala-cli)

Add directives to the `*.scala` file:

```scala
//> using lib "org.typelevel::otel4s-oteljava:@VERSION@" // <1>
//> using lib "io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java8:@OPEN_TELEMETRY_INSTRUMENTATION_ALPHA_VERSION@" // <2>
```

@:@

1. Add the `otel4s-oteljava` library
2. Add the OpenTelemetry [runtime metrics][otel-jvm-metrics-8] library

The producers can be registered manually:

```scala mdoc:silent
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][otel-jvm-metrics-17] 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:

@:select(build-tool)

@:choice(sbt)

Add settings to the `build.sbt`:

```scala
libraryDependencies ++= Seq(
"org.typelevel" %% "otel4s-oteljava" % "@VERSION@", // <1>
"io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java17" % "@OPEN_TELEMETRY_INSTRUMENTATION_ALPHA_VERSION@" // <2>
)
```

@:choice(scala-cli)

Add directives to the `*.scala` file:

```scala
//> using lib "org.typelevel::otel4s-oteljava:@VERSION@" // <1>
//> using lib "io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java17:@OPEN_TELEMETRY_INSTRUMENTATION_ALPHA_VERSION@" // <2>
```

@:@

1. Add the `otel4s-oteljava` library
2. Add the OpenTelemetry [runtime metrics][otel-jvm-metrics-17] library

The producers can be registered manually:

```scala mdoc:silent:reset
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
}

}
```

[semantic-conventions]: https://opentelemetry.io/docs/specs/semconv/runtime/jvm-metrics
[otel-jvm-metrics-8]: https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/runtime-telemetry/runtime-telemetry-java8/library
[otel-jvm-metrics-17]: https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/runtime-telemetry/runtime-telemetry-java17/library