Mercurial > prosody-hg
comparison plugins/mod_http_openmetrics.lua @ 11929:85d51bfcf56b
mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
This version has several changes from the earlier mod_prometheus:
- Conversion of metrics into the text-based OpenMetrics format is moved to
util.openmetrics
- Support for IP-based access control
- Compatibility with earlier Prosody versions removed
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 24 Nov 2021 16:03:05 +0000 |
| parents | |
| children | b4f77a7bf8ab |
comparison
equal
deleted
inserted
replaced
| 11928:16cf863b36c0 | 11929:85d51bfcf56b |
|---|---|
| 1 -- Export statistics in OpenMetrics format | |
| 2 -- | |
| 3 -- Copyright (C) 2014 Daurnimator | |
| 4 -- Copyright (C) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
| 5 -- Copyright (C) 2021 Jonas Schäfer <jonas@zombofant.net> | |
| 6 -- | |
| 7 -- This module is MIT/X11 licensed. | |
| 8 | |
| 9 module:set_global(); | |
| 10 | |
| 11 local statsman = require "core.statsmanager"; | |
| 12 local ip = require "util.ip"; | |
| 13 | |
| 14 local get_metric_registry = statsman.get_metric_registry; | |
| 15 local collect = statsman.collect; | |
| 16 | |
| 17 local get_metrics; | |
| 18 | |
| 19 local permitted_ips = module:get_option_set("openmetrics_allow_ips", { "::1", "127.0.0.1" }); | |
| 20 local permitted_cidr = module:get_option_string("openmetrics_allow_cidr"); | |
| 21 | |
| 22 local function is_permitted(request) | |
| 23 local ip_raw = request.ip; | |
| 24 if permitted_ips:contains(ip_raw) or | |
| 25 (permitted_cidr and ip.match(ip.new_ip(ip_raw), ip.parse_cidr(permitted_cidr))) then | |
| 26 return true; | |
| 27 end | |
| 28 return false; | |
| 29 end | |
| 30 | |
| 31 function get_metrics(event) | |
| 32 if not is_permitted(event.request) then | |
| 33 return 403; -- Forbidden | |
| 34 end | |
| 35 | |
| 36 local response = event.response; | |
| 37 response.headers.content_type = "application/openmetrics-text; version=0.0.4"; | |
| 38 | |
| 39 if collect then | |
| 40 -- Ensure to get up-to-date samples when running in manual mode | |
| 41 collect() | |
| 42 end | |
| 43 | |
| 44 local registry = get_metric_registry() | |
| 45 if registry == nil then | |
| 46 response.headers.content_type = "text/plain; charset=utf-8" | |
| 47 response.status_code = 404 | |
| 48 return "No statistics provider configured\n" | |
| 49 end | |
| 50 | |
| 51 return registry:render(); | |
| 52 end | |
| 53 | |
| 54 function module.add_host(module) | |
| 55 module:depends "http"; | |
| 56 module:provides("http", { | |
| 57 default_path = "metrics"; | |
| 58 route = { | |
| 59 GET = get_metrics; | |
| 60 }; | |
| 61 }); | |
| 62 end |
