Skip to content

Commit cb52774

Browse files
Add connection attributes to sqlalchemy connect span
1 parent 3f8fdf2 commit cb52774

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
([#1592](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1592))
2727
- `opentelemetry-instrumentation-django` Allow explicit `excluded_urls` configuration through `instrument()`
2828
([#1618](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1618))
29+
- Add connection attributes to sqlalchemy connect span
30+
([#1608](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1608))
2931

3032
### Fixed
3133

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ def _wrap_connect(tracer_provider=None):
9191
def _wrap_connect_internal(func, module, args, kwargs):
9292
with tracer.start_as_current_span(
9393
"connect", kind=trace.SpanKind.CLIENT
94-
):
94+
) as span:
95+
if span.is_recording():
96+
attrs, _ = _get_attributes_from_url(module.url)
97+
span.set_attributes(attrs)
98+
span.set_attribute(SpanAttributes.DB_SYSTEM, _normalize_vendor(module.name))
9599
return func(*args, **kwargs)
96100

97101
return _wrap_connect_internal

instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
2323
from opentelemetry.sdk.resources import Resource
2424
from opentelemetry.sdk.trace import TracerProvider, export
25+
from opentelemetry.semconv.trace import SpanAttributes
2526
from opentelemetry.test.test_base import TestBase
2627

2728

@@ -128,11 +129,12 @@ async def run():
128129
def test_not_recording(self):
129130
mock_tracer = mock.Mock()
130131
mock_span = mock.Mock()
132+
mock_context = mock.Mock()
131133
mock_span.is_recording.return_value = False
132-
mock_span.__enter__ = mock.Mock(return_value=(mock.Mock(), None))
133-
mock_span.__exit__ = mock.Mock(return_value=None)
134-
mock_tracer.start_span.return_value = mock_span
135-
mock_tracer.start_as_current_span.return_value = mock_span
134+
mock_context.__enter__ = mock.Mock(return_value=mock_span)
135+
mock_context.__exit__ = mock.Mock(return_value=None)
136+
mock_tracer.start_span.return_value = mock_context
137+
mock_tracer.start_as_current_span.return_value = mock_context
136138
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
137139
tracer.return_value = mock_tracer
138140
engine = create_engine("sqlite:///:memory:")
@@ -159,6 +161,8 @@ def test_create_engine_wrapper(self):
159161
self.assertEqual(len(spans), 2)
160162
# first span - the connection to the db
161163
self.assertEqual(spans[0].name, "connect")
164+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
165+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_SYSTEM], "sqlite")
162166
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
163167
# second span - the query
164168
self.assertEqual(spans[1].name, "SELECT :memory:")
@@ -217,6 +221,8 @@ async def run():
217221
self.assertEqual(len(spans), 2)
218222
# first span - the connection to the db
219223
self.assertEqual(spans[0].name, "connect")
224+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
225+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_SYSTEM], "sqlite")
220226
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
221227
# second span - the query
222228
self.assertEqual(spans[1].name, "SELECT :memory:")

tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_mssql.py

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_engine_execute_errors(self):
7171
spans = self.memory_exporter.get_finished_spans()
7272
# one span for the connection and one for the query
7373
self.assertEqual(len(spans), 2)
74+
self.check_meta(spans[0])
7475
span = spans[1]
7576
# span fields
7677
self.assertEqual(span.name, "SELECT opentelemetry-tests")
@@ -99,6 +100,7 @@ def test_orm_insert(self):
99100
spans = self.memory_exporter.get_finished_spans()
100101
# connect, identity insert on before the insert, insert, and identity insert off after the insert
101102
self.assertEqual(len(spans), 4)
103+
self.check_meta(spans[0])
102104
span = spans[2]
103105
self._check_span(span, "INSERT")
104106
self.assertIn(

tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_mysql.py

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def test_engine_execute_errors(self):
7070
spans = self.memory_exporter.get_finished_spans()
7171
# one span for the connection and one for the query
7272
self.assertEqual(len(spans), 2)
73+
self.check_meta(spans[0])
7374
span = spans[1]
7475
# span fields
7576
self.assertEqual(span.name, "SELECT opentelemetry-tests")

tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_postgres.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_engine_execute_errors(self):
6464
spans = self.memory_exporter.get_finished_spans()
6565
# one span for the connection and one for the query
6666
self.assertEqual(len(spans), 2)
67+
self.check_meta(spans[0])
6768
span = spans[1]
6869
# span fields
6970
self.assertEqual(span.name, "SELECT opentelemetry-tests")

0 commit comments

Comments
 (0)