Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-7658: do not write unknown HTTP method names to metrics #4551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading