Skip to content

sql: let an application choose the span name of SQL statements #10950

Done

Description

@RhysSullivan

Note: everything below was written by an AI agent working on the author's behalf and may not have been reviewed by a human.

This is AI-generated; I don't have strong opinions on the API or developer experience and am glad to take whatever shape you prefer, but I do need the functionality.

Problem

Since #8866, a SQL statement's client span is named after its client's target, following the OpenTelemetry database conventions: db.namespace (the database name), else server.address[:server.port], else db.system.name. Before, every statement span was sql.execute.

The database name usually differs per environment (app_staging, app_prod, a per-branch preview database, postgresql on an embedded PGlite). The same statement therefore has a different span name in each environment. Dashboards, alerts and tests that find statement spans by name have to know every database name, or match on attributes instead. I would like one name for statement spans everywhere, while keeping db.namespace and server.address on the span as attributes, where they already are.

Statement.spanName is a plain function of the client's span attributes, and each driver calls it directly, so there is no way to choose the name without patching Statement. That is what I do today: a patch makes spanName return sql.execute.

Proposal

A setting that chooses the span name, for example a Context.Reference:

// Defaults to the current behaviour.
export const SpanName = Context.Reference<(spanAttributes: ReadonlyArray<readonly [string, unknown]>) => string>(
  "effect/sql/Statement/SpanName",
  { defaultValue: () => spanName }
)

// An application that wants one name:
Effect.provideService(Statement.SpanName, () => "sql.execute")

Statement and the drivers that call Statement.spanName (PgClient, D1Client) would read the reference instead. Transaction control (BEGIN, COMMIT and so on) could keep its own names.

Would you take this? An option on the client config would also work for me. I can open a PR in whichever form you prefer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions