Skip to content

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

Closed
mhennoch wants to merge 7 commits into
open-telemetry:mainfrom
mhennoch:pg-set-app-name
Closed

[db] Add context propagation for PostgreSQL via SET application_name#3581
mhennoch wants to merge 7 commits into
open-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

@XSAM XSAM left a comment

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.

👍

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

@pellared pellared left a comment

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 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 Outdated
Comment thread docs/db/postgresql.md Outdated
@pellared

pellared commented Apr 15, 2026

Copy link
Copy Markdown
Member

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

@lmolkova lmolkova moved this from Untriaged to Awaiting codeowners approval in Semantic Conventions Triage Apr 27, 2026
@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 30, 2026
@github-actions github-actions Bot closed this May 7, 2026
@pellared pellared reopened this May 18, 2026
@pellared pellared removed the Stale label May 18, 2026
@mhennoch
mhennoch requested a review from pellared May 25, 2026 10:31
@pellared
pellared requested a review from Copilot June 1, 2026 13:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a PostgreSQL-specific context propagation mechanism to the database semantic conventions by specifying how to inject W3C Trace Context via the session-scoped application_name setting.

Changes:

  • Document PostgreSQL context propagation using SET application_name with traceparent.
  • Add a changelog entry for the new PostgreSQL context propagation guidance.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
docs/db/postgresql.md Adds a new “Context propagation” section describing SET application_name usage and examples.
.chloggen/postgresql-application-name-propagation.yaml Adds a release note entry for the PostgreSQL context propagation addition.

Comment thread docs/db/postgresql.md
Comment on lines +176 to +180
Instrumentations MAY propagate W3C Trace Context using [`SET application_name`](https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-APPLICATION-NAME) by setting `application_name` to the string representation of [`traceparent`](https://www.w3.org/TR/trace-context/#traceparent-header) before executing a query. The value MUST NOT include any prefix, suffix, or wrapper. Context injection SHOULD NOT be enabled by default, but instrumentation MAY allow users to opt into it.

Other context values (`tracestate`, `baggage`) MUST NOT be injected because `application_name` is limited to less than [`NAMEDATALEN`](https://www.postgresql.org/docs/current/runtime-config-preset.html#GUC-MAX-IDENTIFIER-LENGTH) characters (63 characters in a standard build).

Because `application_name` is session-scoped, instrumentations SHOULD update it before every propagated query on the same physical connection as the query. Instrumentations MAY clear or restore it after query execution. Users should be aware that while enabled, this mechanism may overwrite connection-level `application_name` values between queries.
@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 Jun 16, 2026
@github-actions github-actions Bot closed this Jun 24, 2026
@pellared pellared reopened this Jun 24, 2026
@github-actions github-actions Bot removed the Stale label Jun 25, 2026
@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 Jul 10, 2026
@github-actions github-actions Bot closed this Jul 18, 2026
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 Stale

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants