From: Marek VavruĊĦa Date: Tue, 27 Mar 2018 03:18:56 +0000 (-0700) Subject: http/prometheus: allow finalization of metrics table X-Git-Tag: v2.4.0~30^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34878d5ec44a10cf93abe5665b1189178d282674;p=thirdparty%2Fknot-resolver.git http/prometheus: allow finalization of metrics table This allows other modules to add or modify custom metrics or labels. --- diff --git a/modules/http/README.rst b/modules/http/README.rst index 4798010a1..806e07f19 100644 --- a/modules/http/README.rst +++ b/modules/http/README.rst @@ -120,6 +120,19 @@ You can namespace the metrics in configuration, using `http.prometheus.namespace -- Set Prometheus namespace http.prometheus.namespace = 'resolver_' +You can also add custom metrics or rewrite existing metrics before they are returned to Prometheus client. + +.. code-block:: lua + + http = { + host = 'localhost', + } + + -- Add an arbitrary metric to Prometheus + http.prometheus.finalize = function (metrics) + table.insert(metrics, 'build_info{version="1.2.3"} 1') + end + Tracing requests ^^^^^^^^^^^^^^^^ diff --git a/modules/http/prometheus.lua b/modules/http/prometheus.lua index 898590efb..cf76ef4cf 100644 --- a/modules/http/prometheus.lua +++ b/modules/http/prometheus.lua @@ -1,6 +1,7 @@ -- Module implementation local M = { namespace = '', + finalize = function (_ --[[metrics]]) end, } local snapshots, snapshots_count = {}, 120 @@ -150,6 +151,10 @@ local function serve_prometheus() end table.insert(render, string.format('%slatency_count %f', M.namespace, count)) table.insert(render, string.format('%slatency_sum %f', M.namespace, sum)) + -- Finalize metrics table before rendering + if type(M.finalize) == 'function' then + M.finalize(render) + end return table.concat(render, '\n') .. '\n' end