Issue
I'm looking to migrate a Java app that exposes metrics by using Prometheus APIs to OpenTelemetry APIs.
The app custom metrics part is okay but with Prometheus API, we used to use DefaultExports.initialize()
which comes from simpleclient_hotspot dependency and generates automatically a bunch of metrics of the JVM itself like the followings:
# HELP jvm_memory_bytes_max Max (bytes) of a given JVM memory area.
# TYPE jvm_memory_bytes_max gauge
jvm_memory_bytes_max{area="heap",} 3.221225472E9
jvm_memory_bytes_max{area="nonheap",} -1.0
# HELP jvm_memory_bytes_init Initial bytes of a given JVM memory area.
# TYPE jvm_memory_bytes_init gauge
jvm_memory_bytes_init{area="heap",} 3.221225472E9
jvm_memory_bytes_init{area="nonheap",} 7667712.0
Is there an equivalent library or a way to accomplish the same with OpenTelemetry?
Solution
The opentelemetry-java-instrumentation
project publishes a library for collecting JVM runtime metrics. Source code lives here, and publishes to maven coordinates io.opentelemetry.instrumentation:opentelemetry-runtime-metrics:<version>
.
To use, create an OpenTelemetrySdk
instance, and initialize the various observers you need:
OpenTelemetry opentelemetry = OpenTelemetrySdk.builder().build();
MemoryPools.registerObservers(opentelemetry);
BufferPools.registerObservers(opentelemetry);
Classes.registerObservers(opentelemetry);
Cpu.registerObservers(opentelemetry);
Threads.registerObservers(opentelemetry);
GarbageCollector.registerObservers(opentelemetry);
Answered By - JackB
Answer Checked By - Pedro (JavaFixing Volunteer)