-
Notifications
You must be signed in to change notification settings - Fork 542
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): support custom SQL Commenter attributes #1454
Closed
joelmukuthu
wants to merge
2
commits into
open-telemetry:main
from
joelmukuthu:feat/sqlcommenter-custom-attributes
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ function arrayStringifyHelper(arr: Array<unknown>): string { | |
return '[' + arr.toString() + ']'; | ||
} | ||
|
||
export type SqlCommenterAttributes = Record<string, string>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be preferred that I list all the known attributes instead of the index signature? Or both? |
||
|
||
/** | ||
* Helper function to get a low cardinality span name from whatever info we have | ||
* about the query. | ||
|
@@ -284,7 +286,11 @@ function fixedEncodeURIComponent(str: string) { | |
); | ||
} | ||
|
||
export function addSqlCommenterComment(span: Span, query: string): string { | ||
export function addSqlCommenterComment( | ||
span: Span, | ||
query: string, | ||
customAttributes: SqlCommenterAttributes = {} | ||
): string { | ||
if (typeof query !== 'string' || query.length === 0) { | ||
return query; | ||
} | ||
|
@@ -296,7 +302,7 @@ export function addSqlCommenterComment(span: Span, query: string): string { | |
} | ||
|
||
const propagator = new W3CTraceContextPropagator(); | ||
const headers: { [key: string]: string } = {}; | ||
const headers: SqlCommenterAttributes = customAttributes; | ||
propagator.inject( | ||
trace.setSpan(ROOT_CONTEXT, span), | ||
headers, | ||
|
@@ -311,9 +317,10 @@ export function addSqlCommenterComment(span: Span, query: string): string { | |
} | ||
|
||
const commentString = sortedKeys | ||
.map(key => { | ||
.map((key) => { | ||
const encodedKey = fixedEncodeURIComponent(key); | ||
const encodedValue = fixedEncodeURIComponent(headers[key]); | ||
return `${key}='${encodedValue}'`; | ||
return `${encodedKey}='${encodedValue}'`; | ||
}) | ||
.join(','); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this bit, any suggestions are welcomed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work. This names the symbol but it is still unique each time it is called. You would need to use
Symbol.for
in order to get the same symbol each time, but I think this approach loses some of the advantage of using a symbol.Using context seems like a reasonable way to go about this but I haven't seen any other instrumentations do this in any language I think. I think the idea of using context to inject things into instrumentations has merit, but maybe the mechanism can be generalized to other instrumentations as well.
@open-telemetry/javascript-maintainers what would you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're quite right, I was forced to use a
Symbol
here because that's what the API expects. I think if we came up with a standard way of using the context to pass data to instrumentations, then there'd be a different API here.I'm not sure I can offer much insight into what the API could look like but I'm keen to hear what the others think and very happy to implement what is agreed upon 🙌