]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
http/prometheus: allow finalization of metrics table
authorMarek Vavruša <mvavrusa@cloudflare.com>
Tue, 27 Mar 2018 03:18:56 +0000 (20:18 -0700)
committerGrigorii Demidov <grigorii.demidov@nic.cz>
Thu, 31 May 2018 14:54:10 +0000 (16:54 +0200)
This allows other modules to add or modify custom metrics or labels.

modules/http/README.rst
modules/http/prometheus.lua

index 4798010a183bcc6a3bb2ab4fd938aa511c083bdd..806e07f197fb66cab11667470c5d3c8335d9c070 100644 (file)
@@ -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
 ^^^^^^^^^^^^^^^^
 
index 898590efbd18828458ea7bcd84c7b4f33566166c..cf76ef4cf572795fdd25a3adc2bf15d5c7274fdb 100644 (file)
@@ -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