Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .chloggen/postgresql-application-name-propagation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
change_type: enhancement
component: db
note: Add context propagation for PostgreSQL via `SET application_name`.
issues: [2162]
subtext:
32 changes: 32 additions & 0 deletions docs/db/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ linkTitle: PostgreSQL
<!-- toc -->

- [Spans](#spans)
- [Context propagation](#context-propagation)
- [SET application\_name](#set-application_name)
- [Metrics](#metrics)

<!-- tocstop -->
Expand Down Expand Up @@ -165,6 +167,36 @@ and SHOULD be provided **at span creation time** (if provided at all):
<!-- END AUTOGENERATED TEXT -->
<!-- endsemconv -->

## Context propagation

**Status**: [Development][DocumentStatus]

### 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.
Comment thread
pellared marked this conversation as resolved.
Outdated
Comment thread
pellared marked this conversation as resolved.
Outdated

Variable context parts (`tracestate`, `baggage`) SHOULD NOT be injected since `application_name` value length 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).
Comment thread
mhennoch marked this conversation as resolved.
Outdated

Instrumentations that propagate context MUST execute `SET application_name` on the same physical connection as the SQL statement.

The `application_name` value is visible in [`pg_stat_activity`](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW) and can be included in PostgreSQL server logs via `%a` in [`log_line_prefix`](https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-LOG-LINE-PREFIX).

Example:

For a query `SELECT * FROM songs` where `traceparent` is `00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01`:

Run the following command on the same physical connection as the SQL statement:

```sql
SET application_name = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
```

Then run the query:

```sql
SELECT * FROM songs;
```

## Metrics

PostgreSQL client instrumentations SHOULD collect metrics according to the general
Expand Down
Loading