Skip to content

Commit d22bf39

Browse files
DB and HTTP semantic convention stability migration for SQLAlchemy (#4110)
* Add _semconv set_db fns * lint * Changelog * semconv opt-in for sqlalchemy instr * use imports not hardcode * Update comment on comment-in-attr * Add tests * Changelog * Add migration status * Sqlalchemy http,database opt-in * meter schema_url * Add sqlalchemy _get_db_name_from_cursor * Add per-signal semconv opt-in schema url precedence; add sqlalchemy usage * Update _get_db_name coverage * More db_name_from_cursor cases * split to lint * _get_schema parses url from semconv * _get_* from cursor or conn * Add _semconv set_db_operation, used by sqlalchemy * get_db_name safer, update docker-test * Fix test * Add schema_url asserts * Update CHANGELOG.md --------- Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 6bd19c0 commit d22bf39

9 files changed

Lines changed: 988 additions & 44 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
([#4212](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4212))
2222
- `opentelemetry-instrumentation-botocore`: Add support for instrumenting `aiobotocore`
2323
([#4049](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4049))
24+
- `opentelemetry-instrumentation-sqlalchemy`: implement new semantic convention opt-in migration
25+
([#4110](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4110))
2426

2527
### Fixed
2628

instrumentation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
| [opentelemetry-instrumentation-redis](./opentelemetry-instrumentation-redis) | redis >= 2.6 | No | development
4242
| [opentelemetry-instrumentation-remoulade](./opentelemetry-instrumentation-remoulade) | remoulade >= 0.50 | No | development
4343
| [opentelemetry-instrumentation-requests](./opentelemetry-instrumentation-requests) | requests ~= 2.0 | Yes | migration
44-
| [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy >= 1.0.0, < 2.1.0 | Yes | development
44+
| [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy >= 1.0.0, < 2.1.0 | Yes | migration
4545
| [opentelemetry-instrumentation-sqlite3](./opentelemetry-instrumentation-sqlite3) | sqlite3 | No | development
4646
| [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette >= 0.13 | Yes | development
4747
| [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No | development

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,24 @@
110110
SQLComment in span attribute
111111
****************************
112112
If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in
113-
the query span ``db.statement`` attribute for your needs. If ``commenter_options``
114-
have been set, the span attribute comment will also be configured by this
115-
setting.
113+
the query span ``db.statement`` and/or ``db.query.text`` attribute for your
114+
needs. If ``commenter_options`` have been set, the span attribute comment
115+
will also be configured by this setting.
116116
117117
.. code:: python
118118
119119
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
120120
121121
# Opts into sqlcomment for SQLAlchemy trace integration.
122-
# Opts into sqlcomment for `db.statement` span attribute.
122+
# Opts into sqlcomment for `db.statement` and/or `db.query.text` span attribute.
123123
SQLAlchemyInstrumentor().instrument(
124124
enable_commenter=True,
125125
commenter_options={},
126126
enable_attribute_commenter=True,
127127
)
128128
129129
Warning:
130-
Capture of sqlcomment in ``db.statement`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans <https://opentelemetry.io/docs/specs/semconv/database/database-spans/#generating-a-summary-of-the-query-text>`_ for more information.
130+
Capture of sqlcomment in ``db.statement``/``db.query.text`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans <https://opentelemetry.io/docs/specs/semconv/database/database-spans/#generating-a-summary-of-the-query-text>`_ for more information.
131131
132132
API
133133
---
@@ -141,6 +141,11 @@
141141
from sqlalchemy.engine.base import Engine
142142
from wrapt import wrap_function_wrapper as _w
143143

144+
from opentelemetry.instrumentation._semconv import (
145+
_get_schema_url_for_signal_types,
146+
_OpenTelemetrySemanticConventionStability,
147+
_OpenTelemetryStabilitySignalType,
148+
)
144149
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
145150
from opentelemetry.instrumentation.sqlalchemy.engine import (
146151
EngineTracer,
@@ -181,20 +186,32 @@ def _instrument(self, **kwargs):
181186
Returns:
182187
An instrumented engine if passed in as an argument or list of instrumented engines, None otherwise.
183188
"""
189+
# Initialize semantic conventions opt-in if needed
190+
_OpenTelemetrySemanticConventionStability._initialize()
191+
192+
# Determine schema URL based on both DATABASE and HTTP signal types
193+
# and semconv opt-in mode
194+
schema_url = _get_schema_url_for_signal_types(
195+
[
196+
_OpenTelemetryStabilitySignalType.DATABASE,
197+
_OpenTelemetryStabilitySignalType.HTTP,
198+
]
199+
)
200+
184201
tracer_provider = kwargs.get("tracer_provider")
185202
tracer = get_tracer(
186203
__name__,
187204
__version__,
188205
tracer_provider,
189-
schema_url="https://opentelemetry.io/schemas/1.11.0",
206+
schema_url=schema_url,
190207
)
191208

192209
meter_provider = kwargs.get("meter_provider")
193210
meter = get_meter(
194211
__name__,
195212
__version__,
196213
meter_provider,
197-
schema_url="https://opentelemetry.io/schemas/1.11.0",
214+
schema_url=schema_url,
198215
)
199216

200217
connections_usage = meter.create_up_down_counter(

0 commit comments

Comments
 (0)