]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
http/prometheus: allow custom namespaces
authorMarek Vavruša <mvavrusa@cloudflare.com>
Fri, 23 Mar 2018 18:20:36 +0000 (11:20 -0700)
committerGrigorii Demidov <grigorii.demidov@nic.cz>
Thu, 31 May 2018 14:54:10 +0000 (16:54 +0200)
modules/http/README.rst
modules/http/prometheus.lua

index df21f5c4e3128c31fd9ba451df2b57103475774c..4798010a183bcc6a3bb2ab4fd938aa511c083bdd 100644 (file)
@@ -109,6 +109,17 @@ You can use it out of the box:
        latency_count 2.000000
        latency_sum 11.000000
 
+You can namespace the metrics in configuration, using `http.prometheus.namespace` attribute:
+
+.. code-block:: lua
+
+       http = {
+               host = 'localhost',
+       }
+
+       -- Set Prometheus namespace
+       http.prometheus.namespace = 'resolver_'
+
 Tracing requests
 ^^^^^^^^^^^^^^^^
 
index a6d0cd044f217444b230b1273486be92601f9663..898590efbd18828458ea7bcd84c7b4f33566166c 100644 (file)
@@ -1,3 +1,8 @@
+-- Module implementation
+local M = {
+       namespace = '',
+}
+
 local snapshots, snapshots_count = {}, 120
 
 -- Gauge metrics
@@ -126,33 +131,35 @@ local function serve_prometheus()
                elseif k == 'answer_slow' then
                        table.insert(latency, {'+Inf', v})
                -- Counter as a fallback
-               else table.insert(render, string.format(counter, k, k, v)) end
+               else
+                       local key = M.namespace .. k
+                       table.insert(render, string.format(counter, key, key, v))
+               end
        end
        -- Fill in latency histogram
        local function kweight(x) return tonumber(x) or math.huge end
        table.sort(latency, function (a,b) return kweight(a[1]) < kweight(b[1]) end)
-       table.insert(render, '# TYPE latency histogram')
+       table.insert(render, string.format('# TYPE %slatency histogram', M.namespace))
        local count, sum = 0.0, 0.0
        for _,e in ipairs(latency) do
                -- The information about the %Inf bin is lost, so we treat it
                -- as a timeout (3000ms) for metrics purposes
                count = count + e[2]
                sum = sum + e[2] * (math.min(tonumber(e[1]), 3000.0))
-               table.insert(render, string.format('latency_bucket{le="%s"} %f', e[1], count))
+               table.insert(render, string.format('%slatency_bucket{le="%s"} %f', M.namespace, e[1], count))
        end
-       table.insert(render, string.format('latency_count %f', count))
-       table.insert(render, string.format('latency_sum %f', sum))
+       table.insert(render, string.format('%slatency_count %f', M.namespace, count))
+       table.insert(render, string.format('%slatency_sum %f', M.namespace, sum))
        return table.concat(render, '\n') .. '\n'
 end
 
--- Export endpoints
-return {
-       init = snapshot_start,
-       deinit = snapshot_end,
-       gauges = gauges,
-       endpoints = {
-               ['/stats']     = {'application/json', getstats, stream_stats},
-               ['/frequent']  = {'application/json', function () return stats.frequent() end},
-               ['/metrics']   = {'text/plain; version=0.0.4', serve_prometheus},
-       }
-}
\ No newline at end of file
+-- Export module interface
+M.init = snapshot_start
+M.deinit = snapshot_end
+M.endpoints = {
+       ['/stats']     = {'application/json', getstats, stream_stats},
+       ['/frequent']  = {'application/json', function () return stats.frequent() end},
+       ['/metrics']   = {'text/plain; version=0.0.4', serve_prometheus},
+}
+
+return M
\ No newline at end of file