8000 Make metrics names compatible with Prometheus Java library by mberndt123 · Pull Request #9740 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make metrics names compatible with Prometheus Java library #9740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core-tests/jvm/src/test/scala/zio/MetricsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object MetricsSpec extends ZIOBaseSpec {
val run =
for {
latch <- Promise.make[Nothing, Unit]
frk = (latch.succeed(()) *> ZIO.never).provideLayer(DefaultJvmMetrics.live.unit)
frk = (latch.succeed(()) *> ZIO.never).provideLayer(DefaultJvmMetrics.liveV2.unit)
_ <- frk.forkDaemon.flatMap(f => latch.await *> f.interrupt *> f.await)
} yield ()

Expand Down
24 changes: 20 additions & 4 deletions core/jvm/src/main/scala/zio/metrics/jvm/DefaultJvmMetrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,33 @@ import zio._
trait DefaultJvmMetrics {
protected def jvmMetricsSchedule: ULayer[JvmMetricsSchedule]

@deprecated(
"This app exposes JVM metrics with the non-OpenMetrics-compliant names used in prometheus client_java 0.16: https://github.com/prometheus/client_java/blob/main/docs/content/migration/simpleclient.md#jvm-metrics. Use `appV2` instead, it uses names compatible with the client_java 1.0 library"
)
lazy val app = withMetrics(live)
lazy val appV2 = withMetrics(liveV2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a name like OpenMetricsApp? v2 doesn't have much meaning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V2 does indeed have a meaning: it means that this is the direct successor to live that you should switch to as soon as you've adapted your monitoring. openMetrics does not convey this. And actually the OpenMetrics project has been archived because it was merged into Prometheus:
https://horovits.medium.com/openmetrics-is-archived-merged-into-prometheus-d555598d2d04
And prometheus would also not be a good name because the previous version was also designed to be compatible with an earlier Prometheus library (the simpleclient library which was replaced by the client_java library).
For these reasons, I think that the V2 name is better overall.


/** A ZIO application that periodically updates the JVM metrics */
lazy val app: ZIOAppDefault = new ZIOAppDefault {
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = live
private def withMetrics(metricsLayer: ZLayer[Any, Any, Any]): ZIOAppDefault = new ZIOAppDefault {
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = metricsLayer
override def run: ZIO[Environment with ZIOAppArgs, Any, Any] = ZIO.unit
}

@deprecated(
"This layer exposes JVM metrics with the non-OpenMetrics-compliant names used in prometheus client_java 0.16: https://github.com/prometheus/client_java/blob/main/docs/content/migration/simpleclient.md#jvm-metrics. Use the `liveV2` layer instead, it uses names compatible with the client_java 1.0 library"
)
lazy val live
: ZLayer[Any, Throwable, Reloadable[BufferPools] with ClassLoading with GarbageCollector with MemoryAllocation with MemoryPools with Standard with Thread with VersionInfo] =
withMemoryPoolsLayer(MemoryPools.live)
lazy val liveV2
: ZLayer[Any, Throwable, Reloadable[BufferPools] with ClassLoading with GarbageCollector with MemoryAllocation with MemoryPools with Standard with Thread with VersionInfo] =
withMemoryPoolsLayer(MemoryPools.liveV2)

/**
* Layer that starts collecting the same JVM metrics as the Prometheus Java
* client's default exporters
*/
lazy val live: ZLayer[
def withMemoryPoolsLayer(memoryPools: ZLayer[JvmMetricsSchedule, Throwable, MemoryPools]): ZLayer[
Any,
Throwable,
Reloadable[BufferPools]
Expand All @@ -36,7 +52,7 @@ trait DefaultJvmMetrics {
ClassLoading.live ++
GarbageCollector.live ++
MemoryAllocation.live ++
Memo 10000 ryPools.live ++
memoryPools ++
Standard.live ++
Thread.live ++
VersionInfo.live)
Expand Down
100 changes: 67 additions & 33 deletions core/jvm/src/main/scala/zio/metrics/jvm/MemoryPools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,42 @@ object MemoryPools {
)
)

val liveV2: ZLayer[JvmMetricsSchedule, Throwable, MemoryPools] = withNames(
jvmMemoryCommittedBytes = "jvm_memory_committed_bytes",
jvmMemoryInitBytes = "jvm_memory_init_bytes",
jvmMemoryMaxBytes = "jvm_memory_max_bytes",
jvmMemoryUsedBytes = "jvm_memory_used_bytes",
jvmMemoryPoolUsedBytes = "jvm_memory_pool_used_bytes",
jvmMemoryPoolCommittedBytes = "jvm_memory_pool_committed_bytes",
jvmMemoryPoolMaxBytes = "jvm_memory_pool_max_bytes",
jvmMemoryPoolInitBytes = "jvm_memory_pool_init_bytes"
)

@deprecated(
"This layer exposes JVM metrics with the non-OpenMetrics-compliant names used in prometheus client_java 0.16: https://github.com/prometheus/client_java/blob/main/docs/content/migration/simpleclient.md#jvm-metrics. Use the `liveV2` layer instead, it uses names compatible with the client_java 1.0 library"
)
val live: ZLayer[JvmMetricsSchedule, Throwable, MemoryPools] = withNames(
jvmMemoryCommittedBytes = "jvm_memory_bytes_committed",
jvmMemoryInitBytes = "jvm_memory_bytes_init",
jvmMemoryMaxBytes = "jvm_memory_bytes_max",
jvmMemoryUsedBytes = "jvm_memory_bytes_used",
jvmMemoryPoolUsedBytes = "jvm_memory_pool_bytes_used",
jvmMemoryPoolCommittedBytes = "jvm_memory_pool_bytes_committed",
jvmMemoryPoolMaxBytes = "jvm_memory_pool_bytes_max",
jvmMemoryPoolInitBytes = "jvm_memory_pool_bytes_init"
)

@nowarn("msg=JavaConverters")
val live: ZLayer[JvmMetricsSchedule, Throwable, MemoryPools] =
private def withNames(
jvmMemoryCommittedBytes: String,
jvmMemoryInitBytes: String,
jvmMemoryMaxBytes: String,
jvmMemoryUsedBytes: String,
jvmMemoryPoolUsedBytes: String,
jvmMemoryPoolCommittedBytes: String,
jvmMemoryPoolMaxBytes: String,
jvmMemoryPoolInitBytes: String
): ZLayer[JvmMetricsSchedule, Throwable, MemoryPools] =
ZLayer.scoped {
for {
memoryMXBean <- ZIO.attempt(ManagementFactory.getMemoryMXBean)
Expand All @@ -94,79 +128,79 @@ object MemoryPools {
.contramap[Int](_.toDouble),
ZIO.attempt(memoryMXBean.getObjectPendingFinalizationCount)
)
memoryBytesUsed = pollingMemoryMetric(
"jvm_memory_bytes_used",
memoryUsedBytes = pollingMemoryMetric(
jvmMemoryUsedBytes,
ZIO.attempt(memoryMXBean.getHeapMemoryUsage.getUsed),
ZIO.attempt(memoryMXBean.getNonHeapMemoryUsage.getUsed)
)
memoryBytesCommitted = pollingMemoryMetric(
"jvm_memory_bytes_committed",
memoryCommittedBytes = pollingMemoryMetric(
jvmMemoryCommittedBytes,
ZIO.attempt(memoryMXBean.getHeapMemoryUsage.getCommitted),
ZIO.attempt(memoryMXBean.getNonHeapMemoryUsage.getCommitted)
)
memoryBytesMax = pollingMemoryMetric(
"jvm_memory_bytes_max",
memoryMaxBytes = pollingMemoryMetric(
jvmMemoryMaxBytes,
ZIO.attempt(memoryMXBean.getHeapMemoryUsage.getMax),
ZIO.attempt(memoryMXBean.getNonHeapMemoryUsage.getMax)
)
memoryBytesInit = pollingMemoryMetric(
"jvm_memory_bytes_init",
memoryInitBytes = pollingMemoryMetric(
jvmMemoryInitBytes,
ZIO.attempt(memoryMXBean.getHeapMemoryUsage.getInit),
ZIO.attempt(memoryMXBean.getNonHeapMemoryUsage.getInit)
)

poolBytesUsed = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
poolUsedBytes = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
pollingPoolMetric(
poolBean,
"jvm_memory_pool_bytes_used",
jvmMemoryPoolUsedBytes,
"jvm_memory_pool_collection_used_bytes",
_.getUsed
)
})
poolBytesCommitted = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
poolCommittedBytes = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
pollingPoolMetric(
poolBean,
"jvm_memory_pool_bytes_committed",
jvmMemoryPoolCommittedBytes,
"jvm_memory_pool_collection_committed_bytes",
_.getCommitted
)
})
poolBytesMax = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
poolMaxBytes = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
pollingPoolMetric(
poolBean,
"jvm_memory_pool_bytes_max",
jvmMemoryPoolMaxBytes,
"jvm_memory_pool_collection_max_bytes",
_.getMax
)
})
poolBytesInit = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
poolInitBytes = PollingMetric.collectAll(poolMXBeans.flatMap { poolBean =>
pollingPoolMetric(
poolBean,
"jvm_memory_pool_bytes_init",
jvmMemoryPoolInitBytes,
"jvm_memory_pool_collection_init_bytes",
_.getInit
)
})

schedule <- ZIO.service[JvmMetricsSchedule]
_ <- objectsPendingFinalization.launch(schedule.updateMetrics)
_ <- memoryBytesUsed.launch(schedule.updateMetrics)
_ <- memoryBytesCommitted.launch(schedule.updateMetrics)
_ <- memoryBytesMax.launch(schedule.updateMetrics)
_ <- memoryBytesInit.launch(schedule.updateMetrics)
_ <- poolBytesUsed.launch(schedule.updateMetrics)
_ <- poolBytesCommitted.launch(schedule.updateMetrics)
_ <- poolBytesMax.launch(schedule.updateMetrics)
_ <- poolBytesInit.launch(schedule.updateMetrics)
_ <- memoryUsedBytes.launch(schedule.updateMetrics)
_ <- memoryCommittedBytes.launch(schedule.updateMetrics)
_ <- memoryMaxBytes.launch(schedule.updateMetrics)
_ <- memoryInitBytes.launch(schedule.updateMetrics)
_ <- poolUsedBytes.launch(schedule.updateMetrics)
_ <- poolCommittedBytes.launch(schedule.updateMetrics)
_ <- poolMaxBytes.launch(schedule.updateMetrics)
_ <- poolInitBytes.launch(schedule.updateMetrics)
} yield MemoryPools(
memoryBytesUsed,
memoryBytesCommitted,
memoryBytesMax,
memoryBytesInit,
poolBytesUsed,
poolBytesCommitted,
poolBytesMax,
poolBytesInit
memoryUsedBytes,
memoryCommittedBytes,
memoryMaxBytes,
memoryInitBytes,
poolUsedBytes,
poolCommittedBytes,
poolMaxBytes,
poolInitBytes
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ object JvmMetricAppExample extends ZIOAppDefault {
ZLayer.succeed(MetricsConfig(5.seconds)),

// Default JVM Metrics
DefaultJvmMetrics.live.unit
DefaultJvmMetrics.liveV2.unit
)
}
Loading
0