The Metrics feature allows you to configure the Metrics to get useful information about the server and incoming requests.
io.ktor:ktor-metrics:$ktor_version
中的
io.ktor.metrics.DropwizardMetrics
类中定义。
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>
The Metrics feature exposes a registry
property, that can be used to build and start
metric reporters.
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.
install(DropwizardMetrics) {
JmxReporter.forRegistry(registry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build()
.start()
}
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:
install(DropwizardMetrics) {
Slf4jReporter.forRegistry(registry)
.outputTo(log)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build()
.start(10, TimeUnit.SECONDS)
}
You can use any of the available Metric reporters.
This feature exposes many JVM properties relating to memory usage and thread behavior.
Specifically to Ktor, it exposes:
ktor.calls.active
:Count
- The number of unfinished active requestsktor.calls.duration
- Information about the duration of the callsktor.calls.exceptions
- Information about the number of exceptionsktor.calls.status.NNN
- Information about the number of times that happened a specific HTTP Status Code NNN"/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"/uri(method:VERB).timer"
and ktor.calls.duration
are durations and expose:
The other properties are exposed as counts: