Skip to content

fix(pymssql): Complete semconv stability migration for attributes set in wrapped_connection#4592

Merged
xrmx merged 6 commits into
open-telemetry:mainfrom
luke6Lh43:fix/pymssql-semconv-stability
May 18, 2026
Merged

fix(pymssql): Complete semconv stability migration for attributes set in wrapped_connection#4592
xrmx merged 6 commits into
open-telemetry:mainfrom
luke6Lh43:fix/pymssql-semconv-stability

Conversation

@luke6Lh43

@luke6Lh43 luke6Lh43 commented May 18, 2026

Copy link
Copy Markdown
Member

fix(pymssql): Complete semconv stability migration for attributes set in wrapped_connection

Description

This PR fixes the incomplete semantic convention stability (OTEL_SEMCONV_STABILITY_OPT_IN) implementation in opentelemetry-instrumentation-pymssql.

Problem:
When PR #4109 migrated the base dbapi instrumentation to support stable semantic conventions, pymssql's custom wrapped_connection() method was not updated. This method directly sets net.peer.name, net.peer.port, and db.user using hardcoded string keys because pymssql's connection object doesn't expose connection attributes through the standard path.

As a result, even with OTEL_SEMCONV_STABILITY_OPT_IN=database,http, spans still emit legacy attribute names (net.peer.name, net.peer.port, db.user) instead of their stable equivalents (server.address, server.port, db.client.user).

Fix:
Replace the hardcoded attribute assignments with the semconv-aware helper functions:

  • self.span_attributes["db.user"] = user_set_db_user(...)
  • self.span_attributes["net.peer.name"] = host_set_http_net_peer_name_client(...)
  • self.span_attributes["net.peer.port"] = port_set_http_peer_port_client(...)

These helpers respect the configured opt-in mode and emit old, new, or both attribute keys accordingly.

Changes

instrumentation/opentelemetry-instrumentation-pymssql/src/opentelemetry/instrumentation/pymssql/__init__.py

  • Import _set_db_user, _set_http_net_peer_name_client, _set_http_peer_port_client from opentelemetry.instrumentation.dbapi
  • Replace hardcoded attribute assignments with helper function calls

instrumentation/opentelemetry-instrumentation-pymssql/tests/test_pymssql_integration.py

  • Add use_semconv_opt_in() context manager for opt-in testing
  • Fix net.peer.port assertions: "1433" (string) → 1433 (int), since _set_http_peer_port_client uses set_int_attribute
  • Add test_semconv_stable: verifies that database,http opt-in emits only stable attributes (server.address, server.port, db.client.user)
  • Add test_semconv_dup: verifies that database/dup,http/dup opt-in emits both old and new attribute keys

Testing

All 10 tests pass:

tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_pymssql_setup ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_custom_tracer_provider ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_instrumentor ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_callproc ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_executemany ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_execute ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_execute_no_connection_args ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_execute_with_connection_args ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_semconv_stable ...
tests/test_pymssql_integration.py::TestPyMSSQLIntegration::test_semconv_dup ...

Related Issues

Fixes #4591

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

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

The pymssql instrumentation's wrapped_connection() was setting
net.peer.name, net.peer.port, and db.user using hardcoded legacy
attribute keys, bypassing the semconv stability helpers introduced
in open-telemetry#4109.

Replace hardcoded keys with _set_db_user, _set_http_net_peer_name_client,
and _set_http_peer_port_client so that OTEL_SEMCONV_STABILITY_OPT_IN
correctly emits stable attributes (server.address, server.port, etc.).

Also updates existing test assertions for net.peer.port from string
to int, matching the behavior of set_int_attribute used by the helper.
@xrmx xrmx moved this to Ready for review in Python PR digest May 18, 2026

@xrmx xrmx 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 looks good to me, please add a fragment for the changelog that also list the change in the type of net.peer.port

@github-project-automation github-project-automation Bot moved this from Ready for review to Approved PRs in Python PR digest May 18, 2026
@xrmx xrmx moved this from Approved PRs to Ready for review in Python PR digest May 18, 2026
@xrmx

xrmx commented May 18, 2026

Copy link
Copy Markdown
Contributor

@luke6Lh43 Please also fix lint and run tox -e precommit to fix the formatting

luke6Lh43 and others added 3 commits May 18, 2026 06:51
…st_pymssql_integration.py

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
…st_pymssql_integration.py

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
@luke6Lh43 luke6Lh43 requested a review from a team as a code owner May 18, 2026 11:00
@github-project-automation github-project-automation Bot moved this from Ready for review to Approved PRs in Python PR digest May 18, 2026
@xrmx xrmx merged commit a0c3251 into open-telemetry:main May 18, 2026
753 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest May 18, 2026
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.

opentelemetry-instrumentation-pymssql: OTEL_SEMCONV_STABILITY_OPT_IN partially ineffective for host/port/user attributes

3 participants