Prometheus JVM metrics

Hi,

Any ideas on how to implement JVM metrics with tapir? So far, I’ve come up with this

object MetricsRouter:
  private val registry = PrometheusRegistry.defaultRegistry

  private val threadDemonCount = Metric[IO, Gauge](
    metric = Gauge.builder().name("jvm_threads_daemon_count").help("...").register(registry),
    onRequest = { (_, gauge, m) =>
      m.unit(
        EndpointMetric().onResponseBody((_, _) => IO(gauge.set(ManagementFactory.getThreadMXBean.getDaemonThreadCount)))
      )
    }
  )

  val Metrics: PrometheusMetrics[IO] =
    PrometheusMetrics
      .default[IO]()
      .addCustom(threadDemonCount)

Many thanks!

I think you’d simply need to do io.prometheus.client.hotspot.DefaultExports.initialize()?

I tried it, but sadly no. This solution works, however I am not sure if it is not suboptimal as I don’t really understand how it works :smile: When is the onResponseBody called?

When the response’s body is completely sent