Adding Custom metrics #8270
-
I'm trying to add additional prometheus metrics from a plugin. I'm close, but its generally complaining about the metrics being uninitialized local exporter = require('kong.plugins.prometheus.exporter')
local kong = kong
local table_new = kong.table.new
local get_prometheus = exporter.get_prometheus
local register = table_new(0, 3)
local function init()
local prometheus = get_prometheus()
register.kong_request_ratelimit_reached = prometheus:counter(
'kong_request_rate_limit_reached'
, 'total request that have reached a rate limit threshold'
, {'service', 'route', 'type'}
)
return register
end
return {
init = init
, register = register
} and my plugin is doing this local Plugin = {Priority = 900}
function Plugin:init_worker(conf)
metrics.init()
end When I run a test on it (pongo) I get this 2022/01/07 17:26:03 [error] 90#0: *2 [lua] prometheus.lua:839: log_error(): counter not initialized! Have you called Prometheus:init() from the init_worker_by_lua_block nginx phase?, context: ngx.timer From what I can tell, Prometheus machinery is loaded. In the same test I hit the metrics endpoint and dump the out put. It is all there. The test may be doing something wrong. but is this a thing that is possible? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
This is possible, generally doing what was described. I was trying to require the counter instance directly in the test and increment it, but it generally does not like that. You have to load the plugin + start kong using the test helpers and send a request. That seems to work just fine |
Beta Was this translation helpful? Give feedback.
-
Could you tell me how to solve it in detail? I have also met this problem with you at present, and I have been unable to define metrics, thank you very much |
Beta Was this translation helpful? Give feedback.
-
Can someone share the official documentation to add custom metrics on custom plugins? |
Beta Was this translation helpful? Give feedback.
-
For everyone who came here from google, searching for a simple solution: This exports a new counter-metric Hint: To pass values between plugins, you can use schema.luareturn {
name = "my-metrics",
fields = { },
}
handler.lualocal exporter = require('kong.plugins.prometheus.exporter')
local metrics = {}
local prometheus
local my_labels = { 0, 0 } -- one for every label
local MyMetricsHandler = {
NAME = 'my-metrics',
VERSION = "1.0.0",
PRIORITY = 15,
}
function MyMetricsHandler:init_worker()
prometheus = exporter.get_prometheus()
metrics.my = prometheus:counter("my_requests_total", -- kong_ prefix is already set global
"my labeled request information",
{"label1", "label2"})
end
function MyMetricsHandler:log(conf)
my_labels[1] = "content of label1"
my_labels[2] = "content of label2"
metrics.my:inc(1, my_labels)
end
return MyMetricsHandler |
Beta Was this translation helpful? Give feedback.
Sure. The thing to keep in mind about these kong plugins, is that "its just lua", and as long as you play by the rules of the kong PDK - anything goes.
Basically what I did was separate each phase handler into separate lua modules
in
handler.lua
where the plugin class lives, you initialize your metrics lib, and pass it to your phase functions in addition to the plugin config