nginx流模块的度量

nginx流模块的度量,nginx,lua,prometheus,Nginx,Lua,Prometheus,我试图从nginx获取使用nginx流模块时总连接数的指标。我知道有stub_status选项,但它看起来只适用于http,而不适用于流 我找到了包含流的配置文件,但是我无法为它找到正确的配置文件。我只得到内置的错误度量,但没有定义度量。这是配置文件和响应。同样奇怪的是,响应出现在标题中,而不是正文中 stream { lua_shared_dict prometheus_metrics 10M; lua_package_path "/tmp/nginx-lua-prometheus

我试图从nginx获取使用nginx流模块时总连接数的指标。我知道有stub_status选项,但它看起来只适用于http,而不适用于流

我找到了包含流的配置文件,但是我无法为它找到正确的配置文件。我只得到内置的错误度量,但没有定义度量。这是配置文件和响应。同样奇怪的是,响应出现在标题中,而不是正文中

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
*正在尝试127.0.0.1。。。
*TCP_节点集
*已连接到本地主机(127.0.0.1)端口9145(#0)
>GET/metrics HTTP/1.1
>主机:localhost:9145
>用户代理:curl/7.52.1
>接受:*/*
>

回答这里

  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"})
  }
  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"})
  }