Skip to content

Commit f1b0dd6

Browse files
author
Frederic Spiers
committed
feat(metrics): add lua script for prometheus metrics on latency
1 parent cee71ec commit f1b0dd6

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

docker/nginx/nginx.conf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load_module "/usr/lib/nginx/modules/ngx_stream_module.so";
2+
load_module "/usr/lib/nginx/modules/ngx_lua_module.so";
23

34
worker_processes 1;
45

@@ -26,6 +27,13 @@ http {
2627
return 404;
2728
}
2829

30+
location /metrics {
31+
content_by_lua_block {
32+
local prometheus = require "resty.prometheus"
33+
prometheus:collect()
34+
}
35+
}
36+
2937
location /healthz {
3038
default_type text/plain;
3139
return 200 "OK\n";
@@ -34,6 +42,7 @@ http {
3442
}
3543

3644
stream {
45+
lua_shared_dict prometheus_metrics 10M;
3746
map_hash_bucket_size 128;
3847
map_hash_max_size 4096;
3948

@@ -47,10 +56,20 @@ stream {
4756
resolver kube-dns.kube-system.svc.cluster.local valid=30s;
4857
resolver_timeout 5s;
4958

59+
init_by_lua_block {
60+
local prom = require "docker.nginx.prometheus"
61+
prom.init()
62+
}
63+
5064
server {
5165
listen 127.0.0.1:8443 proxy_protocol;
5266
ssl_preread on;
5367
proxy_pass $ssl_preread_server_name:443;
5468
proxy_protocol off;
69+
70+
log_by_lua_block {
71+
local prom = require "docker.nginx.prometheus"
72+
prom.log_connect_time()
73+
}
5574
}
5675
}

docker/nginx/prometheus.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local prometheus = require "resty.prometheus"
2+
local metric_connections, metric_connect_time
3+
4+
local function init()
5+
prometheus = prometheus.init("prometheus_metrics")
6+
metric_connections = prometheus:counter("nginx_stream_connections_total", "Total connections", {"upstream"})
7+
metric_connect_time = prometheus:histogram("nginx_stream_upstream_connect_seconds", "Upstream connect time",
8+
{"upstream"})
9+
end
10+
11+
local function log_connect_time()
12+
local connect_time = tonumber(ngx.var.upstream_connect_time)
13+
local upstream = ngx.var.upstream_addr or "unknown"
14+
15+
if connect_time then
16+
metric_connect_time:observe(connect_time / 1000, {upstream}) -- Convert ms to seconds
17+
metric_connections:inc(1, {upstream})
18+
end
19+
end
20+
21+
return {
22+
init = init,
23+
log_connect_time = log_connect_time
24+
}

melange/ggbridge.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package:
1010
runtime:
1111
- nginx
1212
- nginx-mod-stream
13+
- nginx-mod-lua
14+
- lua5.1-resty-core
1315
- wstunnel
1416
target-architecture:
1517
- aarch64
@@ -39,4 +41,5 @@ pipeline:
3941
install -dm755 "${{targets.destdir}}"/etc/ggbridge/tls
4042
install -m755 docker/scripts/run.sh "${{targets.destdir}}"/opt/ggbridge/run.sh
4143
install -m644 docker/nginx/nginx.conf "${{targets.destdir}}"/etc/ggbridge/nginx.conf
44+
install -m644 docker/nginx/prometheus.lua "${{targets.destdir}}"/etc/ggbridge/prometheus.lua
4245
- uses: strip

0 commit comments

Comments
 (0)