Skip to content

Support enable_commenter for instrument_connection by psycopg(2#3071

Closed
tammy-baylis-swi wants to merge 30 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:enable-commenter-instrument-connection
Closed

Support enable_commenter for instrument_connection by psycopg(2#3071
tammy-baylis-swi wants to merge 30 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:enable-commenter-instrument-connection

Conversation

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Dec 5, 2024

Description

Fixes #3070

Depends on #3043 to stop errors with manual tests (instrumenting an app and calling instrument_connection). Fixes docs and there will be merge conflicts.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • tox -e py312-test-instrumentation-psycopg2
  • tox -e py312-test-instrumentation-psycopg
  • Instrumented db client app that calls Psycopg2Instrumentor.instrument_connection
  • Instrumented db client app that calls PsycopgInstrumentor.instrument_connection

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@tammy-baylis-swi tammy-baylis-swi force-pushed the enable-commenter-instrument-connection branch from 4f969ef to e148c9f Compare December 7, 2024 01:52
@tammy-baylis-swi
Copy link
Copy Markdown
Contributor Author

Sorry to drag in several participants automatically! I committed changes I didn't mean to, and it's fixed now.

@tammy-baylis-swi tammy-baylis-swi marked this pull request as ready for review December 7, 2024 02:23
@tammy-baylis-swi tammy-baylis-swi requested a review from a team as a code owner December 7, 2024 02:23
@tammy-baylis-swi tammy-baylis-swi requested review from a team and removed request for federicobond February 19, 2026 01:29
@tammy-baylis-swi
Copy link
Copy Markdown
Contributor Author

Hi @open-telemetry/opentelemetry-python-contrib-approvers it's been a while for this one. May I please have a review?

Related PR #3043 has been approved though not yet merged.

trace_id = format(span.get_span_context().trace_id, "032x")
self.assertEqual(
MockCursor.execute.call_args[0][0],
f"Select 1 /*db_driver='psycopg%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we update this to use {trace_flags:02x} instead of hard coding 01 for the trace flags since this will change with the introduction of the random trace id flag. Context: open-telemetry/opentelemetry-python#4854

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great call. I've done this in the other PR (#4267)

enable_commenter: bool = False,
commenter_options: dict = None,
enable_attribute_commenter: bool = False,
):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we keep the old return type of -> ConnectionT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes! I've done this in other PR (#4267)

enable_commenter: bool = False,
commenter_options: dict = None,
enable_attribute_commenter=None,
):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here, should we keep -> ConnectionT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For psycopg2 maybe not as it doesn't support the same Async* types that psycopg(3) does.

connection._is_instrumented_by_opentelemetry = False

if not connection._is_instrumented_by_opentelemetry:
setattr(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will this conflict with your updates in #3043?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Won't be doing that PR anymore

base_factory: type[psycopg.Cursor] | None = None,
tracer_provider: TracerProvider | None = None,
db_api: dbapi.DatabaseApiIntegration = None,
base_factory: psycopg.Cursor = None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should keep as base_factory: type[psycopg.Cursor] | None = None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes! Kept in other PR (#4267)

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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think would be something like base_factory: typing.Optional[typing.Type[pg_cursor]] = None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes! Put in other PR (#4267)

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor Author

Closing this because I've closed dependent #3043

Will be replaced by #4267 when ready.

@github-project-automation github-project-automation bot moved this from Ready for review to Done in Python PR digest Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add sqlcomment support for psycopg(2) instrument_connection