You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "br41n10 (via GitHub)" <gi...@apache.org> on 2024/06/14 05:08:24 UTC

[I] help request: How to expose custom Prometheus metric with custom plugin [apisix]

br41n10 opened a new issue, #11352:
URL: https://github.com/apache/apisix/issues/11352

   ### 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`.
   
   Here is a example plugin code
   
   ``` lua
   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
   
   ```
   
   
   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` or `nginx -V`): openresty/1.25.3.1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to expose custom Prometheus metric with custom plugin [apisix]

Posted by "wklken (via GitHub)" <gi...@apache.org>.
wklken commented on issue #11352:
URL: https://github.com/apache/apisix/issues/11352#issuecomment-2421648458

   just do it in the plugin:
   
   ```lua
   local exporter = require("apisix.plugins.prometheus.exporter")
   
   local metric_api_requests_total
   
   function _M.init()
       prometheus_registry = exporter.get_prometheus()
   
       -- registers  metrics.
       metric_api_requests_total = prometheus_registry:counter(
           "api_requests_total",
           "How many HTTP requests processed, partitioned by status code, method and HTTP path.", {
               "label1",
               "label2",
               ......
           }
       )
   end
   
   
   function _M.log(conf, ctx)
       metric_api_requests_total:inc(
           1, {
               label_value_1,
               label_value_2,
               ......
           }
       )
   end
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org