Search code examples
metricsexecutorservicenewrelicthreadpoolexecutorobservability

Create metrics for Java Threadpool (Executors) in Newrelic


I have a threadpool in java implemented via an ScheduledExecutorService. This pool deals with different jobs and I want to have metrics in NewRelic to get some insights into the threadpool utilization per job type (or thread name) among multiple instances of the service.

I specifically want to measure information like:

  • thread count per job type
  • Memory allocation per job type
  • Average execution time per job type

What would be the best way to measure this (in order to build diagrams in NR) using the NR Java agent? Or, is there any observability tool I can integrate to the service (NR compatible) that can help me measure this out of the box? The service is not using any particular Java Framework like Spring.

Thank you in advance for any hints.


Solution

  • The New Relic Java agent does not extract data from ScheduledExecutorService out of the box.

    The agent will capture thread data by default. If your threads are named, you can see the thread count in the "Threads" tab of your APM page. Note that numbers get converted to '#'.

    To get the average execution, you'd have to use the Java agent API.

    Simplest way would be to add the annotation @Trace(dispatcher=true) to each method that process each job type. If the same method processes multiple job types, along with the annotation, you can name the transaction using NewRelic.setTransactionName(jobType.toString()) inside the method.

    Now for memory, that is not easily retrieved within the JVM. Your best bet would be JFR that provides the allocation by thread. The Java agent can capture that information if you turn Real-time profiling on. But note that it has a high allocation overhead, as well as it increases ingestion.