Skip to content

Commit 1d90124

Browse files
authored
Merge pull request #20 from Intellection/opentelemetry_nginx_module
Add OpenTelemetry distributed tracing support via `ngx_otel_module` module
2 parents 716fc8d + 0aede32 commit 1d90124

File tree

9 files changed

+96
-2
lines changed

9 files changed

+96
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 1.25.3-1
4+
5+
* Add OpenTelemetry (OTel) module i.e. `nginx-module-otel`.
6+
* Update observability headers:
7+
* Add `X-Trace-ID`.
8+
* Update logging fields:
9+
* Add `otel_trace_id`.
10+
311
## 1.25.3
412

513
* Use `zappi/nginx:1.25.3` as the docker base image.

Dockerfile

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apt-get update -y && \
66
apt-get install --no-install-recommends -y \
77
build-essential \
88
ca-certificates \
9+
# headers-more
910
libpcre3 \
1011
libpcre3-dev \
1112
wget \
@@ -18,7 +19,6 @@ WORKDIR /usr/src/
1819
ARG NGINX_VERSION="1.25.3"
1920
ARG NGINX_PKG="nginx-${NGINX_VERSION}.tar.gz"
2021
ARG NGINX_SHA="64c5b975ca287939e828303fa857d22f142b251f17808dfe41733512d9cded86"
21-
2222
RUN wget "http://nginx.org/download/${NGINX_PKG}" && \
2323
echo "${NGINX_SHA} *${NGINX_PKG}" | sha256sum -c - && \
2424
tar --no-same-owner -xzf ${NGINX_PKG} --one-top-level=nginx --strip-components=1
@@ -27,7 +27,6 @@ RUN wget "http://nginx.org/download/${NGINX_PKG}" && \
2727
ARG HEADERS_MORE_VERSION="0.37"
2828
ARG HEADERS_MORE_PKG="v${HEADERS_MORE_VERSION}.tar.gz"
2929
ARG HEADERS_MORE_SHA="cf6e169d6b350c06d0c730b0eaf4973394026ad40094cddd3b3a5b346577019d"
30-
3130
RUN wget "https://github.com/openresty/headers-more-nginx-module/archive/${HEADERS_MORE_PKG}" && \
3231
echo "${HEADERS_MORE_SHA} *${HEADERS_MORE_PKG}" | sha256sum -c - && \
3332
tar --no-same-owner -xzf ${HEADERS_MORE_PKG} --one-top-level=headers-more --strip-components=1
@@ -40,6 +39,28 @@ RUN cd nginx && \
4039
# Production container starts here
4140
FROM zappi/nginx:1.25.3
4241

42+
USER root
43+
44+
# Set up official Nginx repository
45+
RUN apt-get update -y && \
46+
apt-get install --no-install-recommends -y \
47+
ca-certificates \
48+
gnupg2 \
49+
wget && \
50+
rm -rf /var/lib/apt/lists/* /tmp/* && \
51+
wget -q -O - https://nginx.org/keys/nginx_signing.key | gpg --dearmor > /etc/apt/keyrings/nginx.gpg && \
52+
echo "deb [signed-by=/etc/apt/keyrings/nginx.gpg] http://nginx.org/packages/mainline/debian bookworm nginx" | tee /etc/apt/sources.list.d/nginx.list && \
53+
apt-get purge --auto-remove -y \
54+
ca-certificates \
55+
gnupg2 \
56+
wget
57+
58+
# Module: OpenTelemetry (OTel)
59+
RUN apt-get update -y && \
60+
apt-get install --no-install-recommends -y \
61+
nginx-module-otel && \
62+
rm -rf /var/lib/apt/lists/* /tmp/*
63+
4364
# Copy compiled module
4465
COPY --from=builder /usr/src/nginx/objs/*_module.so /etc/nginx/modules/
4566

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ server {
3636
}
3737
```
3838

39+
### Tracing
40+
41+
Add observability by enabling trace propagation and sending telemetry data to an
42+
OTel collector:
43+
44+
```nginx
45+
otel_trace on;
46+
otel_service_name example_service:nginx;
47+
otel_trace_context propagate;
48+
otel_exporter {
49+
endpoint otel-collector:4317;
50+
interval 5s;
51+
batch_size 512;
52+
batch_count 4;
53+
}
54+
```
55+
3956
## Further customisation
4057

4158
Since you're defining a standard `server` block, you can configure it however you like over and above just header manipulation. For example, you can add a custom `location`:

config/http.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ http {
8484
add_header X-Proxy-Backend-Response-Time $upstream_response_time;
8585
add_header X-Proxy-Request-Time $request_time;
8686

87+
# Observability headers
88+
add_header X-Trace-ID $otel_trace_id;
89+
8790
gzip on;
8891
gzip_comp_level 5;
8992
gzip_min_length 256;

config/log.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ log_format main_json escape=json
1616
'"http_x_proxy_backend_connect_time":"$upstream_connect_time",'
1717
'"http_x_proxy_backend_header_time":"$upstream_header_time",'
1818
'"http_x_proxy_backend_response_time":"$upstream_response_time",'
19+
'"otel_trace_id":"$otel_trace_id",'
1920
'"proxy_connection":"$proxy_connection",'
2021
'"proxy_x_forwarded_port":"$proxy_x_forwarded_port",'
2122
'"proxy_x_forwarded_proto":"$proxy_x_forwarded_proto",'

config/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load_module modules/ngx_http_headers_more_filter_module.so;
2+
load_module modules/ngx_otel_module.so;
23

34
include /etc/nginx/main.conf;
45
include /etc/nginx/http.conf;

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ services:
1616
- app
1717
networks:
1818
- local
19+
jaeger:
20+
image: jaegertracing/all-in-one:1.53
21+
ports:
22+
- "16686:16686"
23+
networks:
24+
- local
25+
otel-collector:
26+
image: otel/opentelemetry-collector-contrib:0.92.0
27+
command:
28+
- "--config=/etc/otel-collector/config.yml"
29+
volumes:
30+
- "./example/otel-collector-config.yml:/etc/otel-collector/config.yml"
31+
networks:
32+
- local
1933

2034
networks:
2135
local:

example/app.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Example application configuration
22

3+
otel_trace on;
4+
otel_service_name example_service:nginx;
5+
otel_trace_context propagate;
6+
otel_exporter {
7+
endpoint otel-collector:4317;
8+
interval 5s;
9+
batch_size 512;
10+
batch_count 4;
11+
}
12+
313
server {
414
listen 8080;
515

example/otel-collector-config.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exporters:
2+
debug:
3+
otlp:
4+
endpoint: jaeger:4317
5+
tls:
6+
insecure: true
7+
receivers:
8+
otlp:
9+
protocols:
10+
grpc:
11+
http:
12+
service:
13+
pipelines:
14+
traces:
15+
exporters:
16+
- debug
17+
- otlp
18+
receivers:
19+
- otlp

0 commit comments

Comments
 (0)