How to properly expose hikari metrics to prometheus

2,864 views
Skip to first unread message

andre.pinh...@gmail.com

unread,
Jan 22, 2019, 2:08:51 PM1/22/19
to HikariCP
Hi all.

I'm trying to expose HikariCP metrics to Prometheus. I'm looking for the simplest and best approach to do that. I see here - https://github.com/brettwooldridge/HikariCP/wiki/Dropwizard-Metrics - that Hikari supports Dropwizard, configuring a MetricRegistry but does not explain how to do that. Also, in the source code of Hikari  - https://github.com/brettwooldridge/HikariCP/tree/dev/src/main/java/com/zaxxer/hikari/metrics/prometheus - I found some Java classes that seem to make all the work that it is needed to create a collector and the only thing that needs to be done is expose that collector. But, once again I don't understand how to do it.

So, I'm confused about how to expose the metrics.
 - What is the best approach?
 - How to configure the HikariConfig to do this?
 - how to expose the collector?

I'm using Kotlin, HikariCP 2.7.8 and using Ktor routing - https://ktor.io/servers/features/routing.html - to handling HTTP Requests.

Thanks in advance!

Best Regards,
André

Brett Wooldridge

unread,
Jan 23, 2019, 3:07:25 PM1/23/19
to HikariCP
I am not that familiar with Prometheus, most if not all of that code was contributed.  You can use the setMetricsTrackerFactory method on the config to set the registry with an instance of com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory.

-Brett

andre.pinh...@gmail.com

unread,
Jan 24, 2019, 9:33:04 AM1/24/19
to HikariCP
Hi Brett,

Thanks for your reply.
I will give a try to that.

Br,
André
Message has been deleted

andre.pinh...@gmail.com

unread,
Feb 4, 2019, 10:27:43 AM2/4/19
to HikariCP
Solved. In fact it is a really simple process. Above I leave my documentation of the process.
Thanks for your help Brett! ;)

Br,
André

HikariCP have support to expose metrics for Prometheus thanks to the contribution of the community. This pull request make really easy to expose metrics to prometheus.


There are three simple Steps:

  1. Add prometheus-client dependency - add io.prometheus:simpleclient_servlet:0.6.0
  2. Create a metric controller during the HikariCP configuration;
  3. Expose the controller;

Create metric controller

HikariDataSource(HikariConfig().apply {
                driverClassName = "org.h2.Driver"
                jdbcUrl = "jdbc:h2:mem:test"
                maximumPoolSize = 5
                isAutoCommit = true
                transactionIsolation = "TRANSACTION_REPEATABLE_READ"
                leakDetectionThreshold = 10000
                poolName = "sat"
                metricsTrackerFactory = PrometheusMetricsTrackerFactory()
                validate()
            })

To create the controller you can use the setMetricsTrackerFactory method on the config to set the registry with an instance of com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory. Also it is recommended to give a name for the pool in case if you have different Hikari pools exposing metrics. This will collect the metrics to the the default registry.


Expose the controller

To expose the metrics for prometheus using ktor framework - and using routing feature - just need to create a new handler. This handle will respond with the metrics collected on the default registry. The endpoint "/metrics" the the default for prometheus.

package metrics

import com.proj.common.io.http.ApiContentType
import io.ktor.application.call
import io.ktor.response.respondTextWriter
import io.ktor.routing.Route
import io.ktor.routing.accept
import io.ktor.routing.get
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.common.TextFormat

fun Route.metricsHandler() {
    accept(ApiContentType.v1.test) {
        get("/metrics") {
            call.respondTextWriter{
                TextFormat.write004(this, CollectorRegistry.defaultRegistry.metricFamilySamples())
            }
        }
    }
}

 

Reply all
Reply to author
Forward
0 new messages