Skip to content

Conversation

@marychatte
Copy link
Contributor

@marychatte marychatte commented Nov 25, 2025

Issue Negative http_server_active_requests when collecting ktor metrics

Steps to reproduce:

  1. Define ktor routes which throws exception during or after respond:
get ("/error-during-send") {
    call.respondBytesWriter {
        throw IllegalArgumentException("exception occurred")
    }
}
get ("/error-after-send") {
    call.respondText("Ok")
    throw IllegalArgumentException("exception occurred")
}
  1. Configure KtorServerTelemetry:
install(KtorServerTelemetry) {
    setOpenTelemetry(openTelemetry)
    Experimental.emitExperimentalTelemetry(this)
}
  1. Perform request to any of defined routes, as a result http_server_active_requests = -1

Solution
Ktor’s internal send pipeline (ApplicationSendPipeline) can execute multiple times for a single ApplicationCall, for example, when an exception occurs during sending, then Ktor generates a fallback error response.
The original instrumentation relied on application.sendPipeline.intercept(...), this interceptor is invoked on every send pipeline run, not once per request. As a result, instrumenter.start() is called once (+1 active request), instrumenter.end() is called 1 or 2 times (-1 or -2 active requests), so the counter can become negative.
The idea is to change current interceptor to hook on(ResponseSent). It should be triggered only once, starting from Ktor version 3.3.3 since there was a bug KTOR-9125. And because of this bug I added processedKey attribute to avoid double invocation.

@marychatte marychatte force-pushed the marychatte/fix-negative-active-request-ktor branch from dcb26c0 to c36a597 Compare November 25, 2025 15:15
server.stop(0, 10, TimeUnit.SECONDS)
}

@ParameterizedTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a comment that describes why this is added. Something like

  // regression test for
  // https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/15303
  // verify that active requests are counted correctly when there is a send error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants