-
Notifications
You must be signed in to change notification settings - Fork 943
feat: add tracing for sql #1610
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
06ca51b
feat: Move from datadog to generic otel
f0ssel f8c3ab7
add telemetry pkg
f0ssel 93ec818
add telemetry package
f0ssel b839175
cleanup data we send
f0ssel 25d5cb1
add doc comment
f0ssel 3eeda21
rename to tracing
f0ssel 69ca227
rename to tracing for real
f0ssel af7c5ef
move to coderd
f0ssel 46d9217
wip
f0ssel 2005d4c
fix merge
f0ssel cc5e0b0
Add span formatter
f0ssel 5b9b654
lint
f0ssel a2ddd77
remove postgres dev hack
f0ssel 4f401e1
try to fix tests
f0ssel 2cb35aa
dont use interface
f0ssel a4c4683
pr comments
f0ssel 0f5519b
lint
f0ssel 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
Add span formatter
- Loading branch information
commit cc5e0b00e401e25ce3af4b63f3df5d91ec718fec
There are no files selected for viewing
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,12 @@ | ||||||
package tracing | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"strings" | ||||||
|
||||||
"github.com/nhatthm/otelsql" | ||||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||||||
semconv "go.opentelemetry.io/otel/semconv/v1.7.0" | ||||||
"go.opentelemetry.io/otel/trace" | ||||||
"golang.org/x/xerrors" | ||||||
) | ||||||
|
@@ -17,10 +21,22 @@ func PostgresDriver(tp trace.TracerProvider, service string) (string, error) { | |||||
otelsql.TraceQueryWithoutArgs(), | ||||||
otelsql.WithSystem(semconv.DBSystemPostgreSQL), | ||||||
otelsql.WithTracerProvider(tp), | ||||||
otelsql.WithSpanNameFormatter(formatPostgresSpan), | ||||||
) | ||||||
if err != nil { | ||||||
return "", xerrors.Errorf("registering postgres tracing driver: %w", err) | ||||||
} | ||||||
|
||||||
return driverName, nil | ||||||
} | ||||||
|
||||||
func formatPostgresSpan(ctx context.Context, op string) string { | ||||||
const qPrefix = "-- name: " | ||||||
q := otelsql.QueryFromContext(ctx) | ||||||
if q == "" || !strings.HasPrefix(q, qPrefix) { | ||||||
return strings.ToUpper(op) | ||||||
} | ||||||
|
||||||
s := strings.Split(strings.TrimPrefix(q, qPrefix), " ")[0] | ||||||
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. Just a small perf suggestion to avoid processing the entire query.
Suggested change
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. of course! I was only paying attention to the first line lol |
||||||
return fmt.Sprintf("%s %s", strings.ToUpper(op), s) | ||||||
} |
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.
Should we add the
sql:
prefix (orcoder-db:
), like in https://pkg.go.dev/github.com/nhatthm/otelsql#readme-span-name-formatter? Wondering if it'd help with narrowing down the spans.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 just copying the format I've seen before with other database tracing tools (New Relic) and also mimicing the format of the http spans. Since an http span is
METHOD route
, I followed a similar format ofOP name
so the spans look likeQUERY GetAllUsersById
.I don't love the prefix because I already can see where the span is coming from by the instrumentation library (see the screenshots in description), but I also don't know if this is the case on other tools like Jaeger, which I'd like to setup eventually. I'm down to change it if we find it hard to parse as we use it, does that sound ok?
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.
Yeah, that sounds perfectly reasonable. 👍🏻