How can I better monitor my Dropwizard threads/request queue via a Metric registry?

177 Views Asked by At

So I have this dropwizard application with all of the standard config yaml stuff like:

server:
  maxQueuedRequests: 256
  maxThreads: 256

I'd like to instrument this thread info. I.e. I'd like to know how many requests are queued and how many threads are dedicated to serving requests. Are these metrics automatically pushed or do I need to addd some custom metrics? If I need to add some custom metrics, how do I get access to the thread pool and the request queue?

BTW, I'm using DW 1.3.18.

Thanks!

1

There are 1 best solutions below

0
Kramer On

I figured out a good approach. The thread pool is available in org.eclipse.jetty.server.Server and you can get that from io.dropwizard.setup.Environment in Kotlin like so:

fun Environment.registerThreadPoolMetrics() =
    this.lifecycle().addServerLifecycleListener { server ->
        this.metrics().registerThreadPoolMetrics(
            server.threadPool as QueuedThreadPool,
            "jettyThreadPool"
        )
    }

fun MetricRegistry.registerThreadPoolMetrics(threadPool: QueuedThreadPool, threadPoolName: String) {
    registerGauge(QueuedThreadPool::class.java, threadPoolName, "is_low_on_threads", threadPool.isLowOnThreads)
    registerGauge(QueuedThreadPool::class.java, threadPoolName, "pool_size", threadPool.threads)
    registerGauge(QueuedThreadPool::class.java, threadPoolName, "idle_count", threadPool.idleThreads)
    registerGauge(QueuedThreadPool::class.java, threadPoolName, "active_count", threadPool.busyThreads)
    registerGauge(QueuedThreadPool::class.java, threadPoolName, "queue_size", threadPool.queueSize)
}

Note that the ThreadPool in Jetty isn't necessarily a QueuedThread pool, but in DropWizard it is-hence the cast. I hope that helps anyone else with this quandary.