Skip to content

[db] Add context propagation for PostgreSQL via SET application_name#3581

Open
mhennoch wants to merge 5 commits intoopen-telemetry:mainfrom
mhennoch:pg-set-app-name
Open

[db] Add context propagation for PostgreSQL via SET application_name#3581
mhennoch wants to merge 5 commits intoopen-telemetry:mainfrom
mhennoch:pg-set-app-name

Conversation

@mhennoch
Copy link
Copy Markdown

Add context propagation for PostgreSQL via SET application_name

Part of #2162

Adds SET application_name as a context propagation mechanism for PostgreSQL, following the pattern established for SQL Server (SET CONTEXT_INFO) and Oracle (V$SESSION.ACTION).

Why SET application_name?
PostgreSQL instrumentation already supports SQL Commenter, but it has limitations:

  • Prepared statements — SQL Commenter cannot be used with named/prepared queries, as the comment is baked into the statement text and every subsequent execution carries stale trace context.
  • Stored procedures — SQL comments on the outer CALL statement are not visible to queries executed inside the procedure body.

SET application_name operates at the session level and avoids all of these issues. The value is visible in pg_stat_activity and the OpenTelemetry Collector PostgreSQL receiver already parses it to extract trace context from query samples.

Note on application_name

There is ongoing discussion about whether application_name is the ideal long-term mechanism — a protocol-level approach would be better but doesn't exist today. An imperfect convention behind an opt-in flag is still useful: it unblocks users now and can help motivate database-native solutions over time.

Existing implementations

@github-actions github-actions bot added enhancement New feature or request area:db labels Mar 26, 2026
Copy link
Copy Markdown
Member

@XSAM XSAM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread docs/db/postgresql.md Outdated
mhennoch and others added 3 commits March 27, 2026 11:34
@mhennoch mhennoch marked this pull request as ready for review March 30, 2026 11:40
@mhennoch mhennoch requested review from a team as code owners March 30, 2026 11:40
@github-actions
Copy link
Copy Markdown

This PR has been labeled as stale due to lack of activity. It will be automatically closed if there is no further activity over the next 7 days.

@github-actions github-actions bot added the Stale label Apr 14, 2026
@pellared pellared removed the Stale label Apr 15, 2026
Copy link
Copy Markdown
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good step forward. Even if we later decide that application_name is not the ideal long-term propagation mechanism, having a documented convention now is useful: it gives instrumentation authors something interoperable to implement, helps users immediately, and can also inform a future redesign if we later standardize a better carrier or protocol-level approach.

Comment thread docs/db/postgresql.md

### SET application_name

Instrumentations MAY propagate context using [`SET application_name`](https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-APPLICATION-NAME) by injecting fixed-length part of span context (trace-id, span-id, trace-flags, protocol version) before executing a query. For example, when using W3C Trace Context, only a string representation of [`traceparent`](https://www.w3.org/TR/trace-context/#traceparent-header) SHOULD be injected. Context injection SHOULD NOT be enabled by default, but instrumentation MAY allow users to opt into it.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never says to restore or clear the prior value afterwards. PostgreSQL SET is session-scoped, and application_name is commonly set at connection time and surfaced in pg_stat_activity/logs, so on pooled connections this would overwrite the caller’s configured app name and can leak stale trace IDs into later operations. I think the spec needs either explicit restore/reset semantics or a clear incompatibility warning.

Comment thread docs/db/postgresql.md

### SET application_name

Instrumentations MAY propagate context using [`SET application_name`](https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-APPLICATION-NAME) by injecting fixed-length part of span context (trace-id, span-id, trace-flags, protocol version) before executing a query. For example, when using W3C Trace Context, only a string representation of [`traceparent`](https://www.w3.org/TR/trace-context/#traceparent-header) SHOULD be injected. Context injection SHOULD NOT be enabled by default, but instrumentation MAY allow users to opt into it.
Copy link
Copy Markdown
Member

@pellared pellared Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make an explicit design choice here:

  1. Either this convention is W3C Trace Context-specific, in which case the spec should say so directly and normatively define application_name as carrying only traceparent.
  2. Or we want this to support arbitrary propagators, in which case we should define application_name as a context propagation carrier, similar to the env-carriers spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/env-carriers.md.

Right now the text sits in between those two models. It says “for example, when using W3C Trace Context” but does not actually define carrier semantics for non-W3C propagators.

If we choose the generic route, the carrier should be format-agnostic and treat the value as an opaque string, while the propagator remains responsible for key selection, encoding, validation, and any truncation behavior. We would also need to specify how a multi-field propagator maps into a single application_name string with a tight length limit.

My preference is to decide this explicitly in the spec rather than leave it implicit in instrumentation behavior. Given application_name size is limited. I think this should be always the one representation; the same as for traceparent or some other custom one. We may consider adding some _TP_ (which would stand for traceparent) prefix following what Datadog implementation does.

@pellared
Copy link
Copy Markdown
Member

pellared commented Apr 15, 2026

@open-telemetry/semconv-db-approvers, can you take a look as well?

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

Labels

area:db enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants