Skip to content

starlette: fix transaction completion with instrumentation disabled - #2778

Merged
xrmx merged 1 commit into
elastic:mainfrom
xrmx:fix-starlette-instrument-false
Nov 16, 2026
Merged

xrmx merged 1 commit into
elastic:mainfrom
xrmx:fix-starlette-instrument-false

Conversation

@xrmx

@xrmx xrmx commented Nov 13, 2026

Copy link
Copy Markdown
Member

What does this pull request do?

Keep the Starlette transaction-ending hook active when explicitly installed APM middleware uses INSTRUMENT=False. Previously, the middleware started transactions but never finished or reported them.

Restrict instrumentation to the lifecycle hook so other library integrations remain disabled. Preserve transaction context through outer error handling and streaming responses.

Related issues

Keep the Starlette transaction-ending hook active when explicitly
installed APM middleware uses INSTRUMENT=False. Previously, the
middleware started transactions but never finished or reported them.

Restrict instrumentation to the lifecycle hook so other library
integrations remain disabled. Preserve transaction context
through outer error handling and streaming responses.
@xrmx
xrmx requested a review from a team as a code owner November 13, 2026 17:17
@github-actions

Copy link
Copy Markdown

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🔍 Preview links for changed docs

@github-actions

Copy link
Copy Markdown

Elastic Docs Style Checker (Vale)

Summary: 1 suggestion found

💡 Suggestions (1): Optional style improvements. Apply when helpful.
File Line Rule Message
docs/reference/configuration.md 751 Elastic.WordChoice Consider using 'deactivated, deselected, hidden, turned off, unavailable' instead of 'disabled', unless the term is in the UI.

The Vale linter checks documentation changes against the Elastic Docs style guide. To use Vale locally or report issues, refer to Elastic style guide for Vale.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the Starlette/FastAPI transaction-lifecycle fix.

No open issues 🎉

What changed in this PR

This PR fixes unfinished HTTP transactions in the Elastic APM Python agent when explicitly installed Starlette/FastAPI middleware uses INSTRUMENT=False.

Changes:

  • Keep the transaction-ending lifecycle hook active while leaving library instrumentation disabled.
  • Add regression tests for successful requests, error handling, streaming responses, and disabled-agent behavior.
  • Document the framework-hook exception to INSTRUMENT=False.
File Description
tests/​contrib/​asyncio/​starlette_instrument_disabled_tests.py Add transaction completion and instrumentation-setting regression tests.
elasticapm/​instrumentation/​control.py Allow instrumentation to be restricted to selected classes.
elasticapm/​contrib/​starlette/​__init__.py Install only the lifecycle hook when library instrumentation is disabled.
docs/​reference/​configuration.md Explain the retained Starlette/FastAPI lifecycle hook.

Review effort: Balanced

@hegerchr hegerchr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

@xrmx
xrmx merged commit d4d09b3 into elastic:main Nov 16, 2026
102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants