Skip to content

feat(asgi): respect suppress_http_instrumentation in ASGI middleware#4375

Merged
xrmx merged 5 commits into
open-telemetry:mainfrom
liustve:asgi-suppress-http-spans
Apr 3, 2026
Merged

feat(asgi): respect suppress_http_instrumentation in ASGI middleware#4375
xrmx merged 5 commits into
open-telemetry:mainfrom
liustve:asgi-suppress-http-spans

Conversation

@liustve

@liustve liustve commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Description

ASGI OpenTelemetryMiddleware should respect the suppress_http_instrumentation context and skip server span creation when HTTP instrumentation is suppressed.

parities the same feature in aiohttp-server: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py#L446

What changed:

  • Added is_http_instrumentation_enabled() check in OpenTelemetryMiddleware.call
  • Added test_suppress_http_instrumentation test validating zero spans are emitted when suppression is active

Fixes #4376

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit testing

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • 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

@liustve liustve requested a review from a team as a code owner March 30, 2026 04:38
Add is_http_instrumentation_enabled() check in OpenTelemetryMiddleware.__call__
so ASGI server spans are skipped when HTTP instrumentation is suppressed.

Every other HTTP instrumentor (client and server) already honors this flag.
ASGI was the only one that did not.

@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.

Lgtm! Other approvers will have a look

@tammy-baylis-swi tammy-baylis-swi requested a review from a team March 30, 2026 19:39
@tammy-baylis-swi tammy-baylis-swi moved this to Approved PRs in Python PR digest Mar 30, 2026
@liustve

liustve commented Mar 30, 2026

Copy link
Copy Markdown
Contributor Author

Awesome, thanks! @tammy-baylis-swi

liustve added a commit to aws-observability/aws-otel-python-instrumentation that referenced this pull request Mar 30, 2026
*Description of changes:*

- Suppress redundant HTTP/ASGI spans on MCP client and server by default

- Patch `OpenTelemetryMiddleware.__call__` to respect
`suppress_http_instrumentation` context
note: SHOULD remove this patch once
open-telemetry/opentelemetry-python-contrib#4375
is merged

- Skip creating MCP spans for initialize and notifications/initialized
handshake messages

- Introduce `OTEL_MCP_SUPPRESS_HTTP_INSTRUMENTATION` env var, defaults
to true


<img width="1038" height="546" alt="image"
src="https://github.com/user-attachments/assets/d6f0254a-3b4f-4d46-bb32-424d6fee12b3"
/>

 


By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
@xrmx xrmx enabled auto-merge (squash) April 3, 2026 08:54
@xrmx xrmx merged commit 5430c75 into open-telemetry:main Apr 3, 2026
867 of 868 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest Apr 3, 2026
liustve added a commit to aws-observability/aws-otel-python-instrumentation that referenced this pull request Jun 17, 2026
Automated update of OpenTelemetry dependencies.

**Build Status:** ❌
[failure](https://github.com/aws-observability/aws-otel-python-instrumentation/actions/runs/27184408784)

**Updated versions:**
- [OpenTelemetry
Python](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.42.1):
1.42.1
- [OpenTelemetry
Contrib](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.63b1):
0.63b1
-
[opentelemetry-sdk-extension-aws](https://pypi.org/project/opentelemetry-sdk-extension-aws/2.1.0/):
2.1.0
-
[opentelemetry-propagator-aws-xray](https://pypi.org/project/opentelemetry-propagator-aws-xray/1.0.2/):
1.0.2

**Upstream releases with breaking changes:**
Note: the mechanism to detect upstream breaking changes is not perfect.
Be sure to check all new releases and understand if any additional
changes need to be addressed.


**opentelemetry-python-contrib:**
- [Version
1.41.0/0.62b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.62b0)

*Description of changes:*

- Drops the `opentelemetry-instrumentation-boto` runtime dependency
(legacy boto2 instrumentation), removed entirely upstream in:
open-telemetry/opentelemetry-python-contrib#4303

- Drops Python 3.9 support to match upstream's new minimum (Python
≥3.10): open-telemetry/opentelemetry-python#5076
(core) and
open-telemetry/opentelemetry-python-contrib#4412
(contrib)

- Switches `from importlib_metadata import version` to stdlib `from
importlib.metadata import version` after upstream dropped the
third-party `importlib-metadata` package from `opentelemetry-api`:
open-telemetry/opentelemetry-python#3234

- Refactors the botocore patches (`_botocore_patches.py`) to use the new
`ExtensionRegistry` API. Upstream split `_KNOWN_EXTENSIONS` into
`_BOTOCORE_EXTENSIONS` + `_AIOBOTOCORE_EXTENSIONS`, removed
`_find_extension`, and moved `_safe_invoke` to
`opentelemetry.instrumentation.botocore.utils` as part of adding
aiobotocore instrumentation:
open-telemetry/opentelemetry-python-contrib#4049

- Removes the Starlette ASGI `suppress_http_instrumentation` patch
(which the existing ADOT TODO already flagged) since the ASGI middleware
now natively honors `suppress_http_instrumentation` upstream:
open-telemetry/opentelemetry-python-contrib#4375

- Adapts the configurator tests to upstream's renamed
`BaggageSpanProcessor._baggage_key_predicate` → `_predicates` (now a
list of callables, after the constructor was reworked to accept either a
single predicate or a sequence):
open-telemetry/opentelemetry-python-contrib#4371

- Adapts `test_otlp_attribute_vs_body_types` to upstream's
`_VALID_ANY_VALUE_TYPES` switching `typing.Mapping`/`typing.Sequence` to
`collections.abc.Mapping`/`collections.abc.Sequence`:
open-telemetry/opentelemetry-python#5133

- Bumps the mysqlclient contract-test pin from `2.2.4` to `2.2.8` and
updates the matching `db.name` assertion. The 0.62b0 dbapi
semantic-convention migration
(open-telemetry/opentelemetry-python-contrib#4109)
added an empty-string filter inside `_set_db_name`, which exposed a
latent gap: mysqlclient < 2.2.7 didn't expose a Python-level `conn.db`
attribute (added upstream in
PyMySQL/mysqlclient#752, shipped in mysqlclient
2.2.7), so OTel's dbapi instrumentation defaulted `database` to `""` and
the empty value used to land on the span unconditionally. After 0.62b0
the empty value is dropped, so the assertion `db.name == ""` now fails
with "key not found". Bumping mysqlclient lets `conn.db` flow through as
the actual database name (`testdb`) so the assertion becomes meaningful.


By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Steve Liu <liustve@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ASGI OpenTelemetryMiddleware does not respect suppress_http_instrumentation

3 participants