-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
665 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Metrics | Cats Effect IO runtime | ||
|
||
## Available metrics | ||
|
||
## Getting started | ||
|
||
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-instrumentation-metrics" % "@VERSION@" // <1> | ||
) | ||
``` | ||
|
||
@:choice(scala-cli) | ||
|
||
Add directives to the `*.scala` file: | ||
|
||
```scala | ||
//> using dep "org.typelevel::otel4s-instrumentation-metrics:@VERSION@" // <1> | ||
``` | ||
|
||
@:@ | ||
|
||
1. Add the `otel4s-instrumentation-metrics` library | ||
|
||
## Registering metrics collectors | ||
|
||
@:select(otel-backend) | ||
|
||
@:choice(oteljava) | ||
|
||
```scala mdoc:reset:silent | ||
import cats.effect._ | ||
import org.typelevel.otel4s.instrumentation.ce.IORuntimeMetrics | ||
import org.typelevel.otel4s.metrics.MeterProvider | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
import org.typelevel.otel4s.oteljava.OtelJava | ||
|
||
object Main extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = | ||
OtelJava.autoConfigured[IO]().use { otel4s => | ||
implicit val mp: MeterProvider[IO] = otel4s.meterProvider | ||
IORuntimeMetrics | ||
.register[IO](runtime.metrics, IORuntimeMetrics.Config.default) | ||
.surround { | ||
program(otel4s.meterProvider, otel4s.tracerProvider) | ||
} | ||
} | ||
|
||
@annotation.nowarn("cat=unused") | ||
def program( | ||
meterProvider: MeterProvider[IO], | ||
tracerProvider: TracerProvider[IO] | ||
): IO[Unit] = | ||
IO.unit | ||
|
||
} | ||
``` | ||
|
||
@:choice(sdk) | ||
|
||
```scala mdoc:reset:silent | ||
import cats.effect._ | ||
import org.typelevel.otel4s.instrumentation.ce.IORuntimeMetrics | ||
import org.typelevel.otel4s.metrics.MeterProvider | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
import org.typelevel.otel4s.sdk.OpenTelemetrySdk | ||
|
||
object Main extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = | ||
OpenTelemetrySdk.autoConfigured[IO]().use { autoConfigured => | ||
val sdk = autoConfigured.sdk | ||
implicit val mp: MeterProvider[IO] = sdk.meterProvider | ||
IORuntimeMetrics | ||
.register[IO](runtime.metrics, IORuntimeMetrics.Config.default) | ||
.surround { | ||
program(sdk.meterProvider, sdk.tracerProvider) | ||
} | ||
} | ||
|
||
@annotation.nowarn("cat=unused") | ||
def program( | ||
meterProvider: MeterProvider[IO], | ||
tracerProvider: TracerProvider[IO] | ||
): IO[Unit] = | ||
IO.unit | ||
|
||
} | ||
``` | ||
|
||
@:@ | ||
|
||
|
||
## Customization |
102 changes: 102 additions & 0 deletions
102
...ive/src/main/scala/org/typelevel/otel4s/instrumentation/ce/IORuntimeMetricsPlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.typelevel.otel4s.instrumentation.ce | ||
|
||
import cats.effect.Resource | ||
import cats.effect.Sync | ||
import cats.effect.unsafe.metrics.{IORuntimeMetrics => CatsIORuntimeMetrics} | ||
import cats.syntax.applicative._ | ||
import org.typelevel.otel4s.Attributes | ||
import org.typelevel.otel4s.metrics.MeterProvider | ||
|
||
private[ce] trait IORuntimeMetricsPlatform { | ||
self: IORuntimeMetrics.type => | ||
|
||
sealed trait Config { | ||
def cpuStarvationMetrics: Boolean | ||
def cpuStarvationMetricsAttributes: Attributes | ||
|
||
def withCpuStarvationMetrics: Config | ||
def withoutCpuStarvationMetrics: Config | ||
def withCpuStarvationMetricsAttributes(attributes: Attributes): Config | ||
} | ||
|
||
object Config { | ||
private val Default: Config = Impl( | ||
cpuStarvationMetrics = true, | ||
cpuStarvationMetricsAttributes = Attributes.empty | ||
) | ||
|
||
def default: Config = Default | ||
|
||
private case class Impl( | ||
cpuStarvationMetrics: Boolean, | ||
cpuStarvationMetricsAttributes: Attributes | ||
) extends Config { | ||
def withCpuStarvationMetrics: Config = | ||
copy(cpuStarvationMetrics = true) | ||
|
||
def withoutCpuStarvationMetrics: Config = | ||
copy(cpuStarvationMetrics = false) | ||
|
||
def withCpuStarvationMetricsAttributes(attributes: Attributes): Config = | ||
copy(cpuStarvationMetricsAttributes = attributes) | ||
} | ||
} | ||
|
||
/** Registers the following collectors depending on the `config`: | ||
* - CPU starvation | ||
* | ||
* @example | ||
* {{{ | ||
* object Main extends IOApp.Simple { | ||
* def program( | ||
* meterProvider: MeterProvider[IO], | ||
* tracerProvider: TracerProvider[IO] | ||
* ): IO[Unit] = ??? | ||
* | ||
* def run: IO[Unit] = | ||
* OpenTelemetrySdk.autoConfigured[IO]().use { autoConfigured => | ||
* val sdk = autoConfigured.sdk | ||
* implicit val mp: MeterProvider[IO] = sdk.meterProvider | ||
* | ||
* IORuntimeMetrics | ||
* .register[IO](runtime.metrics, IORuntimeMetrics.Config.default) | ||
* .surround { | ||
* program(sdk.meterProvider, sdk.tracerProvider) | ||
* } | ||
* } | ||
* } | ||
* }}} | ||
* | ||
* =CPU starvation metrics= | ||
* | ||
* Registers the CPU starvation: | ||
* - `cats.effect.runtime.cpu.starvation.count` | ||
* - `cats.effect.runtime.cpu.starvation.clock.drift.current` | ||
* - `cats.effect.runtime.cpu.starvation.clock.drift.max` | ||
* | ||
* To disable CPU starvation metrics, customize a config: | ||
* {{{ | ||
* val config = IORuntimeMetrics.Config.default.withoutCpuStarvationMetrics | ||
* IORuntimeMetrics.register[IO](runtime.metrics, config) | ||
* }}} | ||
* | ||
* To attach attributes to CPU starvation metrics, customize a config: | ||
* {{{ | ||
* val config = IORuntimeMetrics.Config.default.withCpuStarvationMetricsAttributes( | ||
* Attributes(Attribute("key", "value")) | ||
* ) | ||
* IORuntimeMetrics.register[IO](runtime.metrics, config) | ||
* }}} | ||
*/ | ||
def register[F[_]: Sync: MeterProvider]( | ||
metrics: CatsIORuntimeMetrics, | ||
config: Config | ||
): Resource[F, Unit] = | ||
Resource.eval(MeterProvider[F].get(Const.MeterNamespace)).flatMap { implicit meter => | ||
cpuStarvationMetrics( | ||
metrics.cpuStarvation, | ||
config.cpuStarvationMetricsAttributes | ||
).whenA(config.cpuStarvationMetrics) | ||
} | ||
|
||
} |
Oops, something went wrong.