Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
  • 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.
31 changes: 31 additions & 0 deletions examples/src/main/scala/ObservableExample.scala
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
}

0 comments on commit 3e82e3f

Please sign in to comment.