-
Notifications
You must be signed in to change notification settings - Fork 36
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
Christian Kjær Larsen
committed
Apr 1, 2023
1 parent
872534c
commit 3e82e3f
Showing
1 changed file
with
31 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import cats.effect.{IO, IOApp, Resource} | ||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import org.typelevel.otel4s.metrics.ObservableCounter | ||
import org.typelevel.otel4s.java.OtelJava | ||
import java.lang.management.ManagementFactory | ||
import javax.management.{MBeanServer, ObjectName} | ||
|
||
object ObservableExample extends IOApp.Simple { | ||
|
||
val mbeanServer: MBeanServer = ManagementFactory.getPlatformMBeanServer | ||
val mbeanName = new ObjectName("cats.effect.metrics:type=CpuStarvation") | ||
|
||
def meterResource: Resource[IO, ObservableCounter] = | ||
Resource | ||
.eval(IO(GlobalOpenTelemetry.get)) | ||
.evalMap(OtelJava.forAsync[IO]) | ||
.evalMap(_.meterProvider.get("observable-example")) | ||
.flatMap( | ||
_.observableCounter("cats-effect-runtime-cpu-starvation-count") | ||
.withDescription("CE runtime starvation count") | ||
.createWithCallback(obs => | ||
IO( | ||
mbeanServer | ||
.getAttribute(mbeanName, "CpuStarvationCount") | ||
.asInstanceOf[Long] | ||
).flatMap(c => obs.record(c)) | ||
) | ||
) | ||
|
||
def run: IO[Unit] = meterResource.useForever | ||
} |