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

DB-API instrumentor populates span after sqlcomment creation, not before #2935

Merged

Conversation

tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Oct 28, 2024

Description

Re-orders database query span population by DB-API instrumentor so that db.statement span attribute includes any sqlcomments generated.

Fixes #2936

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?

Added unit test. Locally installed this version of opentelemetry-instrumentation-dbapi, upstream dependencies, and downstream instrumentors for psycopg2, psycopg that use all of the above. Instrumented two Flask app that query PostgreSQL with general logs enabled to check sqlcomments: one via psycopg2, one with psycopg2. sqlcommenter enabled as per docs (here and here). Spans were exported to a locally-running otel-collector to inspect attributes including db.statement.

After changes, example span from psycopg2 query with updated db.statement:

ScopeSpans #0
ScopeSpans SchemaURL:
InstrumentationScope opentelemetry.instrumentation.psycopg2 0.49b0.dev
Span #0
    Trace ID       : acbdad825030e4fc58260b2bd9861eb3
    Parent ID      : b1b7f10bb5c841ff
    ID             : 451dac2c63520272
    Name           : SELECT
    Kind           : Client
    Start time     : 2024-10-29 19:17:38.333191847 +0000 UTC
    End time       : 2024-10-29 19:17:38.335095514 +0000 UTC
    Status code    : Unset
    Status message :
Attributes:
     -> db.system: Str(postgresql)
     -> db.name: Str(world-db)
     -> db.statement: Str(SELECT * FROM city WHERE id = %s /*db_driver='psycopg2%%3A2.9.9',traceparent='00-acbdad825030e4fc58260b2bd9861eb3-451dac2c63520272-01'*/)
     -> db.user: Str(world)
     -> net.peer.name: Str(postgres-world-db)
     -> net.peer.port: Int(5432)

which matches sqlcomment in corresponding postgresql general log:

2024-10-29 19:17:30 UTC,4/3,0,LOG,00000,"statement: SELECT * FROM city WHERE id = '1818' /*db_driver='psycopg2%3A2.9.9',traceparent='00-acbdad825030e4fc58260b2bd9861eb3-451dac2c63520272-01'*/",,,,,,,,,"","client backend",,0

Before changes, db.statement does not include sqlcomment (trimmed for clarity):

Attributes:
     -> db.statement: Str(SELECT * FROM city WHERE id = %s)

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 changed the title [WIP] DB-API instrumentor populates span after sqlcomment creation, not before DB-API instrumentor populates span after sqlcomment creation, not before Oct 29, 2024
@tammy-baylis-swi tammy-baylis-swi marked this pull request as ready for review October 29, 2024 19:58
@tammy-baylis-swi tammy-baylis-swi requested a review from a team as a code owner October 29, 2024 19:58
@tammy-baylis-swi tammy-baylis-swi requested a review from xrmx November 4, 2024 20:35
@xrmx xrmx closed this Nov 12, 2024
@xrmx xrmx reopened this Nov 12, 2024
@lzchen lzchen merged commit 53b8714 into open-telemetry:main Nov 19, 2024
566 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include sqlcomment in db.statement span attribute from DB-API instrumentation
3 participants