All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zio.metrics.jvm.GarbageCollector.scala Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package zio.metrics.jvm

import zio._
import zio.metrics._

import java.lang.management.ManagementFactory
import scala.annotation.nowarn
import scala.collection.JavaConverters._

final case class GarbageCollector(
  gcCollectionSecondsSum: PollingMetric[Any, Throwable, Chunk[MetricState.Gauge]],
  gcCollectionSecondsCount: PollingMetric[Any, Throwable, Chunk[MetricState.Gauge]]
)

object GarbageCollector {
  @nowarn("msg=JavaConverters")
  val live: ZLayer[JvmMetricsSchedule, Throwable, GarbageCollector] =
    ZLayer.scoped {
      for {
        gcMXBeans <- ZIO.attempt(ManagementFactory.getGarbageCollectorMXBeans.asScala)
        gcCollectionSecondsSum = PollingMetric.collectAll(gcMXBeans.map { gc =>
                                   PollingMetric(
                                     Metric
                                       .gauge("jvm_gc_collection_seconds_sum")
                                       .tagged("gc", gc.getName)
                                       .contramap((ms: Long) => ms.toDouble / 1000.0),
                                     ZIO.attempt(gc.getCollectionTime)
                                   )
                                 })
        gcCollectionSecondsCount = PollingMetric.collectAll(gcMXBeans.map { gc =>
                                     PollingMetric(
                                       Metric
                                         .gauge("jvm_gc_collection_seconds_count")
                                         .tagged("gc", gc.getName)
                                         .contramap[Long](_.toDouble),
                                       ZIO.attempt(gc.getCollectionCount)
                                     )
                                   })

        schedule <- ZIO.service[JvmMetricsSchedule]
        _        <- gcCollectionSecondsSum.launch(schedule.updateMetrics)
        _        <- gcCollectionSecondsCount.launch(schedule.updateMetrics)
      } yield GarbageCollector(gcCollectionSecondsSum, gcCollectionSecondsCount)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy