-
Notifications
You must be signed in to change notification settings - Fork 932
Support enable_commenter for instrument_connection by psycopg(2 #3071
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
Changes from 19 commits
d2da8cd
4783f3a
bf49c8c
e148c9f
58daeb6
8e8b759
e3d7941
a94a591
0e24d6d
da529bd
197e5ea
b3ae9c8
64941f9
e6ca8fe
84d82d0
65ac19a
1ca0cd0
afff465
fd11f1a
90051b7
3587846
a738d5e
eeab531
e3db4a8
1526aaa
e081502
48c8a9f
5e81f06
cdfc2e5
458d297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by | ||
| using ``PsycopgInstrumentor``. | ||
|
|
||
| .. _Psycopg: http://initd.org/psycopg/ | ||
| .. _Psycopg: https://www.psycopg.org/psycopg3/docs/ | ||
|
|
||
| SQLCOMMENTER | ||
| ***************************************** | ||
|
|
@@ -150,6 +150,7 @@ | |
| ) | ||
| from psycopg.sql import Composed # pylint: disable=no-name-in-module | ||
|
|
||
| from opentelemetry import trace as trace_api | ||
| from opentelemetry.instrumentation import dbapi | ||
| from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
| from opentelemetry.instrumentation.psycopg.package import _instruments | ||
|
|
@@ -174,7 +175,7 @@ def instrumentation_dependencies(self) -> Collection[str]: | |
|
|
||
| def _instrument(self, **kwargs): | ||
| """Integrate with PostgreSQL Psycopg library. | ||
| Psycopg: http://initd.org/psycopg/ | ||
| Psycopg: https://www.psycopg.org/psycopg3/docs/ | ||
| """ | ||
| tracer_provider = kwargs.get("tracer_provider") | ||
| enable_sqlcommenter = kwargs.get("enable_commenter", False) | ||
|
|
@@ -237,15 +238,24 @@ def _uninstrument(self, **kwargs): | |
|
|
||
| # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql | ||
| @staticmethod | ||
| def instrument_connection(connection, tracer_provider=None): | ||
| """Enable instrumentation in a psycopg connection. | ||
| def instrument_connection( | ||
| connection: psycopg.Connection, | ||
| tracer_provider: typing.Optional[trace_api.TracerProvider] = None, | ||
| enable_commenter: bool = False, | ||
| commenter_options: dict = None, | ||
| ): | ||
|
Member
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. Should we keep the old return type of
Contributor
Author
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. Yes! I've done this in other PR (#4267) |
||
| """Enable instrumentation of a Psycopg connection. | ||
|
|
||
| Args: | ||
| connection: psycopg.Connection | ||
| The psycopg connection object to be instrumented. | ||
| tracer_provider: opentelemetry.trace.TracerProvider, optional | ||
| The TracerProvider to use for instrumentation. If not provided, | ||
| the global TracerProvider will be used. | ||
| enable_commenter: bool, optional | ||
| Optional flag to enable/disable sqlcommenter (default False). | ||
| commenter_options: dict, optional | ||
| Optional configurations for tags to be appended at the sql query. | ||
|
|
||
| Returns: | ||
| An instrumented psycopg connection object. | ||
|
|
@@ -258,7 +268,9 @@ def instrument_connection(connection, tracer_provider=None): | |
| connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory | ||
| ) | ||
| connection.cursor_factory = _new_cursor_factory( | ||
| tracer_provider=tracer_provider | ||
| tracer_provider=tracer_provider, | ||
| enable_commenter=enable_commenter, | ||
| commenter_options=commenter_options, | ||
| ) | ||
| connection._is_instrumented_by_opentelemetry = True | ||
| else: | ||
|
|
@@ -342,14 +354,23 @@ def get_statement(self, cursor, args): | |
| return statement | ||
|
|
||
|
|
||
| def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): | ||
| def _new_cursor_factory( | ||
| db_api: dbapi.DatabaseApiIntegration = None, | ||
| base_factory: pg_cursor = None, | ||
| tracer_provider: typing.Optional[trace_api.TracerProvider] = None, | ||
| enable_commenter: bool = False, | ||
| commenter_options: dict = None, | ||
| ): | ||
| if not db_api: | ||
| db_api = DatabaseApiIntegration( | ||
| __name__, | ||
| PsycopgInstrumentor._DATABASE_SYSTEM, | ||
| connection_attributes=PsycopgInstrumentor._CONNECTION_ATTRIBUTES, | ||
| version=__version__, | ||
| tracer_provider=tracer_provider, | ||
| enable_commenter=enable_commenter, | ||
| commenter_options=commenter_options, | ||
| connect_module=psycopg, | ||
| ) | ||
|
|
||
| base_factory = base_factory or pg_cursor | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,10 @@ | |
| # limitations under the License. | ||
|
|
||
| """ | ||
| The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by | ||
| The integration with PostgreSQL supports the `Psycopg2`_ library, it can be enabled by | ||
| using ``Psycopg2Instrumentor``. | ||
|
|
||
| .. _Psycopg: http://initd.org/psycopg/ | ||
| .. _Psycopg2: https://www.psycopg.org/docs/ | ||
|
|
||
| SQLCOMMENTER | ||
| ***************************************** | ||
|
|
@@ -142,11 +142,15 @@ | |
| from typing import Collection | ||
|
|
||
| import psycopg2 | ||
| from psycopg2.extensions import ( | ||
| connection as pg_connection, # pylint: disable=no-name-in-module | ||
| ) | ||
| from psycopg2.extensions import ( | ||
| cursor as pg_cursor, # pylint: disable=no-name-in-module | ||
| ) | ||
| from psycopg2.sql import Composed # pylint: disable=no-name-in-module | ||
|
|
||
| from opentelemetry import trace as trace_api | ||
| from opentelemetry.instrumentation import dbapi | ||
| from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
| from opentelemetry.instrumentation.psycopg2.package import _instruments | ||
|
|
@@ -170,8 +174,8 @@ def instrumentation_dependencies(self) -> Collection[str]: | |
| return _instruments | ||
|
|
||
| def _instrument(self, **kwargs): | ||
| """Integrate with PostgreSQL Psycopg library. | ||
| Psycopg: http://initd.org/psycopg/ | ||
| """Integrate with PostgreSQL Psycopg2 library. | ||
| Psycopg2: https://www.psycopg.org/docs/ | ||
| """ | ||
| tracer_provider = kwargs.get("tracer_provider") | ||
| enable_sqlcommenter = kwargs.get("enable_commenter", False) | ||
|
|
@@ -199,20 +203,28 @@ def _uninstrument(self, **kwargs): | |
|
|
||
| # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql | ||
| @staticmethod | ||
| def instrument_connection(connection, tracer_provider=None): | ||
| """Enable instrumentation in a psycopg2 connection. | ||
| def instrument_connection( | ||
| connection: pg_connection, | ||
| tracer_provider: typing.Optional[trace_api.TracerProvider] = None, | ||
| enable_commenter: bool = False, | ||
| commenter_options: dict = None, | ||
| ): | ||
|
Member
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. Same here, should we keep
Contributor
Author
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. For psycopg2 maybe not as it doesn't support the same |
||
| """Enable instrumentation of a Psycopg2 connection. | ||
|
|
||
| Args: | ||
| connection: psycopg2.extensions.connection | ||
| The psycopg2 connection object to be instrumented. | ||
| tracer_provider: opentelemetry.trace.TracerProvider, optional | ||
| The TracerProvider to use for instrumentation. If not specified, | ||
| The TracerProvider to use for instrumentation. If not provided, | ||
| the global TracerProvider will be used. | ||
| enable_commenter: bool, optional | ||
| Optional flag to enable/disable sqlcommenter (default False). | ||
| commenter_options: dict, optional | ||
| Optional configurations for tags to be appended at the sql query. | ||
|
|
||
| Returns: | ||
| An instrumented psycopg2 connection object. | ||
| """ | ||
|
|
||
| if not hasattr(connection, "_is_instrumented_by_opentelemetry"): | ||
| connection._is_instrumented_by_opentelemetry = False | ||
|
|
||
|
|
@@ -221,7 +233,9 @@ def instrument_connection(connection, tracer_provider=None): | |
| connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory | ||
| ) | ||
| connection.cursor_factory = _new_cursor_factory( | ||
| tracer_provider=tracer_provider | ||
| tracer_provider=tracer_provider, | ||
| enable_commenter=enable_commenter, | ||
| commenter_options=commenter_options, | ||
| ) | ||
| connection._is_instrumented_by_opentelemetry = True | ||
| else: | ||
|
|
@@ -284,14 +298,23 @@ def get_statement(self, cursor, args): | |
| return statement | ||
|
|
||
|
|
||
| def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): | ||
| def _new_cursor_factory( | ||
| db_api: dbapi.DatabaseApiIntegration = None, | ||
| base_factory: pg_cursor = None, | ||
|
Member
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. I think would be something like
Contributor
Author
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. Yes! Put in other PR (#4267) |
||
| tracer_provider: typing.Optional[trace_api.TracerProvider] = None, | ||
| enable_commenter: bool = False, | ||
| commenter_options: dict = None, | ||
| ): | ||
| if not db_api: | ||
| db_api = DatabaseApiIntegration( | ||
| __name__, | ||
| Psycopg2Instrumentor._DATABASE_SYSTEM, | ||
| connection_attributes=Psycopg2Instrumentor._CONNECTION_ATTRIBUTES, | ||
| version=__version__, | ||
| tracer_provider=tracer_provider, | ||
| enable_commenter=enable_commenter, | ||
| commenter_options=commenter_options, | ||
| connect_module=psycopg2, | ||
| ) | ||
|
|
||
| base_factory = base_factory or pg_cursor | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.