diff --git a/docker/nginx/nginx.conf b/docker/nginx/nginx.conf index 4a22467..88bc9ea 100644 --- a/docker/nginx/nginx.conf +++ b/docker/nginx/nginx.conf @@ -1,4 +1,5 @@ load_module "/usr/lib/nginx/modules/ngx_stream_module.so"; +load_module "/usr/lib/nginx/modules/ngx_lua_module.so"; worker_processes 1; @@ -26,6 +27,13 @@ http { return 404; } + location /metrics { + content_by_lua_block { + local prometheus = require "resty.prometheus" + prometheus:collect() + } + } + location /healthz { default_type text/plain; return 200 "OK\n"; @@ -34,6 +42,7 @@ http { } stream { + lua_shared_dict prometheus_metrics 10M; map_hash_bucket_size 128; map_hash_max_size 4096; @@ -47,10 +56,20 @@ stream { resolver kube-dns.kube-system.svc.cluster.local valid=30s; resolver_timeout 5s; + init_by_lua_block { + local prom = require "docker.nginx.prometheus" + prom.init() + } + server { listen 127.0.0.1:8443 proxy_protocol; ssl_preread on; proxy_pass $ssl_preread_server_name:443; proxy_protocol off; + + log_by_lua_block { + local prom = require "docker.nginx.prometheus" + prom.log_connect_time() + } } } diff --git a/docker/nginx/prometheus.lua b/docker/nginx/prometheus.lua new file mode 100644 index 0000000..d905f55 --- /dev/null +++ b/docker/nginx/prometheus.lua @@ -0,0 +1,24 @@ + local prometheus = require "resty.prometheus" + local metric_connections, metric_connect_time + + local function init() + prometheus = prometheus.init("prometheus_metrics") + metric_connections = prometheus:counter("nginx_stream_connections_total", "Total connections", {"upstream"}) + metric_connect_time = prometheus:histogram("nginx_stream_upstream_connect_seconds", "Upstream connect time", + {"upstream"}) + end + + local function log_connect_time() + local connect_time = tonumber(ngx.var.upstream_connect_time) + local upstream = ngx.var.upstream_addr or "unknown" + + if connect_time then + metric_connect_time:observe(connect_time / 1000, {upstream}) -- Convert ms to seconds + metric_connections:inc(1, {upstream}) + end + end + + return { + init = init, + log_connect_time = log_connect_time + } \ No newline at end of file diff --git a/melange/ggbridge.yaml b/melange/ggbridge.yaml index 25db9c8..db84067 100644 --- a/melange/ggbridge.yaml +++ b/melange/ggbridge.yaml @@ -10,6 +10,7 @@ package: runtime: - nginx - nginx-mod-stream + - lua-resty-core - wstunnel target-architecture: - aarch64 @@ -39,4 +40,5 @@ pipeline: install -dm755 "${{targets.destdir}}"/etc/ggbridge/tls install -m755 docker/scripts/run.sh "${{targets.destdir}}"/opt/ggbridge/run.sh install -m644 docker/nginx/nginx.conf "${{targets.destdir}}"/etc/ggbridge/nginx.conf + install -m644 docker/nginx/prometheus.lua "${{targets.destdir}}"/etc/ggbridge/prometheus.lua - uses: strip