-
Notifications
You must be signed in to change notification settings - Fork 626
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 metrics instrumentation sqlalchemy #1645
Merged
srikanthccv
merged 27 commits into
open-telemetry:main
from
shalevr:Metrics-instrumentation-sqlalchemy
Feb 26, 2023
Merged
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f08c3ad
Add metrics instrumentation sqlalchemy
shalevr 28c8c2e
refactored the connection usage
shalevr cecafe1
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr ae56830
Add changelog entry and supports_metrics
shalevr eb5e1dc
Fix lint
shalevr b22f06a
Using semantic convention
shalevr 85e81b0
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
srikanthccv 454229a
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 2e59e58
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 281b933
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr d73eff3
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr a36873d
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 2af13ee
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr b5a9272
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 41a9b5a
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 176e8e6
Update test.yml
shalevr 622f948
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
srikanthccv 3104b4b
Fix Changelog
shalevr 8c03ff9
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 35d1709
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 58532fc
Remove unused code
shalevr 2ad2d06
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr dece77d
work with the metric basic test function from test_base.py
shalevr e9c6954
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 0a48b5a
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 244cde6
Merge branch 'main' into Metrics-instrumentation-sqlalchemy
shalevr 71b234e
Fix merge issue
shalevr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,6 @@ | |
) | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.instrumentation.sqlalchemy.package import ( | ||
_instrumenting_module_name, | ||
) | ||
from opentelemetry.instrumentation.sqlalchemy.version import __version__ | ||
from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment | ||
from opentelemetry.instrumentation.utils import _get_opentelemetry_values | ||
|
@@ -44,49 +41,36 @@ def _normalize_vendor(vendor): | |
return vendor | ||
|
||
|
||
def _get_tracer(tracer_provider=None): | ||
return trace.get_tracer( | ||
_instrumenting_module_name, | ||
__version__, | ||
tracer_provider=tracer_provider, | ||
) | ||
|
||
|
||
def _wrap_create_async_engine(tracer_provider=None, enable_commenter=False): | ||
def _wrap_create_async_engine( | ||
tracer, connections_usage, enable_commenter=False | ||
): | ||
# pylint: disable=unused-argument | ||
def _wrap_create_async_engine_internal(func, module, args, kwargs): | ||
"""Trace the SQLAlchemy engine, creating an `EngineTracer` | ||
object that will listen to SQLAlchemy events. | ||
""" | ||
engine = func(*args, **kwargs) | ||
EngineTracer( | ||
_get_tracer(tracer_provider), engine.sync_engine, enable_commenter | ||
tracer, engine.sync_engine, connections_usage, enable_commenter | ||
) | ||
return engine | ||
|
||
return _wrap_create_async_engine_internal | ||
|
||
|
||
def _wrap_create_engine(tracer_provider=None, enable_commenter=False): | ||
# pylint: disable=unused-argument | ||
def _wrap_create_engine_internal(func, module, args, kwargs): | ||
def _wrap_create_engine(tracer, connections_usage, enable_commenter=False): | ||
def _wrap_create_engine_internal(func, _module, args, kwargs): | ||
"""Trace the SQLAlchemy engine, creating an `EngineTracer` | ||
object that will listen to SQLAlchemy events. | ||
""" | ||
engine = func(*args, **kwargs) | ||
EngineTracer(_get_tracer(tracer_provider), engine, enable_commenter) | ||
EngineTracer(tracer, engine, connections_usage, enable_commenter) | ||
return engine | ||
|
||
return _wrap_create_engine_internal | ||
|
||
|
||
def _wrap_connect(tracer_provider=None): | ||
tracer = trace.get_tracer( | ||
_instrumenting_module_name, | ||
__version__, | ||
tracer_provider=tracer_provider, | ||
) | ||
|
||
def _wrap_connect(tracer): | ||
# pylint: disable=unused-argument | ||
def _wrap_connect_internal(func, module, args, kwargs): | ||
with tracer.start_as_current_span( | ||
|
@@ -107,10 +91,16 @@ class EngineTracer: | |
_remove_event_listener_params = [] | ||
|
||
def __init__( | ||
self, tracer, engine, enable_commenter=False, commenter_options=None | ||
self, | ||
tracer, | ||
engine, | ||
connections_usage, | ||
enable_commenter=False, | ||
commenter_options=None, | ||
): | ||
self.tracer = tracer | ||
self.engine = engine | ||
self.connections_usage = connections_usage | ||
self.vendor = _normalize_vendor(engine.name) | ||
self.enable_commenter = enable_commenter | ||
self.commenter_options = commenter_options if commenter_options else {} | ||
|
@@ -123,6 +113,49 @@ def __init__( | |
engine, "after_cursor_execute", _after_cur_exec | ||
) | ||
self._register_event_listener(engine, "handle_error", _handle_error) | ||
self._register_event_listener(engine, "connect", self._pool_connect) | ||
self._register_event_listener(engine, "close", self._pool_close) | ||
self._register_event_listener(engine, "checkin", self._pool_checkin) | ||
self._register_event_listener(engine, "checkout", self._pool_checkout) | ||
|
||
def _get_pool_name(self): | ||
return self.engine.pool.logging_name or "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The specification didn't write what to do in case the pool_name is not provided, so I kept it empty in this case. |
||
|
||
def _add_idle_to_connection_usage(self, value): | ||
self.connections_usage.add( | ||
value, | ||
attributes={ | ||
"pool.name": self._get_pool_name(), | ||
"state": "idle", | ||
}, | ||
) | ||
|
||
def _add_used_to_connection_usage(self, value): | ||
self.connections_usage.add( | ||
value, | ||
attributes={ | ||
"pool.name": self._get_pool_name(), | ||
"state": "used", | ||
}, | ||
) | ||
|
||
def _pool_connect(self, _dbapi_connection, _connection_record): | ||
self._add_idle_to_connection_usage(1) | ||
|
||
def _pool_close(self, _dbapi_connection, _connection_record): | ||
self._add_idle_to_connection_usage(-1) | ||
|
||
# Called when a connection returns to the pool. | ||
def _pool_checkin(self, _dbapi_connection, _connection_record): | ||
self._add_used_to_connection_usage(-1) | ||
self._add_idle_to_connection_usage(1) | ||
|
||
# Called when a connection is retrieved from the Pool. | ||
def _pool_checkout( | ||
self, _dbapi_connection, _connection_record, _connection_proxy | ||
): | ||
self._add_idle_to_connection_usage(-1) | ||
self._add_used_to_connection_usage(1) | ||
|
||
@classmethod | ||
def _register_event_listener(cls, target, identifier, func, *args, **kw): | ||
|
@@ -153,9 +186,8 @@ def _operation_name(self, db_name, statement): | |
return self.vendor | ||
return " ".join(parts) | ||
|
||
# pylint: disable=unused-argument | ||
def _before_cur_exec( | ||
self, conn, cursor, statement, params, context, executemany | ||
self, conn, cursor, statement, params, context, _executemany | ||
shalevr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
attrs, found = _get_attributes_from_url(conn.engine.url) | ||
if not found: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!