-
Notifications
You must be signed in to change notification settings - Fork 0
feat(metrics): add lua script for prometheus metrics on latency #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Lua Module Path Mismatch Causes Runtime ErrorsThe Additional Locations (1) |
||
} | ||
|
||
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() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ package: | |
runtime: | ||
- nginx | ||
- nginx-mod-stream | ||
- lua-resty-core | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
- 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Prometheus Metrics Initialization Failure
The
/metrics
endpoint callsprometheus:collect()
without initializing alua-resty-prometheus
instance. This causes a runtime error and prevents it from accessing metrics stored in theprometheus_metrics
shared dictionary, which the stream context uses.