Skip to content

fix(service): the Service makes a second server span per request #5003

Done

Description

@dan-petty

Problem Description

The Service makes a second server span for each request, probes included. _service_trace_middleware (src/devops_cli/server/service.py) already skips CONST_SERVICE_PROBE_PATHS and names its spans HTTP <method> <path>. A second source of server spans makes GET /healthz, GET /readyz and GET /metrics (with http.route), and a duplicate POST /webhooks/github next to HTTP POST /webhooks/github at the same rate. This item is rank 11 of the cluster integration plan #4989.

Reproduction Steps

  1. In Tempo, search the roadmap-service spans for 10 min with TraceQL: { resource.service.name = "roadmap-service" && span.kind = server }.
  2. Count the spans by name.

Expected vs Actual Behavior

Expected: one server span for each webhook request, and no span for /healthz, /readyz and /metrics.

Actual (Service 0.117.1, 2026-11-12): /healthz and /readyz 0.047 to 0.10 spans/s each, /metrics 0.007 to 0.017/s, and two server spans for each webhook. Probe spans are about 6 times the webhook spans. They fill the trace search and the Tempo storage (Tempo peaked at 76 % of its 2Gi limit). Loki shows a trace_id on each /readyz and /healthz access line.

Proposed Fix

Find the second source of server spans, remove the duplicate server span, and keep the probe paths out. Prove it with a fail-to-pass test. Do not add a collector filter: it hides the defect (AGENTS.md rule 11), and the gateway makes no probe spans.

Environment Diagnostics

roadmap-service 0.117.1 in the homelab cluster; traces to the cluster OpenTelemetry Collector and Tempo.

Logs, Traces, or Terminal Output

Tempo TraceQL rate, 05:30 to 05:40 UTC 2026-11-12, roadmap-service: /healthz 0.10/s, /readyz 0.10/s, /metrics 0.017/s, /webhooks/github 0.037/s.

Acceptance Criteria

  • A test fails before the fix and passes after it: one request to the app makes one server span, and a probe request makes none.
  • After the deploy, Tempo shows no GET /healthz, GET /readyz or GET /metrics spans from roadmap-service.

Dependencies

None.

Source: #4989

Alternatives check of 2026-11-21: in-use.
The repository already uses this tool.
Tool: opentelemetry-instrumentation-fastapi (https://pypi.org/project/opentelemetry-instrumentation-fastapi), Apache-2.0, last activity 2026-10-29, maintained.
Reason: Searches and package facts show the OpenTelemetry FastAPI instrumentation, already used here, creates one server span per request with http.route and excludes probe URLs; it covers the span source but not deleting the hand-written middleware or the regression test.

Covers:

  • One server span per request with http.route
  • Probe paths dropped through excluded_urls, no collector filter
  • Maintained auto-instrumentation instead of hand-written span code

Lacks:

  • Does not remove the Service's own _service_trace_middleware
  • No fail-to-pass regression test in the repository
  • Server span names lose the HTTP <method> <path> form

Proposed design

Problem statement

The Service creates two server spans for each HTTP request. The hand-written _service_trace_middleware in src/devops_cli/server/service.py creates one span and skips the probe paths. The OpenTelemetry FastAPI instrumentation creates a second span with http.route for the same request, including /healthz, /readyz and /metrics. The duplicate fills the trace search and Tempo storage. The fix keeps the maintained instrumentation, removes the hand-written middleware, sets excluded_urls to the probe paths, and adds a fail-to-pass test. After the deploy, Tempo must show no probe spans from the Service.

Acceptance criteria

Requirement: One server span per request

The Service SHALL create exactly one server span for each HTTP request to a path outside CONST_SERVICE_PROBE_PATHS.

Scenario: A webhook request creates one server span

  • WHEN A test sends one POST request to /webhooks/github to the FastAPI app with an in-memory span exporter.
  • THEN The exporter holds exactly one span whose kind is server.

Requirement: No server span for probe paths

The Service MUST create no server span for an HTTP request to a path in CONST_SERVICE_PROBE_PATHS.

Scenario: A health probe creates no server span

  • WHEN A test sends one GET request to /healthz to the FastAPI app with an in-memory span exporter.
  • THEN The exporter holds no span whose kind is server.

Requirement: One server-span source

The Service SHALL use the OpenTelemetry FastAPI instrumentation as its only source of server spans.

Scenario: The hand-written trace middleware is removed

  • WHEN A test imports the module devops_cli.server.service.
  • THEN The module does not define _service_trace_middleware.

Requirement: Regression test covers the duplicate span

The change MUST add a regression test that detects the duplicate server span.

Scenario: The regression test fails on the unfixed code

  • WHEN A test runs the new regression test against the code before the fix.
  • THEN The regression test fails with two server spans for one request.

Existing Tool Considered

The repository already uses opentelemetry-instrumentation-fastapi (https://pypi.org/project/opentelemetry-instrumentation-fastapi), Apache-2.0, last activity 2026-10-29, maintained. It creates one server span per request with http.route and excludes probe URLs through excluded_urls, so it covers the probe half of the need without a collector filter. It cannot remove the duplicate span source: the Service must remove _service_trace_middleware and add the fail-to-pass test in its own code.

Key questions

  • Which server-span source should the Service keep: the hand-written _service_trace_middleware or the OpenTelemetry FastAPI instrumentation?: Keep the OpenTelemetry FastAPI instrumentation and remove _service_trace_middleware. Set excluded_urls to the probe paths so the instrumentation creates no span for them. (best practice) [sources: src/devops_cli/server/service.py:1071, src/devops_cli/config/constants.py:1813]
    • Rejected: Keep the hand-written middleware and remove the instrumentor. It keeps code that the project builds and maintains itself, while the maintained instrumentation covers the need.
    • Chosen: Keep the OpenTelemetry FastAPI instrumentation and remove the middleware. The maintained instrumentation creates the server span with http.route and excludes probe URLs before any span is created.
  • How should the Service exclude the probe paths from spans?: Set the FastAPI instrumentation's excluded_urls to the comma-separated paths in CONST_SERVICE_PROBE_PATHS. (best practice) [sources: src/devops_cli/config/constants.py:1813]
    • Rejected: Add a collector filter. The issue forbids it because it hides the defect (AGENTS.md rule 11).
    • Chosen: Set excluded_urls on the FastAPI instrumentation. The instrumentation drops the probe request before any span is created, with no collector filter.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/bug
    scope/service
    status/new
    value/medium
    effort/medium
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The searches point at the OpenTelemetry Python FastAPI/ASGI instrumentation as the likely second server-span source: its spans are named GET /healthz / POST /webhooks/github with http.route, exactly the duplicates described. That instrumentation already covers the probe half of the need through excluded_urls / OTEL_PYTHON_FASTAPI_EXCLUDED_URLS, which drops /healthz, /readyz and /metrics before any span is created (no collector filter needed) and yields one server span per request. It cannot, however, remove the duplicate itself: the Service must delete one of its two span sources (the hand-written _service_trace_middleware or the instrumentor) and add the fail-to-pass test in its own code.

    The repository already uses this tool.

    Tool: opentelemetry-instrumentation-fastapi (https://pypi.org/project/opentelemetry-instrumentation-fastapi)

    • License: Apache-2.0
    • Last activity: 2026-10-29
    • Maintained: yes

    Covers:

    • Excludes probe URLs at the instrumentation layer via excluded_urls / OTEL_PYTHON_FASTAPI_EXCLUDED_URLS, so /healthz, /readyz and /metrics make no span
    • Creates exactly one server span per request, with http.route, from the framework instrumentation alone
    • Does the exclusion in-process, without the collector filter the issue forbids

    Lacks:

    • Does not remove the duplicate server span: the Service must drop one of its two span sources (the hand-written _service_trace_middleware or the instrumentor) in its own code
    • Does not identify which of the two sources makes the second span
    • Provides no fail-to-pass test that one request makes one server span and a probe makes none

    Tools considered:

    Searches run:

    • opentelemetry-instrumentation-fastapi duplicate server span custom middleware, results: 5
    • opentelemetry instrument fastapi exclude health check probe endpoints excluded_urls, results: 5
    • devops-cli roadmap-service opentelemetry fastapi server span middleware, results: 5
    • FastAPIInstrumentor two spans per request OpenTelemetryMiddleware double server span issue, results: 5
    • opentelemetry python duplicate spans fastapi instrumentation asgi middleware both instrumented, results: 5
    • github dan-petty devops-cli roadmap service, results: 5
    • "devops_cli" "FastAPI" roadmap service OpenTelemetry, results: 5
    • FastAPIInstrumentor span name "GET /healthz" http.route server span, results: 5
    • devops-cli starlette webhook router service opentelemetry instrumentation, results: 5

    Sources:

    Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.

  6. added
    status/ready
    and removed
    status/new
    on Nov 16, 2026
  7. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 181f85e (see proposed design in issue body).

  8. added
    status/in-progress
    and removed
    status/ready
    on Nov 24, 2026
  9. added
    status/in-review
    and removed
    status/in-progress
    on Nov 24, 2026
  10. removed
    status/in-review
    on Nov 24, 2026
  11. devops-cli-service commented on Nov 24, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-24 18:31 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/servicesource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions