Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connection attributes to sqlalchemy connect span #1608

Merged
merged 7 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to ## Unreleased ###Added section


### 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