如何通过 Metric 注册表更好地监控我的 Dropwizard 线程/请求队列?

问题描述 投票:0回答:1

所以我有这个 dropwizard 应用程序,其中包含所有标准配置 yaml 内容,例如:

server:
  maxQueuedRequests: 256
  maxThreads: 256

我想检测此线程信息。 IE。我想知道有多少请求排队以及有多少线程专用于服务请求。这些指标是自动推送的还是我需要添加一些自定义指标?如果我需要添加一些自定义指标,如何访问线程池和请求队列?

顺便说一句,我正在使用 DW 1.3.18。

谢谢!

dropwizard
1个回答
0
投票

我想出了一个好方法。线程池在 org.eclipse.jetty.server.Server 中可用,您可以从 Kotlin 中的 io.dropwizard.setup.Environment 获取它,如下所示:

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)
}

请注意,Jetty 中的 ThreadPool 不一定是 QueuedThread 池,但在 DropWizard 中却是——因此进行强制转换。我希望这能帮助其他人解决这个难题。

© www.soinside.com 2019 - 2024. All rights reserved.