Skip to content

Negative http_server_active_requests when collecting ktor metrics #15303

@marychatte

Description

@marychatte

Describe the bug

We are using opentelemetry to collect ktor metrics, and http_server_active_requests in many cases is a big and negative number

Image

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

Expected behavior

http_server_active_requests is always >= 0

Actual behavior

KtorServerTelemetry is done via DefaultHttpServerInstrumenterBuilder and the needed metric is set here, the code for activeRequests looks good with valid +1 on start and -1 on end.
But I also found this bug with PR, which can be connected. The solution was to add .propagateOperationListenersToOnEnd() when InstrumenterBuilder is created

Shouldn't the same be done with DefaultHttpServerInstrumenterBuilder?

public InstrumenterBuilder<REQUEST, RESPONSE> instrumenterBuilder() {
    SpanNameExtractor<? super REQUEST> spanNameExtractor =
        spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

    InstrumenterBuilder<REQUEST, RESPONSE> builder =
        Instrumenter.<REQUEST, RESPONSE>builder(
                openTelemetry, instrumentationName, spanNameExtractor)
            .setSpanStatusExtractor(
                statusExtractorTransformer.apply(HttpSpanStatusExtractor.create(attributesGetter)))
            .addAttributesExtractor(httpAttributesExtractorBuilder.build())
            .addAttributesExtractors(additionalExtractors)
            .addContextCustomizer(httpServerRouteBuilder.build())
            .addOperationMetrics(HttpServerMetrics.get())
            .setSchemaUrl(SchemaUrls.V1_37_0);
    if (emitExperimentalHttpServerTelemetry) {
      builder
          .addAttributesExtractor(HttpExperimentalAttributesExtractor.create(attributesGetter))
          .addOperationMetrics(HttpServerExperimentalMetrics.get());
    }
    InstrumenterUtil.propagateOperationListenersToOnEnd(builder); // here
    builderCustomizer.accept(builder);
    return builder;
  }

Javaagent or library instrumentation version

v3.3.0

Environment

No response

Additional context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageNew issue that requires triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions