Software /
code /
prosody
Changeset
13659:5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Nice to have this in OpenMetricts instead of debug logs
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 07 Feb 2025 09:22:52 +0100 |
parents | 13658:33806569d7c5 |
children | 13660:31f8fd2bcab9 |
files | net/adns.lua net/unbound.lua util/startup.lua |
diffstat | 3 files changed, 34 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/net/adns.lua Thu Feb 06 17:37:59 2025 +0000 +++ b/net/adns.lua Fri Feb 07 09:22:52 2025 +0100 @@ -74,11 +74,17 @@ return handler; end +local function measure(_qclass, _qtype) + return measure; +end + function async_resolver_methods:lookup(handler, qname, qtype, qclass) local resolver = self._resolver; + local m = measure(qclass or "IN", qtype or "A"); return coroutine.wrap(function (peek) if peek then log("debug", "Records for %s already cached, using those...", qname); + m(); handler(peek); return; end @@ -89,6 +95,7 @@ log("debug", "Reply for %s (%s)", qname, coroutine.running()); end if ok then + m(); ok, err = pcall(handler, resolver:peek(qname, qtype, qclass)); else log("error", "Error sending DNS query: %s", err); @@ -129,4 +136,5 @@ end; resolver = new_async_resolver; new_async_socket = new_async_socket; + instrument = function(measure_) measure = measure_; end; };
--- a/net/unbound.lua Thu Feb 06 17:37:59 2025 +0000 +++ b/net/unbound.lua Fri Feb 07 09:22:52 2025 +0100 @@ -20,7 +20,6 @@ local promise = require"prosody.util.promise"; local new_id = require "prosody.util.id".short; -local gettime = require"socket".gettime; local dns_utils = require"prosody.util.dns"; local classes, types, errors = dns_utils.classes, dns_utils.types, dns_utils.errors; local parsers = dns_utils.parsers; @@ -116,21 +115,26 @@ return setmetatable(a, answer_mt); end +local function measure(_qclass, _qtype) + return measure; +end + local function lookup(callback, qname, qtype, qclass) if not unbound then initialize(); end qtype = qtype and s_upper(qtype) or "A"; qclass = qclass and s_upper(qclass) or "IN"; local ntype, nclass = types[qtype], classes[qclass]; - local startedat = gettime(); + + local m; local ret; local log_query = logger.init("unbound.query"..new_id()); local function callback_wrapper(a, err) - local gotdataat = gettime(); + m(); waiting_queries[ret] = nil; if a then prep_answer(a); - log_query("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status, - a.secure and "Secure" or a.bogus or "Insecure", gotdataat - startedat); -- Insecure as in unsigned + log_query("debug", "Results for %s %s %s: %s (%s)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status, + a.secure and "Secure" or a.bogus or "Insecure"); -- Insecure as in unsigned else log_query("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err)); end @@ -138,6 +142,7 @@ if not ok then log_query("error", "Error in callback: %s", cerr); end end log_query("debug", "Resolve %s %s %s", qname, qclass, qtype); + m = measure(qclass, qtype); local err; ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass); if ret then @@ -225,6 +230,8 @@ }; } +_M.instrument = function(measure_) measure = measure_; end; + function _M.resolver() return wrapper; end return _M;
--- a/util/startup.lua Thu Feb 06 17:37:59 2025 +0000 +++ b/util/startup.lua Fri Feb 07 09:22:52 2025 +0100 @@ -425,6 +425,19 @@ async.set_schedule_function(timer.add_task); end +function startup.instrument() + local statsmanager = require "prosody.core.statsmanager"; + local timed = require"prosody.util.openmetrics".timed; + + local adns = require "prosody.net.adns"; + if adns.instrument then + local m = statsmanager.metric("histogram", "prosody_dns", "seconds", "DNS lookups", { "qclass"; "qtype" }, { + buckets = { 1 / 1024; 1 / 256; 1 / 64; 1 / 16; 1 / 4; 1; 4 }; + }); + adns.instrument(function(qclass, qtype) return timed(m:with_labels(qclass, qtype)); end); + end +end + function startup.init_data_store() require "prosody.core.storagemanager"; end @@ -922,6 +935,7 @@ startup.load_secondary_libraries(); startup.init_promise(); startup.init_async(); + startup.instrument(); startup.init_http_client(); startup.init_data_store(); startup.init_global_protection();