Skip to content

Commit fed17d8

Browse files
committed
make nginx tracing more clear
1 parent c171920 commit fed17d8

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

docs/product/drains/otlp-guides/nginx.mdx

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,54 @@ ___OTLP_LOGS_URL___
3737
x-sentry-auth: sentry sentry_key=___PUBLIC_KEY___
3838
```
3939

40-
## Step 3: Configure Nginx Logging (Optional)
40+
## Step 3: Configure Nginx Logging
4141

42-
Nginx logs are stored by default at `/var/log/nginx/access.log`. For better log parsing, you can configure Nginx to output logs in JSON format.
42+
Nginx logs are stored by default at `/var/log/nginx/access.log`. For better observability, configure Nginx to output structured JSON logs with trace context fields. This allows your Nginx logs to be correlated with distributed traces in Sentry.
43+
44+
### JSON Logging with Trace Context (Recommended)
45+
46+
To correlate Nginx logs with traces, you need to include trace context fields in your log format. This requires the [NGINX OpenTelemetry module](https://github.com/open-telemetry/opentelemetry-cpp-contrib/tree/main/instrumentation/nginx) to be installed, which provides the `$otel_trace_id`, `$otel_span_id`, and `$otel_trace_flags` variables.
4347

4448
Add the following to your Nginx configuration (typically `/etc/nginx/nginx.conf`):
4549

50+
```nginx {filename:nginx.conf}
51+
load_module modules/ngx_otel_module.so;
52+
53+
http {
54+
# Enable OpenTelemetry tracing
55+
otel_exporter {
56+
endpoint localhost:4317;
57+
}
58+
otel_trace on;
59+
60+
log_format json_combined escape=json
61+
'{'
62+
'"time_local":"$time_local",'
63+
'"remote_addr":"$remote_addr",'
64+
'"remote_user":"$remote_user",'
65+
'"request":"$request",'
66+
'"status":$status,'
67+
'"body_bytes_sent":$body_bytes_sent,'
68+
'"http_referer":"$http_referer",'
69+
'"http_user_agent":"$http_user_agent",'
70+
'"request_time":$request_time,'
71+
'"trace_id":"$otel_trace_id",'
72+
'"span_id":"$otel_span_id",'
73+
'"trace_flags":"$otel_trace_flags"'
74+
'}';
75+
76+
access_log /var/log/nginx/access.log json_combined;
77+
78+
# ... rest of your configuration
79+
}
80+
```
81+
82+
The trace context fields enable Sentry to link your Nginx access logs directly to the corresponding traces, giving you full visibility into request flows.
83+
84+
### Basic JSON Logging (Without Trace Context)
85+
86+
If you don't have the OpenTelemetry module installed, you can still use structured JSON logging:
87+
4688
```nginx {filename:nginx.conf}
4789
http {
4890
log_format json_combined escape=json
@@ -77,18 +119,36 @@ Create a configuration file with the File Log Receiver and the OTLP HTTP exporte
77119

78120
For additional configuration options, see the [File Log Receiver Documentation](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/filelogreceiver).
79121

80-
### Basic Configuration
122+
### Configuration with Trace Context (Recommended)
81123

82-
This configuration collects Nginx access and error logs:
124+
If you configured Nginx with the OpenTelemetry module and JSON logging with trace context fields, use this configuration to parse the logs and extract trace correlation:
83125

84126
```yaml {filename:config.yaml}
85127
receivers:
86128
filelog:
87129
include:
88130
- /var/log/nginx/access.log
131+
attributes:
132+
service.name: nginx
133+
operators:
134+
- type: json_parser
135+
timestamp:
136+
parse_from: attributes.time_local
137+
layout: "%d/%b/%Y:%H:%M:%S %z"
138+
- type: trace_parser
139+
trace_id:
140+
parse_from: attributes.trace_id
141+
span_id:
142+
parse_from: attributes.span_id
143+
trace_flags:
144+
parse_from: attributes.trace_flags
145+
146+
filelog/error:
147+
include:
89148
- /var/log/nginx/error.log
90149
attributes:
91150
service.name: nginx
151+
log.type: error
92152

93153
processors:
94154
batch:
@@ -109,35 +169,27 @@ service:
109169
logs:
110170
receivers:
111171
- filelog
172+
- filelog/error
112173
processors:
113174
- batch
114175
exporters:
115176
- otlphttp/sentry
116177
```
117178
118-
### Configuration with JSON Parsing
179+
The `trace_parser` operator extracts the trace ID, span ID, and trace flags from the parsed JSON attributes and sets them on the log record. This enables Sentry to correlate these logs with the corresponding traces.
180+
181+
### Basic Configuration
119182

120-
If you configured Nginx to output JSON logs, you can parse them for structured log data:
183+
This configuration collects Nginx access and error logs:
121184

122185
```yaml {filename:config.yaml}
123186
receivers:
124187
filelog:
125188
include:
126189
- /var/log/nginx/access.log
127-
attributes:
128-
service.name: nginx
129-
operators:
130-
- type: json_parser
131-
timestamp:
132-
parse_from: attributes.time_local
133-
layout: "%d/%b/%Y:%H:%M:%S %z"
134-
135-
filelog/error:
136-
include:
137190
- /var/log/nginx/error.log
138191
attributes:
139192
service.name: nginx
140-
log.type: error
141193
142194
processors:
143195
batch:
@@ -158,7 +210,6 @@ service:
158210
logs:
159211
receivers:
160212
- filelog
161-
- filelog/error
162213
processors:
163214
- batch
164215
exporters:

0 commit comments

Comments
 (0)