在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):knyar/nginx-lua-prometheus开源软件地址(OpenSource Url):https://github.com/knyar/nginx-lua-prometheus开源编程语言(OpenSource Language):Lua 88.6%开源软件介绍(OpenSource Introduction):Prometheus metric library for NginxThis is a Lua library that can be used with Nginx to keep track of metrics and expose them on a separate web page to be pulled by Prometheus. InstallationTo use this library, you will need the ngx_lua nginx module. You can either use
a lua-enabled nginx-based server like OpenResty,
or a regulal nginx server with the module enabled: for example, on Debian 10 you
can simply install The library file - OpenResty users will find this library in opm. It is also available via luarocks. Quick start guideTo track request latency broken down by server name and request count
broken down by server name and status, add the following to the lua_shared_dict prometheus_metrics 10M;
lua_package_path "/path/to/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})
} This:
Last step is to configure a separate server that will expose the metrics. Please make sure to only make it reachable from your Prometheus server: server {
listen 9145;
allow 192.168.0.0/16;
deny all;
location /metrics {
content_by_lua_block {
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
}
}
} Metrics will be available at API referenceinit()syntax: require("prometheus").init(dict_name, [options]]) Initializes the module. This should be called once from the init_worker_by_lua_block section of nginx configuration.
Returns a Example: init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics", {sync_interval=3})
} prometheus:counter()syntax: prometheus:counter(name, description, label_names) Registers a counter. Should be called once for each counter from the init_worker_by_lua_block section.
Naming section of Prometheus documentation provides good guidelines on choosing metric and label names. Returns a Example: init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_bytes = prometheus:counter(
"nginx_http_request_size_bytes", "Total size of incoming requests")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
} prometheus:gauge()syntax: prometheus:gauge(name, description, label_names) Registers a gauge. Should be called once for each gauge from the init_worker_by_lua_block section.
Returns a Example: init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
} prometheus:histogram()syntax: prometheus:histogram(name, description, label_names, buckets) Registers a histogram. Should be called once for each histogram from the init_worker_by_lua_block section.
Returns a Example: init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_response_sizes = prometheus:histogram(
"nginx_http_response_size_bytes", "Size of HTTP responses", nil,
{10,100,1000,10000,100000,1000000})
} prometheus:collect()syntax: prometheus:collect() Presents all metrics in a text format compatible with Prometheus. This should be called in content_by_lua_block to expose the metrics on a separate HTTP page. Example: location /metrics {
content_by_lua_block { prometheus:collect() }
allow 192.168.0.0/16;
deny all;
} prometheus:metric_data()syntax: prometheus:metric_data() Returns metric data as an array of strings. counter:inc()syntax: counter:inc(value, label_values) Increments a previously registered counter. This is usually called from log_by_lua_block globally or per server/location.
The number of label values should match the number of label names defined when
the counter was registered using Example: log_by_lua_block {
metric_bytes:inc(tonumber(ngx.var.request_length))
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
} counter:del()syntax: counter:del(label_values) Delete a previously registered counter. This is usually called when you don't
need to observe such counter (or a metric with specific label values in this
counter) any more. If this counter has labels, you have to pass
The number of label values should match the number of label names defined when
the counter was registered using This function will wait for counter:reset()syntax: counter:reset() Delete all metrics for a previously registered counter. If this counter have no
labels, it is just the same as This function will wait for gauge:set()syntax: gauge:set(value, label_values) Sets the current value of a previously registered gauge. This could be called
from log_by_lua_block
globally or per server/location to modify a gauge on each request, or from
content_by_lua_block
just before
gauge:inc()syntax: gauge:inc(value, label_values) Increments or decrements a previously registered gauge. This is usually called when you want to observe the real-time value of a metric that can both be increased and decreased.
The number of label values should match the number of label names defined when
the gauge was registered using gauge:del()syntax: gauge:del(label_values) Delete a previously registered gauge. This is usually called when you don't
need to observe such gauge (or a metric with specific label values in this
gauge) any more. If this gauge has labels, you have to pass
The number of label values should match the number of label names defined when
the gauge was registered using gauge:reset()syntax: gauge:reset() Delete all metrics for a previously registered gauge. If this gauge have no
labels, it is just the same as histogram:observe()syntax: histogram:observe(value, label_values) Records a value in a previously registered histogram. Usually called from log_by_lua_block globally or per server/location.
Example: log_by_lua_block {
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
metric_response_sizes:observe(tonumber(ngx.var.bytes_sent))
} histogram:reset()syntax: histogram:reset() Delete all metrics for a previously registered histogram. This function will wait for Built-in metricsThe module increments an error metric called CaveatsUsage in stream moduleFor now, there is no way to share a dictionary between HTTP and Stream modules in Nginx. If you are using this library to collect metrics from stream module, you will need to configure a separate endpoint to return them. Here's an example. server {
listen 9145;
content_by_lua_block {
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("")
ngx.say(table.concat(prometheus:metric_data(), ""))
else
ngx.say("HTTP/1.1 404 Not Found")
end
}
} Known issueslibnginx-mod-http-lua broken in some Debian and Ubuntu versionsNote that recent stable versions of Debian and Ubuntu are known to package
The following versions of Debian and Ubuntu have been known to have this issue: TroubleshootingMake sure that nginx lua module is enabledIf you experience problems indicating that nginx doesn't know how to interpret
lua scripts, please make sure that the lua
module is enabled. You might
need something like this in your load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so; Keep lua code cache enabledThis module expects the
lua_code_cache
option to be Try using an older version of the libraryIf you are seeing library initialization errors, followed by errors for each
metric change request (e.g. attempt to index global '...' (a nil value)),
you are probably using an old version of lua-nginx-module. For example, this
will happen if you try using the latest version of this library with the
If you cannot upgrade nginx and lua-nginx-module, you can try using an older version of this library; it will not have the latest performance optimizations, but will still be functional. The recommended older release to use is 0.20181120. DevelopmentInstall dependencies for testing
Run tests
Releasing new version
Credits
LicenseLicensed under MIT license. Third Party LicenseFollowing third party modules are used in this library: This module is licensed under the Apache 2.0 license. Copyright (C) 2019, Kong Inc. All rights reserved. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论