Skip to content

Commit 6603159

Browse files
Add connection attributes to sqlalchemy connect span
1 parent bbe7578 commit 6603159

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ 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+
attrs, _ = _get_attributes_from_url(module.url)
96+
span.set_attributes(attrs)
9597
return func(*args, **kwargs)
9698

9799
return _wrap_connect_internal

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

+3
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

@@ -159,6 +160,7 @@ def test_create_engine_wrapper(self):
159160
self.assertEqual(len(spans), 2)
160161
# first span - the connection to the db
161162
self.assertEqual(spans[0].name, "connect")
163+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
162164
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
163165
# second span - the query
164166
self.assertEqual(spans[1].name, "SELECT :memory:")
@@ -217,6 +219,7 @@ async def run():
217219
self.assertEqual(len(spans), 2)
218220
# first span - the connection to the db
219221
self.assertEqual(spans[0].name, "connect")
222+
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
220223
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
221224
# second span - the query
222225
self.assertEqual(spans[1].name, "SELECT :memory:")

0 commit comments

Comments
 (0)