Skip to content

Commit

Permalink
Add connection attributes to sqlalchemy connect span
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jan 29, 2023
1 parent bbe7578 commit 6603159
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def _wrap_connect(tracer_provider=None):
def _wrap_connect_internal(func, module, args, kwargs):
with tracer.start_as_current_span(
"connect", kind=trace.SpanKind.CLIENT
):
) as span:
attrs, _ = _get_attributes_from_url(module.url)
span.set_attributes(attrs)
return func(*args, **kwargs)

return _wrap_connect_internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider, export
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase


Expand Down Expand Up @@ -159,6 +160,7 @@ def test_create_engine_wrapper(self):
self.assertEqual(len(spans), 2)
# first span - the connection to the db
self.assertEqual(spans[0].name, "connect")
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
# second span - the query
self.assertEqual(spans[1].name, "SELECT :memory:")
Expand Down Expand Up @@ -217,6 +219,7 @@ async def run():
self.assertEqual(len(spans), 2)
# first span - the connection to the db
self.assertEqual(spans[0].name, "connect")
self.assertEqual(spans[0].attributes[SpanAttributes.DB_NAME], ":memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
# second span - the query
self.assertEqual(spans[1].name, "SELECT :memory:")
Expand Down

0 comments on commit 6603159

Please sign in to comment.