fix: use sqlc query names for otelpgx/Datadog DB span resources - #791
Merged
Conversation
otelpgx WithTrimSQLInSpanName took the first word of sqlc's leading `-- name: Foo` comment, collapsing Datadog resources to `query --`. Prefer the sqlc name (else first SQL keyword) via WithSpanNameFunc. Co-authored-by: Soner <github@shyim.de>
Contributor
Greptile SummaryThe PR replaces otelpgx’s first-token SQL naming with a low-cardinality helper that recognizes sqlc annotations and otherwise uses the first SQL keyword.
Confidence Score: 5/5The PR appears safe to merge, with no actionable correctness or security issues identified. The tracer option combination invokes the helper for both span naming and database operation attributes, and the parser handles the sqlc and plain-SQL formats currently sent through the pool.
|
| Filename | Overview |
|---|---|
| api/internal/database/db.go | Correctly combines otelpgx’s trim mode with the custom callback; the options are complementary rather than overriding. |
| api/internal/database/span_name.go | Adds bounded span-name extraction that matches the repository’s generated sqlc annotation format and current plain-SQL callers. |
| api/internal/database/span_name_test.go | Covers named CRUD queries, plain statements, leading whitespace and comments, and unknown-input fallbacks. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
SQL["SQL statement"] --> Parser["SQLSpanName"]
Parser -->|sqlc annotation| QueryName["Generated query name"]
Parser -->|plain SQL| Keyword["First SQL keyword"]
Parser -->|empty/comments only| Unknown["UNKNOWN"]
QueryName --> Tracer["otelpgx tracer"]
Keyword --> Tracer
Unknown --> Tracer
Tracer --> Span["Span: query <name>"]
Tracer --> Operation["db.operation.name: <name>"]
Reviews (1): Last reviewed commit: "fix: use sqlc query names for otelpgx DB..." | Re-trigger Greptile
This was referenced Aug 12, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
In Datadog production, ~1M worker spans / 6h collapsed to
resource_name:query --withdb.operation.name: --. The full SQL remained indb.statement.text/db.query.text, so the statements themselves were fine — only the resource/operation name was wrong.Root cause
sqlc embeds a leading annotation in generated SQL constants:
NewPoolconfigured otelpgx withWithTrimSQLInSpanName(), whose default span-name func takes the first whitespace-delimited word of the statement. For sqlc SQL that first “word” is--, so spans becomequery --.Fix
database.SQLSpanName, which:-- name: <Name> ...comment is present (e.g.UpsertEnvironmentExtension)--comments and use the first SQL keyword (SELECT/INSERT/ …)otelpgx.WithSpanNameFunc(SQLSpanName)alongside the existingWithTrimSQLInSpanName()so span names anddb.operation.nameboth use the helperNo query semantics, pool settings, or unrelated telemetry changes.