Skip to content

fix: use sqlc query names for otelpgx/Datadog DB span resources - #791

Merged
shyim merged 1 commit into
mainfrom
cursor/otelpgx-sqlc-span-names-c5cf
Aug 12, 2026
Merged

fix: use sqlc query names for otelpgx/Datadog DB span resources#791
shyim merged 1 commit into
mainfrom
cursor/otelpgx-sqlc-span-names-c5cf

Conversation

@shyim

@shyim shyim commented Aug 12, 2026

Copy link
Copy Markdown
Member

Problem

In Datadog production, ~1M worker spans / 6h collapsed to resource_name:query -- with db.operation.name: --. The full SQL remained in db.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:

-- name: UpsertEnvironmentExtension :exec
INSERT INTO ...

NewPool configured otelpgx with WithTrimSQLInSpanName(), whose default span-name func takes the first whitespace-delimited word of the statement. For sqlc SQL that first “word” is --, so spans become query --.

Fix

  • Add database.SQLSpanName, which:
    • Prefer the sqlc query name when a -- name: <Name> ... comment is present (e.g. UpsertEnvironmentExtension)
    • Otherwise skip leading whitespace / non-sqlc -- comments and use the first SQL keyword (SELECT / INSERT / …)
    • Never uses the full SQL body (keeps cardinality low)
  • Wire it with otelpgx.WithSpanNameFunc(SQLSpanName) alongside the existing WithTrimSQLInSpanName() so span names and db.operation.name both use the helper
  • Unit-test named INSERT/SELECT/UPDATE/DELETE, plain SQL, and leading whitespace / multi-comment cases

No query semantics, pool settings, or unrelated telemetry changes.

Open in Web Open in Cursor 

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>
@shyim
shyim marked this pull request as ready for review August 12, 2026 05:35
@shyim
shyim merged commit 516112a into main Aug 12, 2026
5 checks passed
@shyim
shyim deleted the cursor/otelpgx-sqlc-span-names-c5cf branch August 12, 2026 05:35
@greptile-apps

greptile-apps Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR replaces otelpgx’s first-token SQL naming with a low-cardinality helper that recognizes sqlc annotations and otherwise uses the first SQL keyword.

  • Configures the database tracer to derive span and operation names through SQLSpanName.
  • Extracts validated sqlc query names while handling whitespace and leading line comments.
  • Adds table-driven coverage for generated queries, plain SQL, comments, and empty statements.

Confidence Score: 5/5

The 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.

Important Files Changed

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 &lt;name&gt;"]
  Tracer --> Operation["db.operation.name: &lt;name&gt;"]
Loading

Reviews (1): Last reviewed commit: "fix: use sqlc query names for otelpgx DB..." | Re-trigger Greptile

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.

2 participants