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(instrumentation-pg): add post-query custom attributes hook for spans #767

Closed

Conversation

Alon-Katz
Copy link

@Alon-Katz Alon-Katz commented Nov 30, 2021

Which problem is this PR solving?

  • There is currently no way to add custom attributes to Postgres spans before the query is resolved

Short description of the changes

@Alon-Katz Alon-Katz requested a review from a team as a code owner November 30, 2021 13:43
@linux-foundation-easycla

This comment has been minimized.

1 similar comment
@linux-foundation-easycla
Copy link

CLA Not Signed

@linux-foundation-easycla

This comment has been minimized.

@Alon-Katz
Copy link
Author

/easycla

@codecov
Copy link

codecov bot commented Nov 30, 2021

Codecov Report

Merging #767 (ae57bd8) into main (81b3190) will decrease coverage by 1.75%.
The diff coverage is 93.97%.

❗ Current head ae57bd8 differs from pull request most recent head f724864. Consider uploading reports for the commit f724864 to get more accurate results

@@            Coverage Diff             @@
##             main     #767      +/-   ##
==========================================
- Coverage   94.88%   93.13%   -1.76%     
==========================================
  Files          13       10       -3     
  Lines         684      888     +204     
  Branches      138       94      -44     
==========================================
+ Hits          649      827     +178     
- Misses         35       61      +26     
Impacted Files Coverage Δ
...node/opentelemetry-instrumentation-pg/src/utils.ts 96.77% <88.88%> (ø)
...e/opentelemetry-instrumentation-pg/test/pg.test.ts 94.13% <94.11%> (ø)
...elemetry-instrumentation-pg/src/instrumentation.ts 86.27% <100.00%> (ø)
...y-instrumentation-long-task/src/instrumentation.ts
...metry-instrumentation-document-load/src/version.ts
...lemetry-instrumentation-document-load/src/utils.ts
...telemetry-instrumentation-long-task/src/version.ts
...entelemetry-instrumentation-long-task/test/util.ts
...lugin-react-load/src/BaseOpenTelemetryComponent.ts
...entation-document-load/src/enums/AttributeNames.ts
... and 16 more

@vmarchaud
Copy link
Member

@Alon-Katz You'll need to rebase your PR against main

@vmarchaud vmarchaud requested a review from a team December 5, 2021 14:33
@Alon-Katz
Copy link
Author

@vmarchaud Sorry for the delay, I have rebased against main.

@vmarchaud
Copy link
Member

@rauno56 could you take the time to review this PR ?

@rauno56
Copy link
Member

rauno56 commented Dec 20, 2021

I apologize for missing this one, @Alon-Katz. 20 days is really an unacceptable delay for a review.

This is no doubt an useful feature. Not only for pg but potentially every other instrumentation.

What kind of custom attributes are you looking to add to the spans from pg, @Alon-Katz?
From the test cases in the PR I'm guessing it's about doing additional processing on what's already in the attributes and trying to provide additional convenience filtering to the o11y suite you are using?

@vmarchaud
Copy link
Member

@rauno56 Judging from the fact that the PR has been opened from https://github.com/epsagon (which is an apm product) i guess they want to have more metadata to display.

@rauno56
Copy link
Member

rauno56 commented Dec 20, 2021

Sure, I was asking to validate whether it could be done somewhere else in the pipeline. It's a functionality commonly asked for - it doesn't scale well to add post-hooks to each of the instrumentations.
Currently the span processor provides a onEnd hook to a ReadableSpan, which, is not sufficient in this case - we should either reconsider that or add something into the instrumentation base.

@vmarchaud
Copy link
Member

we should either reconsider that or add something into the instrumentation base.

There has been multiple discussions across different instrumentations to do this on the instrument base, the problem is that you generally want instrumentation specific context so using SpanProcessor isn't viable. While no one took the work to add this kind of mechanism on instrumentation base its okay for me to let people add those hooks in each instrumentation

@rauno56
Copy link
Member

rauno56 commented Dec 20, 2021

What to you mean by instrumentation specific context? If I understand you correctly, I can only come up with

  1. the configuration for the instrumentation;
  2. information that's already on the span.

@vmarchaud
Copy link
Member

What to you mean by instrumentation specific context

Not every information is available as span attributes, for example one might want to add remote server version but we don't have it by default. I don't think its scalable to try add all data by default and let people use span processors. Specially since vendor might want other kind of data that we didn't think of.
I'm not sure to see why it's important anyway ? We already have those hooks in other instrumentation (so it solves a problem) and while there is no system on the base instrumentation class we can still merge this PR to allow this kind of use case ?

@rauno56
Copy link
Member

rauno56 commented Dec 21, 2021

Not every information is available as span attributes, for example one might want to add remote server version but we don't have it by default. I don't think its scalable to try add all data by default and let people use span processors. Specially since vendor might want other kind of data that we didn't think of.

We agree on that - There has to be a mechanism for vendors or users to add their own attributes and this PR solves for that in the context of pg instrumentation.

However, now that we've seen the same feature added into different instrumentations with somewhat different semantics or ergonomics again and again we should know better. I think merging this does more harm than good long term and support solving this in a more general manner instead.

Do you think there's an usecase we cannot support by making it a general feature?

@vmarchaud
Copy link
Member

vmarchaud commented Dec 21, 2021

Do you think there's an usecase we cannot support by making it a general feature?

I agree that we need to make it a general feature, but i don't agree with refusing PRs because contributors don't have the time to make a generic system.

cc @dyladan if you could give your opinion on this to see what to do here

@dyladan
Copy link
Member

dyladan commented Dec 21, 2021

I think as a feature it makes sense. There will always be information that people want to add to the spans with additional context that is not available at the span processor level. @rauno56 is the owner of this module and I think he should have the final decision. When we decided to give ownership of modules to the contributors who maintain them, the original idea was that they would have the final say in decisions like this unless there was a security concern or similar extenuating circumstance. My personal suggestion would be to accept the feature because there is currently no generic system and the feature provides value, but ultimately the decision is Rauno's.

Creating a generic system for this feature might be difficult because of all the different types of modules involved, but maybe we could at least make the configurations consistent across instrumentations that hook similar modules. For example, all the DB instrumentations would probably benefit from the same hooks with consistent naming. It might make sense for us to make our own document in this repo which describes a set of suggested hooks for each type of module?

@rauno56
Copy link
Member

rauno56 commented Dec 27, 2021

I agree that we need to make it a general feature, but i don't agree with refusing PRs because contributors don't have the time to make a generic system.

Yes, that makes sense actually, @vmarchaud. My bad. @Alon-Katz, could you please enable "Allow edits from maintainers" so we wouldn't be stuck with the branch being out of date with the base branch.

In conclusion, I'd love to merge this for now with the hope to replace it with something more general in the long run.

I do not understand, however, why do you find it difficult to make something more general?

@dyladan
Copy link
Member

dyladan commented Dec 27, 2021

I do not understand, however, why do you find it difficult to make something more general?

Simply because of the wide variety of modules that need to be instrumented and the resulting variety of useful hook points and their arguments. Maybe its easier than I'm thinking.

@vmarchaud
Copy link
Member

I do not understand, however, why do you find it difficult to make something more general?

I don't think there is a lot of hidden complixity in this (not saying its trivial though) but we all have limited time to allocate and this wasn't a priority in the past (specially with the GA of api/sdk). There was discussion for a more generic instrumentation API that might have specified how to handle generic configuration like this (open-telemetry/opentelemetry-js#2359), so generally we might want to have a complete API for instrumentation before trying to implement this feature in the base instrumentation class

@Alon-Katz
Copy link
Author

Yes, that makes sense actually, @vmarchaud. My bad. @Alon-Katz, could you please enable "Allow edits from maintainers" so we wouldn't be stuck with the branch being out of date with the base branch.

@rauno56 I followed the link for "Allow edits from maintainers", but for some reason it seems like I do not have this option. According to the documentation, the option should be in the right section below the notifications, this is how it looks on my side:
Screen Shot 2021-12-28 at 13 49 16

@Flarna
Copy link
Member

Flarna commented Dec 28, 2021

@Alon-Katz Most likely the problem is because the repo you use is not your personal fork, it seems to be a fork from an organization. For such forks it's not working to allow maintainers to edit via this checkbox.

So either move to a private fork in future PRs or take the burden to merge master regularly...

I think @dyladan found some rest call from github to enable this for a single branch/PR even on org forks.

@dyladan
Copy link
Member

dyladan commented Dec 28, 2021

You can do it with cURL:

curl -X PATCH -u $GITHUB_USER:$GITHUB_TOKEN -H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/vnd.github.v3+json" -d "{\"maintainer_can_modify\": true}"  "https://api.github.com/repos/open-telemetry/opentelemetry-js-contrib/pulls/767"

@Alon-Katz
Copy link
Author

You can do it with cURL:

curl -X PATCH -u $GITHUB_USER:$GITHUB_TOKEN -H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/vnd.github.v3+json" -d "{\"maintainer_can_modify\": true}"  "https://api.github.com/repos/open-telemetry/opentelemetry-js-contrib/pulls/767"

Thanks! I ran the command, hopefully it works.

@linux-foundation-easycla
Copy link

CLA Not Signed

@rauno56
Copy link
Member

rauno56 commented Jan 7, 2022

CLA Not Signed

* white_check_mark  Alon Katz ([d6f8af0](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/d6f8af008163a72c8b590ae002a7c780aab5b866), [85d5611](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/85d561152a84e0e63b094f64ae7ab958130ba63b), [27a4050](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/27a405076c3bf2214a6d8d9f34227a73e8b8cde1))

* white_check_mark  Rauno Viskus ([f724864](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/f724864f9ac08e07a85406937f37f1c809d6db4c))


* x The commit ([07924d6](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/07924d68c84e601d308ac19b4369c3c6d3271497)) is missing the User's ID, preventing the EasyCLA check. [Consult GitHub Help](https://help.github.com/en/github/committing-changes-to-your-project/why-are-my-commits-linked-to-the-wrong-user) to resolve.For further assistance with EasyCLA, [please submit a support request ticket](https://jira.linuxfoundation.org/servicedesk/customer/portal/4).

@Alon-Katz seems like your CLA is signed for the Cisco email, but one of the commits(07924d6) is done from an Epsagon email address. That's why EasyCLA is probably upset.

I'll review the PR on Monday.

}

export interface PgPostQueryHookFunction {
(ctx: QueryContext): void;
Copy link
Member

Choose a reason for hiding this comment

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

Can you please change the hook signature to follow the other hook example in this file:

export interface PgInstrumentationExecutionResponseHook {
  (span: api.Span, responseInfo: PgResponseHookInformation): void;
}

First parameter should be the span.
Second parameter is called Info on the other hook. Could be nice in my opinion to call it queryInfo (to stay consistent with the existing name.
From what I remember, this is also the convention in other instrumentations.

Comment on lines +574 to +575
assert.strictEqual(ctx.query, query);
assert.strictEqual(ctx.params, undefined);
Copy link
Member

Choose a reason for hiding this comment

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

These assertions are performed in the hook body.
I think that if they fail, the exception will just be silently ignored and test will pass.
That is because the hook is protected with safeExecuteInTheMiddle which does not propagate exceptions to the caller.

}
});
assert.strictEqual(called, true);
});
Copy link
Member

Choose a reason for hiding this comment

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

I also suggest having some negative tests for the hook - what happens if it throws? what if it's not a function?
This introduced new logic is currently not covered with tests.
You can see an example of that on the responseHook tests here

@@ -563,5 +563,113 @@ describe('pg', () => {
client.query('SELECT NOW()').then(queryHandler);
});
});

it('should call postQueryHook with query text if set', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

You can wrap this section with a describe to group all these tests under postQueryHook.
This will also be consistent with the existing tests for responseHook see here

@blumamir
Copy link
Member

blumamir commented Feb 2, 2022

Hi @Alon-Katz
Still interested in this PR?

@Alon-Katz
Copy link
Author

Hi @blumamir, thanks for time you put into making the CR but unfortunately I will not have the time to continue this PR at the moment, I will close it.

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.

Copy post-query custom attributes hook from pg plugin to pg instrumentation