Skip to content

Commit

Permalink
KTOR-7658: do not write unknown HTTP method names to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-erofeev committed Dec 13, 2024
1 parent b1e1bd9 commit 10218d1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.ktor.server.metrics.micrometer

import io.ktor.http.HttpMethod.Companion.DefaultMethods
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.application.hooks.Metrics
Expand Down Expand Up @@ -146,19 +147,22 @@ public val MicrometerMetrics: ApplicationPlugin<MicrometerMetricsConfig> =

@OptIn(InternalAPI::class)
on(Metrics) { call ->
active?.incrementAndGet()
call.attributes.put(measureKey, CallMeasure(Timer.start(registry)))
if (call.request.httpMethod in DefaultMethods) {
active?.incrementAndGet()
call.attributes.put(measureKey, CallMeasure(Timer.start(registry)))
}
}

on(ResponseSent) { call ->
active?.decrementAndGet()
val measure = call.attributes[measureKey]
measure.timer.stop(
Timer.builder(metricName)
.addDefaultTags(call, measure.throwable)
.apply { pluginConfig.timerBuilder(this, call, measure.throwable) }
.register(registry)
)
call.attributes.getOrNull(measureKey)?.let { measure ->
active?.decrementAndGet()
measure.timer.stop(
Timer.builder(metricName)
.addDefaultTags(call, measure.throwable)
.apply { pluginConfig.timerBuilder(this, call, measure.throwable) }
.register(registry)
)
}
}

on(CallFailed) { call, cause ->
Expand All @@ -167,7 +171,9 @@ public val MicrometerMetrics: ApplicationPlugin<MicrometerMetricsConfig> =
}

application.monitor.subscribe(RoutingRoot.RoutingCallStarted) { call ->
call.attributes[measureKey].route = call.route.parent.toString()
call.attributes.getOrNull(measureKey)?.let { measure ->
measure.route = call.route.parent.toString()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ class MicrometerMetricsTests {
testRegistry.assertActive(0.0)
}

@Test
fun `time is not measured for custom http requests`() = testApplication {
val testRegistry = SimpleMeterRegistry()
install(MicrometerMetrics) {
registry = testRegistry
}

routing {
get("/uri") {
call.respond("hello")
}
}

var timers = testRegistry.find(requestTimeTimerName).timers()
assertTrue(timers.isEmpty())

client.get("/uri")
timers = testRegistry.find(requestTimeTimerName).timers()
assertEquals(1, timers.size, "Metrics should be recorded for GET requests")

client.request("/uri") {
method = HttpMethod("CUSTOM")
}
timers = testRegistry.find(requestTimeTimerName).timers()
assertEquals(1, timers.size, "Metrics should not be recorded for custom requests")

client.put("/uri")
timers = testRegistry.find(requestTimeTimerName).timers()
assertEquals(2, timers.size, "Metrics should be recorded for PUT requests")
}

@Test
fun `errors are recorded`() = testApplication {
val testRegistry = SimpleMeterRegistry()
Expand Down

0 comments on commit 10218d1

Please sign in to comment.