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

feat(pg): support hooking into span when query is started #1307

Merged
merged 1 commit into from
Dec 1, 2022

Conversation

ethanresnick
Copy link
Contributor

@ethanresnick ethanresnick commented Nov 26, 2022

Which problem is this PR solving?

Right now, it's possible to modify the span generated for a query after results are returned for the query, using the responseHook. However, it isn't possible to modify the span at the start of the query, based on the information available when the query is issued (i.e., the args passed to client.query), because that information isn't passed to the responseHook.

For my use case, I'm using Datadog and am tracing an app that issues relatively few distinct queries. Therefore, it makes sense to identify each query as a distinct Datadog "resource", by adding a resource.name attribute to each span, where the value of the attribute is the query text. However, this is currently impossible, because the query text isn't available in the responseHook, and there's no other hook where it is available.

Short description of the changes

  • Add a queryHook param to support the above use case.

NB: this PR shares the first two commits with #1306, so it may be simpler to review that first. It relies on the code cleanup from #1306 to simplify implementing the queryHook here. (The code cleanup now allows the main code path in _getClientQueryPatch to have a single, normalized queryConfig object that can be passed to the queryHook.)

@ethanresnick ethanresnick requested a review from a team November 26, 2022 03:05
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 26, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested a review from rauno56 November 26, 2022 03:05
@ethanresnick ethanresnick changed the title pg: support hooking into span when query is started feat(pg): support hooking into span when query is started Nov 26, 2022
@ethanresnick ethanresnick force-pushed the query-hook branch 5 times, most recently from 7286646 to 0993f7b Compare November 26, 2022 10:10
@blumamir
Copy link
Member

Maybe I am missing something, but the query text is currently always captured into db.statement span attribute here
Are you looking to duplicate this attribute in your app and record the same text twice?
I am good with adding this hook if you need it but just wanna make sure it is needed.

@ethanresnick
Copy link
Contributor Author

Are you looking to duplicate this attribute in your app and record the same text twice?

Yup, exactly. I need to duplicate the value into the resource.name attribute to make the Datadog UI work well for my use case (see OP).

@ethanresnick ethanresnick force-pushed the query-hook branch 2 times, most recently from 593cf15 to f171555 Compare November 29, 2022 03:47
@blumamir
Copy link
Member

blumamir commented Nov 30, 2022

Are you looking to duplicate this attribute in your app and record the same text twice?

Yup, exactly. I need to duplicate the value into the resource.name attribute to make the Datadog UI work well for my use case (see OP).

Are you using OpenTelemetry Collector to send spans to DD? there is the attributesprocessor which does just that

If you can't modify this value in collector or prefer to use instrumentation hook that is fine and I can review this PR.

@codecov
Copy link

codecov bot commented Nov 30, 2022

Codecov Report

Merging #1307 (dd3f8ba) into main (8a375f5) will decrease coverage by 0.27%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1307      +/-   ##
==========================================
- Coverage   96.08%   95.80%   -0.28%     
==========================================
  Files          14       18       +4     
  Lines         893     1217     +324     
  Branches      191      259      +68     
==========================================
+ Hits          858     1166     +308     
- Misses         35       51      +16     
Impacted Files Coverage Δ
...elemetry-instrumentation-pg/src/instrumentation.ts 90.90% <100.00%> (ø)
...node/opentelemetry-instrumentation-pg/src/utils.ts 98.33% <100.00%> (ø)
...try-instrumentation-pg/src/enums/AttributeNames.ts 100.00% <0.00%> (ø)
...tapackages/auto-instrumentations-node/src/utils.ts 98.21% <0.00%> (ø)

@ethanresnick
Copy link
Contributor Author

Are you using OpenTelemetry Collector to send spans to DD?

Unfortunately I'm using the DD agent's built-in support for receiving Otel data (this approach), so that I don't have to run a standalone instance of the collector. I think that the DD agent doesn't support attribute processors (or it doesn't expose that support), so a hook seems easier.

@ethanresnick
Copy link
Contributor Author

@blumamir Ok, I think I addressed all your comments. I also ended up renaming the hook from queryHook to requestHook, to match the name of the analogous hook on the redis instrumentation (and for parallelism with responseHook). Honestly, requestHook feels like a bit of a weird name to me, but I figured it was more important to be consistent.

Copy link
Member

@blumamir blumamir left a comment

Choose a reason for hiding this comment

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

LGTM 🥇

@ethanresnick
Copy link
Contributor Author

@rauno56 Given your comment in #1306, I guess this is good to merge too (once CI finishes)? Thanks :)

@blumamir blumamir merged commit f0a9368 into open-telemetry:main Dec 1, 2022
@dyladan dyladan mentioned this pull request Dec 1, 2022
@ethanresnick ethanresnick deleted the query-hook branch December 1, 2022 05:21
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.

3 participants