Skip to content

starlette: add missing configuration params from ASGI Middleware#4566

Open
Fiery-Fenix wants to merge 9 commits into
open-telemetry:mainfrom
Fiery-Fenix:feat/strarlette_exclude_spans
Open

starlette: add missing configuration params from ASGI Middleware#4566
Fiery-Fenix wants to merge 9 commits into
open-telemetry:mainfrom
Fiery-Fenix:feat/strarlette_exclude_spans

Conversation

@Fiery-Fenix

@Fiery-Fenix Fiery-Fenix commented May 11, 2026

Copy link
Copy Markdown

Description

Unlike FastAPI instrumentation, Starlette manual instrumentation is missing several configuration parameters available in underlying ASGI OpenTelemetryMiddleware.
This PR is adding passthrough of the following OpenTelemetryMiddleware parameters to the Starlette instrument_app function:

  • excluded_urls
  • http_capture_headers_server_request
  • http_capture_headers_server_response
  • http_capture_headers_sanitize_fields
  • exclude_spans

Changes in this PR are mostly port of respective functionality from FastAPI inbstrumentation.
Additional tests were added to high-level coverage of the new configuration parameters.

Partially fixing #3725, at least for manual instrumentation

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Tests has passed for all supported platforms:

for p in "10" "11" "12" "13" "14"; do for v in "oldest" "latest"; do tox -e "py3${p}-test-instrumentation-starlette-${v}"; done; done

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@Fiery-Fenix Fiery-Fenix requested a review from a team as a code owner May 11, 2026 12:44
@emdneto

emdneto commented May 12, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

@Fiery-Fenix Fiery-Fenix force-pushed the feat/strarlette_exclude_spans branch from 47090ed to d9081d4 Compare May 13, 2026 11:38
@Fiery-Fenix Fiery-Fenix force-pushed the feat/strarlette_exclude_spans branch from d9081d4 to 6a36ed4 Compare May 13, 2026 11:41
@Fiery-Fenix

Copy link
Copy Markdown
Author

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

Thanks for note! I have updated PR with correct changelog fragment

@tammy-baylis-swi tammy-baylis-swi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This lgtm!

The typecheck failure is coming from genAI instrumentation, not this code. Continuing to use schema url for 1.11.0 makes sense as Starlette instrumentation hasn't yet migrated to new semconv. I appreciate the tests.

@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest May 14, 2026
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Jun 4, 2026
)
self._is_instrumented_by_opentelemetry = True
# adding apps to set for uninstrumenting
_InstrumentedStarlette._instrumented_starlette_apps.add(self)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we delegate to instrument_app now, this might be a double add?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, you're right - it's a double add, removed.

return _instruments

def _instrument(self, **kwargs: Unpack[InstrumentKwargs]):
def _instrument(self, **kwargs: Any):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are you doing this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Because it's LSP (Liskov Substitution Principle) violation, we shouldn't break a contract defined by a base class.
BaseInstrumentor._instrument defines **kwargs as Any, so I'm simply keeping the contract of the BaseInstrumentor._instrument.
Also, I have checked other instrumentation libraries and found that only 3 out of 49 libraries are using Unpack typing, so I'm assuming that it's not a common approach to use it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, I have checked other instrumentation libraries and found that only 3 out of 49 libraries are using Unpack typing, so I'm assuming that it's not a common approach to use it.

Yes, I added them.

Because it's LSP (Liskov Substitution Principle) violation, we shouldn't break a contract defined by a base class.

Yes, I know, but fixing the _instrument is more work, and it's easier to start making the packages better looking before doing that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see, sound reasonable, I have returned Unpack part and updated it to reflect arguments changes in this PR

@github-actions github-actions Bot removed the Stale label Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

5 participants