Skip to content

test: adapt FastAPI tests for native OTel spans - #912

Open
CagriYonca wants to merge 2 commits into
mainfrom
update-fastapi-tests
Open

CagriYonca wants to merge 2 commits into
mainfrom
update-fastapi-tests

Conversation

@CagriYonca

@CagriYonca CagriYonca commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

FastAPI 0.115.0 added opentelemetry-api as a dependency and now emits its own spans automatically. Since Instana registers itself as the global OTel TracerProvider, these spans land in Instana's recorder and were breaking all FastAPI tests with unexpected span counts.

Per request, FastAPI now adds:

  • fastapi.dependencies, fastapi.endpoint, fastapi.serialization — parented under the asgi span
  • GET /path — orphan (separate trace, p == 0); FastAPI opens it before Instana's middleware runs so it never inherits the Instana trace context

Changes:

  • Added yield to the _resource fixture (teardown was silently not running)
  • Updated span counts in all tests
  • Added assert_fastapi_otel_spans() helper in tests/helpers.py to avoid repeating the same OTel span assertions across all tests

No production code is changed. The question of whether to suppress FastAPI's native spans at the instrumentation level is tracked separately.

Note: Removed unused variable from sqlalchemy tests based on ruff report.

@CagriYonca CagriYonca self-assigned this Oct 7, 2026
@CagriYonca
CagriYonca requested a review from a team as a code owner October 7, 2026 12:12
@CagriYonca
CagriYonca force-pushed the update-fastapi-tests branch from b69cabc to a256fe1 Compare October 7, 2026 12:15

@pvital pvital left a comment

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.

Few requests.

Comment thread tests/frameworks/test_fastapi.py Outdated
Comment thread tests/frameworks/test_fastapi.py
Comment thread tests/clients/test_sqlalchemy.py
@CagriYonca
CagriYonca force-pushed the update-fastapi-tests branch from a256fe1 to 1f27e81 Compare October 7, 2026 12:36
@CagriYonca
CagriYonca requested a review from pvital October 7, 2026 12:50
Signed-off-by: Cagri Yonca <cagri@ibm.com>
Signed-off-by: Cagri Yonca <cagri@ibm.com>
@CagriYonca
CagriYonca force-pushed the update-fastapi-tests branch from 0b4418f to 78e05f4 Compare October 7, 2026 12:57
def assert_fastapi_otel_spans(
spans: list,
http_route: str,
*,

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 this * here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It forces you to give argument keys for cleaner function calls:

assert_fastapi_otel_spans(spans, "/400", False, 400)  # not valid
assert_fastapi_otel_spans(spans, "/400", expect_serialization=False, http_status_code=400)  # valid

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants