Metrics with Dropwizard metrics

The Metrics feature allows you to configure the Metricsto get useful information about the server and incoming requests.

This feature is defined in the class io.ktor.metrics.DropwizardMetrics in the artifact io.ktor:ktor-metrics:$ktor_version.

dependencies { implementation "io.ktor:ktor-metrics:$ktor_version"}

dependencies { implementation("io.ktor:ktor-metrics:$ktor_version")}

<project> … <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-metrics</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies></project>

Installing

The Metrics feature exposes a registry property, that can be used to build and startmetric reporters.

JMX Reporter

The JMX Reporter allows you to expose all the metrics to JMX,allowing you to view those metrics with jconsole or jvisualvm with the MBeans plugin.

  1. install(DropwizardMetrics) {
  2. JmxReporter.forRegistry(registry)
  3. .convertRatesTo(TimeUnit.SECONDS)
  4. .convertDurationsTo(TimeUnit.MILLISECONDS)
  5. .build()
  6. .start()
  7. }

Ktor Metrics: JMX

SLF4J Reporter

The SLF4J Reporter allows you to periodically emit reports to any output supported by SLF4J.For example, to output the metrics every 10 seconds, you would:

  1. install(DropwizardMetrics) {
  2. Slf4jReporter.forRegistry(registry)
  3. .outputTo(log)
  4. .convertRatesTo(TimeUnit.SECONDS)
  5. .convertDurationsTo(TimeUnit.MILLISECONDS)
  6. .build()
  7. .start(10, TimeUnit.SECONDS)
  8. }

Ktor Metrics: SLF4J

Other reporters

You can use any of the available Metric reporters.

Exposed reports

This feature exposes many JVM properties relating to memory usage and thread behavior.

Global:

Specifically to Ktor, it exposes:

  • ktor.calls.active:Count - The number of unfinished active requests
  • ktor.calls.duration - Information about the duration of the calls
  • ktor.calls.exceptions - Information about the number of exceptions
  • ktor.calls.status.NNN - Information about the number of times that happened a specific HTTP Status Code NNN

Per endpoint:

  • "/uri(method:VERB).NNN" - Information about the number of times that happened a specific HTTP Status Code NNN, for this path, for this verb
  • "/uri(method:VERB).meter" - Information about the number of calls for this path, for this verb
  • "/uri(method:VERB).timer" - Information about the durations for this endpoint

Information per report

Durations

"/uri(method:VERB).timer" and ktor.calls.duration are durations and expose:

  • 50thPercentile
  • 75thPercentile
  • 95thPercentile
  • 98thPercentile
  • 99thPercentile
  • 999thPercentile
  • Count
  • DurationUnit
  • OneMinuteRate
  • FifteenMinuteRate
  • FiveMinuteRate
  • Max
  • Mean
  • MeanRate
  • Min
  • RateUnit
  • StdDev

Counts

The other properties are exposed as counts:

  • Count
  • FifteenMinuteRate
  • FiveMinuteRate
  • OneMinuteRate
  • MeanRate
  • RateUnit