Skip to content

Commit 68b7f7f

Browse files
committed
Add NGINX log exporter
1 parent d0163da commit 68b7f7f

File tree

6 files changed

+226
-1
lines changed

6 files changed

+226
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# truncate nginx logs that are shared with exporter so they don't fill the disk space
4+
5+
set -e
6+
7+
if [ ! "$CRON_DAILY_TRUNCATE_EXPORTER_LOGS" = "True" ];then
8+
exit 0
9+
fi
10+
11+
# logs are only used by exporter which reads then directly, no need to keep old logs around
12+
truncate -s0 /var/log/nginx/prometheus-nginxlog-exporter/access.log

docker/defaults.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ LOGGING_DRIVER=journald
198198

199199
# enable daily or weekly database backups to volume
200200
CRON_DAILY_POSTGRESQL_BACKUP=True
201+
CRON_DAILY_TRUNCATE_EXPORTER_LOGS=True
201202
CRON_WEEKLY_POSTGRESQL_BACKUP=False
202203

203204
INTERNETNL_BRANDING=False

docker/docker-compose.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ services:
5555
# persist certbot configuration between restarts
5656
- certbot-config:/etc/letsencrypt
5757
- htpasswd-files:/etc/nginx/htpasswd/external
58+
- nginx-logs-exporter:/var/log/nginx/prometheus-nginxlog-exporter/
5859

5960
healthcheck:
6061
test: ["CMD", "service", "nginx", "status"]
@@ -645,6 +646,7 @@ services:
645646
volumes:
646647
- manual-hof:/app/manual-hall-of-fame/
647648
- postgres-backups:/var/lib/postgresql/backups
649+
- nginx-logs-exporter:/var/log/nginx/prometheus-nginxlog-exporter/
648650

649651
healthcheck:
650652
test: ["CMD", "pgrep", "crond"]
@@ -819,6 +821,24 @@ services:
819821
volumes:
820822
- /var/run/docker.sock:/var/run/docker.sock
821823

824+
nginx_logs_exporter:
825+
image: ghcr.io/martin-helmich/prometheus-nginxlog-exporter/exporter:v1
826+
command:
827+
- -config-file=/config.hcl
828+
configs:
829+
- source: nginx_logs_exporter_config
830+
target: /config.hcl
831+
832+
restart: unless-stopped
833+
logging:
834+
driver: $LOGGING_DRIVER
835+
options:
836+
tag: '{{.Name}}'
837+
networks:
838+
- internal
839+
volumes:
840+
- nginx-logs-exporter:/var/log/nginx/prometheus-nginxlog-exporter/
841+
822842
volumes:
823843
postgres: {}
824844
postgres-backups: {}
@@ -835,6 +855,24 @@ volumes:
835855
grafana-data: {}
836856
# shares hosters HoF file between cron and app
837857
manual-hof: {}
858+
# shares nginx log files with log exporter
859+
nginx-logs-exporter: {}
860+
861+
configs:
862+
nginx_logs_exporter_config:
863+
content: |
864+
namespace "nginx" {
865+
format = "$$remote_addr $$host $$remote_user [$$time_local] $$proxy_host \"$$request\" $$status $$body_bytes_sent \"$$http_referer\" \"$$http_user_agent\" \"$$http_x_forwarded_for\""
866+
867+
source = {
868+
files = ["/var/log/nginx/prometheus-nginxlog-exporter/access.log"]
869+
}
870+
871+
relabel "proxy_host" { from = "proxy_host" }
872+
relabel "remote_user" { from = "remote_user" }
873+
}
874+
875+
838876
839877
networks:
840878
# disable default network
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"annotations": {
3+
"list": [
4+
{
5+
"builtIn": 1,
6+
"datasource": {
7+
"type": "grafana",
8+
"uid": "-- Grafana --"
9+
},
10+
"enable": true,
11+
"hide": true,
12+
"iconColor": "rgba(0, 211, 255, 1)",
13+
"name": "Annotations & Alerts",
14+
"target": {
15+
"limit": 100,
16+
"matchAny": false,
17+
"tags": [],
18+
"type": "dashboard"
19+
},
20+
"type": "dashboard"
21+
}
22+
]
23+
},
24+
"editable": true,
25+
"fiscalYearStartMonth": 0,
26+
"graphTooltip": 1,
27+
"links": [],
28+
"liveNow": false,
29+
"panels": [
30+
{
31+
"datasource": {
32+
"type": "prometheus",
33+
"uid": "PBFA97CFB590B2093"
34+
},
35+
"description": "",
36+
"fieldConfig": {
37+
"defaults": {
38+
"color": {
39+
"mode": "palette-classic"
40+
},
41+
"custom": {
42+
"axisCenteredZero": false,
43+
"axisColorMode": "text",
44+
"axisLabel": "",
45+
"axisPlacement": "auto",
46+
"barAlignment": 0,
47+
"drawStyle": "line",
48+
"fillOpacity": 0,
49+
"gradientMode": "none",
50+
"hideFrom": {
51+
"legend": false,
52+
"tooltip": false,
53+
"viz": false
54+
},
55+
"lineInterpolation": "linear",
56+
"lineWidth": 1,
57+
"pointSize": 5,
58+
"scaleDistribution": {
59+
"type": "linear"
60+
},
61+
"showPoints": "auto",
62+
"spanNulls": false,
63+
"stacking": {
64+
"group": "A",
65+
"mode": "none"
66+
},
67+
"thresholdsStyle": {
68+
"mode": "off"
69+
}
70+
},
71+
"mappings": [],
72+
"thresholds": {
73+
"mode": "absolute",
74+
"steps": [
75+
{
76+
"color": "green",
77+
"value": null
78+
},
79+
{
80+
"color": "red",
81+
"value": 80
82+
}
83+
]
84+
},
85+
"unit": "reqpm"
86+
},
87+
"overrides": []
88+
},
89+
"gridPos": {
90+
"h": 19,
91+
"w": 24,
92+
"x": 0,
93+
"y": 0
94+
},
95+
"id": 3,
96+
"interval": "1m",
97+
"options": {
98+
"legend": {
99+
"calcs": [
100+
"sum",
101+
"mean",
102+
"last"
103+
],
104+
"displayMode": "table",
105+
"placement": "bottom",
106+
"showLegend": true,
107+
"sortBy": "Total",
108+
"sortDesc": true
109+
},
110+
"tooltip": {
111+
"mode": "single",
112+
"sort": "none"
113+
}
114+
},
115+
"pluginVersion": "9.3.1",
116+
"targets": [
117+
{
118+
"datasource": {
119+
"type": "prometheus",
120+
"uid": "oUXCLhCMk"
121+
},
122+
"editorMode": "code",
123+
"exemplar": false,
124+
"expr": "sum by (status) (rate(nginx_http_response_count_total{proxy_host=\"192.168.42.103:8080\"}[$__rate_interval])) * 60",
125+
"format": "time_series",
126+
"instant": false,
127+
"legendFormat": "__auto",
128+
"range": true,
129+
"refId": "A"
130+
}
131+
],
132+
"title": "Frontend HTTP requests",
133+
"type": "timeseries"
134+
}
135+
],
136+
"refresh": "30s",
137+
"schemaVersion": 38,
138+
"style": "dark",
139+
"tags": [
140+
"beta"
141+
],
142+
"templating": {
143+
"list": []
144+
},
145+
"time": {
146+
"from": "now-3h",
147+
"to": "now"
148+
},
149+
"timepicker": {},
150+
"timezone": "",
151+
"title": "NGINX logs (webserver)",
152+
"uid": "2R3DkoIIz",
153+
"version": 3,
154+
"weekStart": ""
155+
}

docker/monitoring/prometheus/prometheus.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,16 @@ scrape_configs:
8585
action: drop
8686
static_configs:
8787
- targets:
88-
- docker_stats_exporter:9338
88+
- docker_stats_exporter:9338
89+
90+
- job_name: nginx_logs_exporter
91+
scheme: http
92+
metrics_path: /metrics
93+
metric_relabel_configs:
94+
# drop exporter internal metrics
95+
- source_labels: [ "__name__" ]
96+
regex: '(go|python|process)_.+'
97+
action: drop
98+
static_configs:
99+
- targets:
100+
- nginx_logs_exporter:4040

docker/webserver/nginx_templates/app.conf.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ log_format default '$remote_addr $host $remote_user [$time_local] "$request" '
55

66
access_log /var/log/nginx/access.log default;
77

8+
# create seperate logfile, since the access.log file is a symlink to stdout
9+
# it cannot be used by prometheus-nginxlog-exporter
10+
log_format nginx_logs_exporter '$remote_addr $host $remote_user [$time_local] $proxy_host "$request" '
11+
'$status $body_bytes_sent "$http_referer" '
12+
'"$http_user_agent" "$http_x_forwarded_for"';
13+
access_log /var/log/nginx/prometheus-nginxlog-exporter/access.log nginx_logs_exporter;
14+
815
# disable version in server banner
916
server_tokens off;
1017
# requires openresty

0 commit comments

Comments
 (0)