Micrometer Metrics

The MicrometerMetrics feature enables Micrometer metrics in your Ktor server application and allows you to choose the required monitoring system, such as Prometheus, JMX, Elastic, and so on. By default, Ktor exposes metrics for monitoring HTTP requests and a set of low-level metrics for monitoring the JVM. You can customize these metrics or create new ones.

Add Dependencies

To enable MicrometerMetrics, you need to include the following artifacts in the build script:

  • Add the ktor-metrics-micrometer dependency:

    Micrometer Metrics - 图1

    Gradle (Groovy)

    Gradle (Kotlin)

    Maven

    Micrometer Metrics - 图2

    1. implementation "io.ktor:ktor-metrics-micrometer:$ktor_version"
  • Add a dependency required for a monitoring system. The example below shows how to add an artifact for Prometeus:

    Micrometer Metrics - 图3

    Gradle (Groovy)

    Gradle (Kotlin)

    Maven

    Micrometer Metrics - 图4

    1. implementation "io.micrometer:micrometer-registry-prometheus:$prometeus_version"

Install MicrometerMetrics

To install the MicrometerMetrics feature, pass it to the install function in the application initialization code. This can be the main function …

  1. import io.ktor.features.*
  2. // ...
  3. fun Application.main() {
  4. install(MicrometerMetrics)
  5. // ...
  6. }

… or a specified module:

  1. import io.ktor.features.*
  2. // ...
  3. fun Application.module() {
  4. install(MicrometerMetrics)
  5. // ...
  6. }

Exposed Metrics

Ktor exposes the following metrics for monitoring HTTP requests:

  • ktor.http.server.requests.active: a gauge that counts the amount of concurrent HTTP requests. This metric doesn’t provide any tags.

  • ktor.http.server.requests: a timer for measuring the time of each request. This metric provides a set of tags for monitoring request data, including address for a requested URL, method for an HTTP method, route for a Ktor route handling requests, and so on.

tip

Micrometer Metrics - 图5

The metric names may be different depending on the configured monitoring system.

In addition to HTTP metrics, Ktor exposes a set of metrics for monitoring the JVM.

Create a Registry

After installing MicrometerMetrics, you need to create a registry for your monitoring system and assign it to the registry property. In the example below, the PrometheusMeterRegistry is created outside the install block to have the capability to reuse this registry in different route handlers:

  1. import io.ktor.features.*
  2. // ...
  3. fun Application.module() {
  4. val appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
  5. install(MicrometerMetrics) {
  6. registry = appMicrometerRegistry
  7. }
  8. }

Configure Metrics

The MicrometerMetrics feature provides various configuration options that can be accessed using MicrometerMetrics.Configuration.

Timers

To customize tags for each timer, you can use the timers function that is called for each request:

  1. install(MicrometerMetrics) {
  2. // ...
  3. timers { call, exception ->
  4. tag("region", call.request.headers["regionId"])
  5. }
  6. }

Distribution Statistics

You configure distribution statistics using the distributionStatisticConfig property, for example:

  1. install(MicrometerMetrics) {
  2. // ...
  3. distributionStatisticConfig = DistributionStatisticConfig.Builder()
  4. .percentilesHistogram(true)
  5. .maximumExpectedValue(Duration.ofSeconds(20).toNanos())
  6. .sla(
  7. Duration.ofMillis(100).toNanos(),
  8. Duration.ofMillis(500).toNanos()
  9. )
  10. .build()
  11. }

JVM and System Metrics

In addition to HTTP metrics, Ktor exposes a set of metrics for monitoring the JVM. You can customize a list of these metrics using the meterBinders property, for example:

  1. install(MicrometerMetrics) {
  2. // ...
  3. meterBinders = listOf(
  4. JvmMemoryMetrics(),
  5. JvmGcMetrics(),
  6. ProcessorMetrics()
  7. )
  8. }

You can also assign an empty list to disable these metrics at all.

Prometheus: Expose a Scrape Endpoint

If you use Prometheus as a monitoring system, you need to expose an HTTP endpoint to the Prometheus scraper. In Ktor, you can do this in the following way:

  1. Create a dedicated route that accepts GET requests by the required address (/metrics in the example below).

  2. Use call.respond to send scraping data to Prometheus.

  1. fun Application.module() {
  2. val appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
  3. install(MicrometerMetrics) {
  4. registry = appMicrometerRegistry
  5. // ...
  6. }
  7. routing {
  8. get("/metrics") {
  9. call.respond(appMicrometerRegistry.scrape())
  10. }
  11. }
  12. }