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 Feb 15, 2023
1 parent 1e89854 commit 1c6a24c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1592](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1592))
- `opentelemetry-instrumentation-django` Allow explicit `excluded_urls` configuration through `instrument()`
([#1618](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1618))
- Add connection attributes to sqlalchemy connect span
([#1608](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1608))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ 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:
if span.is_recording():
attrs, _ = _get_attributes_from_url(module.url)
span.set_attributes(attrs)
span.set_attribute(SpanAttributes.DB_SYSTEM, _normalize_vendor(module.name))
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 @@ -128,11 +129,12 @@ async def run():
def test_not_recording(self):
mock_tracer = mock.Mock()
mock_span = mock.Mock()
mock_context = mock.Mock()
mock_span.is_recording.return_value = False
mock_span.__enter__ = mock.Mock(return_value=(mock.Mock(), None))
mock_span.__exit__ = mock.Mock(return_value=None)
mock_tracer.start_span.return_value = mock_span
mock_tracer.start_as_current_span.return_value = mock_span
mock_context.__enter__ = mock.Mock(return_value=mock_span)
mock_context.__exit__ = mock.Mock(return_value=None)
mock_tracer.start_span.return_value = mock_context
mock_tracer.start_as_current_span.return_value = mock_context
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
engine = create_engine("sqlite:///:memory:")
Expand All @@ -159,6 +161,8 @@ 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].attributes[SpanAttributes.DB_SYSTEM], "sqlite")
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 +221,8 @@ 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].attributes[SpanAttributes.DB_SYSTEM], "sqlite")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
# second span - the query
self.assertEqual(spans[1].name, "SELECT :memory:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_engine_execute_errors(self):
spans = self.memory_exporter.get_finished_spans()
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
self.check_meta(spans[0])
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT opentelemetry-tests")
Expand Down Expand Up @@ -99,6 +100,7 @@ def test_orm_insert(self):
spans = self.memory_exporter.get_finished_spans()
# connect, identity insert on before the insert, insert, and identity insert off after the insert
self.assertEqual(len(spans), 4)
self.check_meta(spans[0])
span = spans[2]
self._check_span(span, "INSERT")
self.assertIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def test_engine_execute_errors(self):
spans = self.memory_exporter.get_finished_spans()
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
self.check_meta(spans[0])
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT opentelemetry-tests")
Expand Down

0 comments on commit 1c6a24c

Please sign in to comment.