Skip to content

Commit 0d7fd1f

Browse files
frenzzyjoseluisq
andauthored
Log trace context option (v3) (#11)
* docs: document the log trace context option Describe the `--log-trace-context` option that adds the trace context of a valid W3C `traceparent` request header to the JSON log lines written for the request (the `response` line, the access log format line and the lines logged while the request is handled), and list it in the CLI, environment variable and config file references. * docs: note that the trace context header comes from any client Say that `traceparent` is taken from any client, unlike the proxy headers, so the trace context fields are only reliable behind a proxy that sets or replaces it; that a version above `00` keeps only the `sampled` flag; and that lines logged while the response body is sent get the fields too. --------- Co-authored-by: Jose Quintana <1700322+joseluisq@users.noreply.github.com>
1 parent 4302451 commit 0d7fd1f

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/v3/configuration/cli.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Options:
122122
Log the first X-Forwarded-For address of requests from --trusted-proxies [env: SERVER_LOG_FORWARDED_FOR=] [default: false] [possible values: true, false]
123123
--trusted-proxies <TRUSTED_PROXIES>
124124
List of proxy IPs to use the X-Real-IP and X-Forwarded-For headers from. The default trusts no one, so the headers are not logged unless set. IPv4 entries also match their IPv4-mapped IPv6 form (`::ffff:a.b.c.d`) [env: SERVER_TRUSTED_PROXIES=]
125+
--log-trace-context [<LOG_TRACE_CONTEXT>]
126+
Add the `trace_id`, `span_id` and `trace_flags` fields of a valid W3C `traceparent` request header to the log lines of the request. The header is taken from any client, so the fields are only reliable behind a proxy that sets or replaces it. Requires `--log-format json` [env: SERVER_LOG_TRACE_CONTEXT=] [default: false] [possible values: true, false]
125127
--redirect-trailing-slash [<REDIRECT_TRAILING_SLASH>]
126128
Check for a trailing slash in the requested directory URI and redirect permanently (308) to the same path with a trailing slash suffix if it is missing [env: SERVER_REDIRECT_TRAILING_SLASH=] [default: true] [possible values: true, false]
127129
--include-hidden [<INCLUDE_HIDDEN>]

‎src/v3/configuration/env.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ Log the first `X-Forwarded-For` address of requests from `SERVER_TRUSTED_PROXIES
215215

216216
List of proxy IPs to use the `X-Real-IP` and `X-Forwarded-For` headers from. IPv4 entries also match their IPv4-mapped IPv6 form (`::ffff:a.b.c.d`). See [Trusted proxies](../features/logging.md#trusted-proxies). Default empty (trusts no one, so the headers are not logged).
217217

218+
## SERVER_LOG_TRACE_CONTEXT
219+
220+
Add the `trace_id`, `span_id` and `trace_flags` fields of a valid W3C `traceparent` request header to the log lines written for the request. The header is taken from any client, so the fields are only reliable behind a proxy that sets or replaces it. Requires the `json` log format. See [Logging Trace Context](../features/logging.md#logging-trace-context). Default `false`.
221+
218222
## SERVER_REDIRECT_TRAILING_SLASH
219223

220224
Check for a trailing slash in the requested directory URI and redirect permanently (308) to the same path with a trailing slash suffix if it is missing. Default `true` (enabled).

‎src/v3/configuration/file.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ log-forwarded-for = false
123123
#### Proxy IPs to accept the X-Real-IP and X-Forwarded-For headers from. Empty means none
124124
trusted-proxies = []
125125

126+
#### Log the W3C trace context of the `traceparent` header (JSON log format only)
127+
log-trace-context = false
128+
126129
#### Redirect to trailing slash in the requested directory uri
127130
redirect-trailing-slash = true
128131

‎src/v3/features/logging.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ When the level is `info` or lower, SWS logs one line per request with the `respo
104104
}
105105
```
106106

107-
No line is logged when SWS fails to build a response. The `remote_addr`, `x_real_ip` and `real_remote_ip` fields are added when the corresponding logging options are enabled and a value is available. Fields without a value are omitted.
107+
No line is logged when SWS fails to build a response. The `remote_addr`, `x_real_ip` and `real_remote_ip` fields are added when the corresponding logging options are enabled and a value is available, and the `trace_id`, `span_id` and `trace_flags` fields when [trace context logging](#logging-trace-context) is enabled. Fields without a value are omitted.
108108

109109
## Log Remote Addresses
110110

@@ -294,6 +294,38 @@ The URI and the `Host`, `User-Agent` and `Referer` headers come from the client.
294294

295295
An escaped value longer than 4 KiB is cut and ends with `...`, so a large header cannot produce a large log line.
296296

297+
## Logging Trace Context
298+
299+
When SWS runs behind a proxy or load balancer that starts or continues a [W3C Trace Context](https://www.w3.org/TR/trace-context/) trace, the proxy forwards a `traceparent` request header. SWS can add the trace context of that header to its log lines, so a log backend can link them to the trace and to the logs of other services.
300+
301+
This feature is disabled by default. Enable it with the boolean `--log-trace-context` option, the equivalent [SERVER_LOG_TRACE_CONTEXT](./../configuration/env#server-log-trace-context) env or the `log-trace-context` key under `[general]` in the [TOML file](./../configuration/file). It requires the `json` [log format](#log-format): SWS refuses to start with `--log-trace-context` and another format.
302+
303+
When enabled and the request has a valid `traceparent` header, the log lines written for the request get three top-level fields, as recommended by the OpenTelemetry [Trace Context in Non-OTLP Log Formats](https://opentelemetry.io/docs/specs/otel/compatibility/logging_trace_context/) specification:
304+
305+
- `trace_id`: the `trace-id` of the header (32 lowercase hex characters).
306+
- `span_id`: the `parent-id` of the header (16 lowercase hex characters), which is the span of the caller. SWS does not create spans of its own.
307+
- `trace_flags`: the `trace-flags` of the header (2 lowercase hex characters). For a `traceparent` version higher than `00`, only the `sampled` flag is kept (`01` or `00`), since the other flags are not defined by version `00`.
308+
309+
The fields are added to the per-request line (the `response` line of [Per-request logging](#per-request-logging), or the line of the [Access Log Format](#access-log-format) when one is set) and to the other lines logged while the request is handled or its response body is sent, such as the `error_page` warning of a `404`. Lines logged outside the request handler, such as connection errors or lines of the HTTPS redirect listener, do not get them. The fields are added at every log level, e.g. to the `404` warning when the level is `warn`.
310+
311+
```sh
312+
static-web-server -p 8787 -d ./public/ -g info --log-format json --log-trace-context
313+
```
314+
315+
```sh
316+
curl "http://localhost:8787/missing.css" \
317+
--header "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
318+
```
319+
320+
```json
321+
{"timestamp":"2026-09-28T10:39:57.103712+02:00","level":"WARN","method":"GET","uri":"/missing.css","status":404,"error":"Not Found","target":"static_web_server::error_page","span_id":"00f067aa0ba902b7","trace_flags":"01","trace_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
322+
{"timestamp":"2026-09-28T10:39:57.103890+02:00","level":"INFO","message":"response","method":"GET","uri":"/missing.css","status":404,"target":"static_web_server::log_addr","span_id":"00f067aa0ba902b7","trace_flags":"01","trace_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
323+
```
324+
325+
SWS only reads the header. It does not generate trace IDs, export spans or change the headers of the request or the response. Missing, invalid or repeated `traceparent` headers are ignored; see the specification's [versioning rules](https://www.w3.org/TR/trace-context/#versioning-of-traceparent).
326+
327+
Unlike `X-Real-IP` and `X-Forwarded-For`, which are logged only from [trusted proxies](#trusted-proxies), the `traceparent` header is taken from any client. A client that reaches SWS directly can put the trace ID of its choice on the log lines of its requests, so the fields are only reliable behind a proxy that sets or replaces the header (see [Denial of service](https://www.w3.org/TR/trace-context/#denial-of-service) in the specification).
328+
297329
## File Logging
298330

299331
By default **SWS** writes log records to standard error. The `--log-file` option additionally streams every record to a file on disk, in **addition** to `stderr`. This is useful for production deployments where `stderr` is captured by a service manager but a durable on-disk copy is also required.

0 commit comments

Comments
 (0)