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

use sqlalchemy module name not engine. fixes #922 #1086

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1055](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1055))
- Refactoring custom header collection API for consistency
([#1064](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1064))
- `opentelemetry-instrumentation-sqlalchemy` will correctly report `otel.library.name`
([#1086](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1086))

### Added
- `opentelemetry-instrument` and `opentelemetry-bootstrap` now include a `--version` flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _instrument(self, **kwargs):

if kwargs.get("engine") is not None:
return EngineTracer(
_get_tracer(kwargs.get("engine"), tracer_provider),
_get_tracer(tracer_provider),
kwargs.get("engine"),
kwargs.get("enable_commenter", False),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from sqlalchemy.event import listen # pylint: disable=no-name-in-module

from opentelemetry import trace
from opentelemetry.instrumentation.sqlalchemy.package import (
_instrumenting_module_name,
)
from opentelemetry.instrumentation.sqlalchemy.version import __version__
from opentelemetry.instrumentation.utils import (
_generate_opentelemetry_traceparent,
Expand All @@ -40,9 +43,9 @@ def _normalize_vendor(vendor):
return vendor


def _get_tracer(engine, tracer_provider=None):
def _get_tracer(tracer_provider=None):
return trace.get_tracer(
_normalize_vendor(engine.name),
_instrumenting_module_name,
__version__,
tracer_provider=tracer_provider,
)
Expand All @@ -55,7 +58,7 @@ def _wrap_create_async_engine_internal(func, module, args, kwargs):
object that will listen to SQLAlchemy events.
"""
engine = func(*args, **kwargs)
EngineTracer(_get_tracer(engine, tracer_provider), engine.sync_engine)
EngineTracer(_get_tracer(tracer_provider), engine.sync_engine)
return engine

return _wrap_create_async_engine_internal
Expand All @@ -68,7 +71,7 @@ def _wrap_create_engine_internal(func, module, args, kwargs):
object that will listen to SQLAlchemy events.
"""
engine = func(*args, **kwargs)
EngineTracer(_get_tracer(engine, tracer_provider), engine)
EngineTracer(_get_tracer(tracer_provider), engine)
return engine

return _wrap_create_engine_internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

_instrumenting_module_name = "opentelemetry.instrumentation.sqlalchemy"

_instruments = ("sqlalchemy",)
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ async def run():
self.assertEqual(len(spans), 1)
self.assertEqual(spans[0].name, "SELECT :memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
self.assertEqual(
spans[0].instrumentation_scope.name,
"opentelemetry.instrumentation.sqlalchemy",
)

asyncio.get_event_loop().run_until_complete(run())

Expand Down Expand Up @@ -104,6 +108,10 @@ def test_create_engine_wrapper(self):
self.assertEqual(len(spans), 1)
self.assertEqual(spans[0].name, "SELECT :memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
self.assertEqual(
spans[0].instrumentation_scope.name,
"opentelemetry.instrumentation.sqlalchemy",
)

def test_custom_tracer_provider(self):
provider = TracerProvider(
Expand Down Expand Up @@ -154,6 +162,10 @@ async def run():
self.assertEqual(len(spans), 1)
self.assertEqual(spans[0].name, "SELECT :memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
self.assertEqual(
spans[0].instrumentation_scope.name,
"opentelemetry.instrumentation.sqlalchemy",
)

asyncio.get_event_loop().run_until_complete(run())

Expand Down