Skip to content

DB and HTTP semantic convention stability migration for SQLAlchemy#4110

Open
tammy-baylis-swi wants to merge 35 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:sqlalchemy-semconv-opt-in
Open

DB and HTTP semantic convention stability migration for SQLAlchemy#4110
tammy-baylis-swi wants to merge 35 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:sqlalchemy-semconv-opt-in

Conversation

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Jan 21, 2026

Description

Add SQLAlchemy instrumentor support for when OTEL_SEMCONV_STABILITY_OPT_IN includes

  • "database" or "database/dup"
  • "http" or "http/dup"

This changes the SQLComment in span attribute feature if opted in. Inclusion of sqlcomment on query span would be on db.statement and/or db.query.text attribute if default, "database", or "database/dup".

To make the changes cleaner: adds sqlalchemy helper _get_db_name_from_cursor to pull directly instead of having a condition to check attributes taken originally from cursor (see this conversation).

For reusability by other instrumentors later: this PR adds schema url helpers to _semconv.py for choosing the latest appropriate version when an instrumentor uses multiple signal types (http, db, genai).

Adds _set_db_operation to _semconv.py. This adds SQLAlchemy instrumentor setting of db.operation and/or db.operation.name attributes -- this was only done for span name before.

Fixes #2679

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?

  • Added unit tests

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

@tammy-baylis-swi tammy-baylis-swi marked this pull request as ready for review January 22, 2026 23:47
@tammy-baylis-swi tammy-baylis-swi requested a review from a team as a code owner January 22, 2026 23:47
@tammy-baylis-swi tammy-baylis-swi changed the title DB semantic convention stability migration for SQLAlchemy DB and HTTP semantic convention stability migration for SQLAlchemy Jan 26, 2026
@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Jan 26, 2026
Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

Overall looks good 👍🏻

@tammy-baylis-swi tammy-baylis-swi moved this from Ready for review to Approved PRs in Python PR digest Feb 25, 2026
@tammy-baylis-swi
Copy link
Copy Markdown
Contributor Author

@open-telemetry/python-maintainers May I get a review please?

from opentelemetry.trace.status import Status, StatusCode


def _get_db_name_from_cursor_or_conn(vendor, conn, cursor):
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.

Question: why have we started looking also in the connection?

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's more robust to deal with the different DBs that SQLAlchemy can connect to, namely sqlite where connection is more reliable than cursor. It's also a general update from the roundabout way mentioned in this thread. It's a bit extra for a semconv update but might as well since we're changing attributes values anyway.

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.

opentelemetry-instrumentation-sqlalchemy: semantic convention stability migration

4 participants