Skip to content

Commit

Permalink
enable_commenter for instrument_connection psycopg(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Dec 5, 2024
1 parent 6c92f38 commit d2da8cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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
Expand Down Expand Up @@ -195,7 +196,12 @@ 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):
def instrument_connection(
connection,
tracer_provider=None,
enable_commenter: bool = False,
commenter_options: dict = None,
):
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
connection._is_instrumented_by_opentelemetry = False

Expand All @@ -204,7 +210,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:
Expand Down Expand Up @@ -288,14 +296,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
)
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
Expand Down Expand Up @@ -159,7 +160,12 @@ 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):
def instrument_connection(
connection,
tracer_provider=None,
enable_commenter: bool = False,
commenter_options: dict = None,
):
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
connection._is_instrumented_by_opentelemetry = False

Expand All @@ -168,7 +174,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:
Expand Down Expand Up @@ -231,14 +239,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__,
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
Expand Down

0 comments on commit d2da8cd

Please sign in to comment.