简体   繁体   中英

Metrics for nginx stream module

I'm trying to get metrics from nginx about the total connections when using the nginx stream module. I know there is stub_status option but it looks like its only for http and not stream.

I found the prometheus lua plugin which has stream, but I was not able to figure out the right conf for it. I only get the built-in error metric but not the metric defined. Here's the conf and the response. Whats also weird is that the response is in the header and not body.

stream {
   lua_shared_dict prometheus_metrics 10M;
   lua_package_path "/tmp/nginx-lua-prometheus/?.lua;/usr/local/openresty/luajit/lib/lua/5.1/?.lua;;";
   init_by_lua '
      prometheus = require("prometheus").init("prometheus_metrics")
      metric_requests = prometheus:counter("nginx_http_requests_total", "Number of HTTP requests", {"test"})
      metric_connections = prometheus:gauge("nginx_http_connections", "Number of HTTP connections", {"test"})';
   log_by_lua_block {
     metric_requests:inc(1, {"test"})
   }

server {
  listen 9145;
  content_by_lua '
    local sock = assert(ngx.req.socket(true))
    local data = sock:receive()
    local location = "GET /metrics"
    if string.sub(data, 1, string.len(location)) == location then
      ngx.say("HTTP/1.1 200 OK")
      ngx.say("Content-Type: text/plain")
      ngx.say("yo")
      ngx.say(table.concat(prometheus:metric_data(), ""))
    else
      ngx.say("HTTP/1.1 404 Not Found")
    end
  ';
  }
}

curl localhost:9145/metrics -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9145 (#0)
> GET /metrics HTTP/1.1
> Host: localhost:9145
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< # HELP nginx_metric_errors_total Number of nginx-lua-prometheus errors
< # TYPE nginx_metric_errors_total counter
< nginx_metric_errors_total 0
* no chunk, no close, no size. Assume close to signal end
<
* Curl_http_done: called premature == 0
* Closing connection 0

https://github.com/knyar/nginx-lua-prometheus/issues/77

Answered here

  log_by_lua_block {
    metric_connections:set(ngx.var.connections_active, {"active"})
    metric_connections:set(ngx.var.connections_reading, {"reading"})
    metric_connections:set(ngx.var.connections_writing, {"writing"})
    metric_connections:set(ngx.var.connections_waiting, {"waiting"})
    metric_requests:inc(1, {"test"})
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM