micrometer-metrics / micrometer

An application observability facade for the most popular observability tools. Think SLF4J, but for observability.
https://micrometer.io
Apache License 2.0
4.39k stars 966 forks source link

Expose ForkJoinPool::getParallelism as a metric #5236

Open genuss opened 3 days ago

genuss commented 3 days ago

Hello, contributors,

I recently switched to using virtual threads, so became a big user of ForkJoinPool. When I looked at metrics, exposed by micrometer, I saw that parallelism isn't exposed. Is there a reason for this? Well, if not, I believe this could be a nice addition to already exposed ones.

I hesitated to add [ForkJoinPool::getPoolSize](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ForkJoinPool.html#getPoolSize()), as I'm not sure this one is important, but I could add it too. What do you think about it?

jonatan-ivanov commented 2 days ago

I saw that parallelism isn't exposed. Is there a reason for this?

I think no one asked for it so far. What is your use-case behind parallelism? Are you setting it explicitly? By default, it uses the value of Runtime.getRuntime().availableProcessors() (cpu count) which Micrometer already reports but if you set it explicitly, it can have other values.

I think based on the use-case, the pool size can also be useful since its value will be the same as parallelism if enough tasks are submitted but if not, it can be lower, please feel free to add a Gauge for that too.

I also wanted to ask you to write some tests but then I realized there are no tests for ForkJoinPool in ExecutorServiceMetricsTest. :( Let me know if you want to add some, otherwise I will take a look at them.

/cc @shakuzen

genuss commented 5 hours ago

I think no one asked for it so far. What is your use-case behind parallelism? Are you setting it explicitly? By default, it uses the value of Runtime.getRuntime().availableProcessors() (cpu count) which Micrometer already reports but if you set it explicitly, it can have other values.

Well, I don't do it now, but I might in future as in container environment JVM's heuristics don't always work correct. Anyway, it's possible to create a new ForkJoinPool apart from the common one. In this case availableProcessors won't work too.

I also wanted to ask you to write some tests but then I realized there are no tests for ForkJoinPool in ExecutorServiceMetricsTest. :(

I added a simple one. Please tell me if you need more.

genuss commented 4 hours ago

The failed test is this one:

JvmGcMetricsTest > gcTimingIsCorrectForPauseCycleCollectors() FAILED
    org.opentest4j.AssertionFailedError: 
    expected: 10L
     but was: 9L
        at app//io.micrometer.core.instrument.binder.jvm.JvmGcMetricsTest.lambda$checkPhaseCount$3(JvmGcMetricsTest.java:236)

6 tests completed, 1 failed

> Task :micrometer-core:zgcGenerationalTest FAILED

Doesn't look like a related failure, does it?