apisix
apisix copied to clipboard
help request: How to expose custom Prometheus metric with custom plugin
Description
How to expose a metric from custom plugin, using the default prometheus exported URI /apisix/prometheus/metrics
.
In my case, I want get summary metric about request_time, not the builtin histogram metric apisix_http_latency_bucket
. I don't know what is the best practice to achieve this.
Here is my try
local base_prometheus = require("prometheus")
local core = require("apisix.core")
local ngx = ngx
local clear_tab = core.table.clear
local plugin_name = "request-time-metric"
local prometheus = require("apisix.plugins.prometheus.exporter")
local custom_metrics = {}
local schema = {
type = "object",
properties = {},
}
local _M = {
version = 0.1,
priority = 40,
name = plugin_name,
schema = schema,
}
-- need modify ngx_tpl.lua to call this function, I don't think this is a better way
function _M.http_init()
if ngx.get_phase() ~= "init" and ngx.get_phase() ~= "init_worker" then
return
end
clear_tab(custom_metrics)
prometheus = base_prometheus.init("prometheus-metrics", "apisix_")
custom_metrics.test_counter = prometheus:counter("test_counter", "test", {"test"})
end
function _M.http_log(conf, ctx)
custom_metrics.test_counter:inc(1, "test")
end
function _M.log(conf, ctx)
-- print "null"
core.log.warn("metrics: ", core.json.encode(custom_metrics.test_counter, true))
end
return _M
Seems I need to modify ngx_tpl.lua
to call http_init
, but from docs, this is not recommend.
Is this the right way? Or should I just modify the builtin prometheus/exporter.lua plugin (is there will be a problem when upgrade apisix?) ?
Environment
- APISIX version (run
apisix version
): 3.9.1 - OpenResty / Nginx version (run
openresty -V
ornginx -V
): openresty/1.25.3.1