JVM Metrics
ZIO has built-in support for collecting JVM Metrics. These metrics are a direct port of the JVM metrics provided by the Prometheus Java Hotspot library and compatible with that library.
There are five categories of JVM metrics. Let's look at them one by one:
- Buffer Pools
jvm_buffer_pool_used_bytesโ Used bytes of a given JVM buffer pool.jvm_buffer_pool_capacity_bytesโ Bytes capacity of a given JVM buffer pool.jvm_buffer_pool_used_buffersโ Used buffers of a given JVM buffer pool.
- Class Loading
jvm_classes_loadedโ The number of classes that are currently loaded in the JVMjvm_classes_loaded_totalโ The total number of classes that have been loaded since the JVM has started executionjvm_classes_unloaded_totalโ The total number of classes that have been unloaded since the JVM has started execution
- Garbage Collector
jvm_gc_collection_seconds_sumโ Time spent in a given JVM garbage collector in seconds.jvm_gc_collection_seconds_count
- Memory Allocation
jvm_memory_pool_allocated_bytes_totalโ Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously.
- Memory Pools
jvm_memory_bytes_usedโ Used bytes of a given JVM memory area.jvm_memory_bytes_committedโ Committed (bytes) of a given JVM memory area.jvm_memory_bytes_maxโ Max (bytes) of a given JVM memory area.jvm_memory_bytes_initโ Initial bytes of a given JVM memory area.jvm_memory_pool_bytes_usedโ Used bytes of a given JVM memory pool.jvm_memory_pool_bytes_committedโ Committed bytes of a given JVM memory pool.jvm_memory_pool_bytes_maxโ Max bytes of a given JVM memory pool.jvm_memory_pool_bytes_initโ Initial bytes of a given JVM memory pool.
- Standard
process_cpu_seconds_totalโ Total user and system CPU time spent in seconds.process_start_time_secondsโ Start time of the process since unix epoch in seconds.process_open_fdsโ Number of open file descriptors.process_max_fdsโ Maximum number of open file descriptors.process_virtual_memory_bytesโ Virtual memory size in bytes.process_resident_memory_bytesโ Resident memory size in bytes.
- Thread
jvm_threads_currentโ Current thread count of a JVM.jvm_threads_daemonโ Daemon thread count of a JVM.jvm_threads_peakโ Peak thread count of a JVM.jvm_threads_started_totalโ Started thread count of a JVM.jvm_threads_deadlockedโ Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers.jvm_threads_deadlocked_monitorโ Cycles of JVM-threads that are in deadlock waiting to acquire object monitors.jvm_threads_stateโ Current count of threads by state.
- Version Info
jvm_infoversionโ java.runtime.versionvendorโ java.vm.vendorruntimeโ java.runtime.name
Collecting Metricsโ
Collecting Inside a ZIO Applicationโ
JVM Metrics are collection of the following ZIO services:
- BufferPools
- ClassLoading
- GarbageCollector
- MemoryAllocation
- MemoryPools
- Standard
- Thread
- VersionInfo
All of these services are available in the zio.metrics.jvm package. Each service has a live implementation that can be used to collect metrics, or we can use all of them at once with by providing DefaultJvmMetrics.live layer to our application.
Collecting as a Sidecar to a ZIO Applicationโ
ZIO JVM metrics have built-in applications that collect the JVM metrics. They can be composed with other ZIO applications as a sidecar. By doing so, we are able to collect JVM metrics without modifying our main ZIO application. They will be executed as a daemon alongside the main app:
package zio.examples.metrics
import zio._
import zio.http._
import zio.metrics.connectors.prometheus.PrometheusPublisher
import zio.metrics.connectors.{MetricsConfig, prometheus}
import zio.metrics.jvm.DefaultJvmMetrics
object JvmMetricAppExample extends ZIOAppDefault {
private val httpApp =
Routes(
Method.GET / "metrics" ->
handler(ZIO.serviceWithZIO[PrometheusPublisher](_.get.map(Response.text)))
)
override def run = Server
.serve(httpApp)
.provide(
// ZIO Http default server layer, default port: 8080
Server.default,
// The prometheus reporting layer
prometheus.prometheusLayer,
prometheus.publisherLayer,
// Interval for polling metrics
ZLayer.succeed(MetricsConfig(5.seconds)),
// Default JVM Metrics
DefaultJvmMetrics.liveV2.unit
)
}