knyar / nginx-lua-prometheus

Prometheus metric library for Nginx written in Lua
MIT License
1.46k stars 231 forks source link

failed to run log_by_lua*: log_by_lua attempt to index global 'metric_requests' (a nil value) | v2 #102

Closed theonedemon closed 4 years ago

theonedemon commented 4 years ago

errors after add lua_code_cache off

 failed to run log_by_lua*: log_by_lua(nginx.conf:70):2: attempt to index global 'metric_requests' (a nil value)

example config:

http {
 lua_code_cache off; # this settig broken plugin

# prometheus
    lua_shared_dict prometheus_metrics 10M;
    lua_package_path "/nginx-lua-prometheus/?.lua;;";
    init_worker_by_lua_block {
        prometheus = require("prometheus").init("prometheus_metrics")
        metric_requests = prometheus:counter("nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
        metric_latency = prometheus:histogram("nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
        metric_connections = prometheus:gauge("nginx_http_connections", "Number of HTTP connections", {"state"})
    }
    log_by_lua_block {
        metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
        metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
    }
}
knyar commented 4 years ago

Currently this module requires code cache to be enabled. I believe this is documented in README.