Comparison

net/unbound.lua @ 11250:d1351683dfe5

net.unbound: Allow tracing individual queries with a logger per query
author Kim Alvefur <zash@zash.se>
date Tue, 05 Jan 2021 20:04:07 +0100
parent 11249:137c44d2714a
child 11252:ba335004ca60
comparison
equal deleted inserted replaced
11249:137c44d2714a 11250:d1351683dfe5
11 local s_format = string.format; 11 local s_format = string.format;
12 local s_lower = string.lower; 12 local s_lower = string.lower;
13 local s_upper = string.upper; 13 local s_upper = string.upper;
14 local noop = function() end; 14 local noop = function() end;
15 15
16 local log = require "util.logger".init("unbound"); 16 local logger = require "util.logger";
17 local log = logger.init("unbound");
17 local net_server = require "net.server"; 18 local net_server = require "net.server";
18 local libunbound = require"lunbound"; 19 local libunbound = require"lunbound";
19 local promise = require"util.promise"; 20 local promise = require"util.promise";
21 local new_id = require "util.id".medium;
20 22
21 local gettime = require"socket".gettime; 23 local gettime = require"socket".gettime;
22 local dns_utils = require"util.dns"; 24 local dns_utils = require"util.dns";
23 local classes, types, errors = dns_utils.classes, dns_utils.types, dns_utils.errors; 25 local classes, types, errors = dns_utils.classes, dns_utils.types, dns_utils.errors;
24 local parsers = dns_utils.parsers; 26 local parsers = dns_utils.parsers;
102 qtype = qtype and s_upper(qtype) or "A"; 104 qtype = qtype and s_upper(qtype) or "A";
103 qclass = qclass and s_upper(qclass) or "IN"; 105 qclass = qclass and s_upper(qclass) or "IN";
104 local ntype, nclass = types[qtype], classes[qclass]; 106 local ntype, nclass = types[qtype], classes[qclass];
105 local startedat = gettime(); 107 local startedat = gettime();
106 local ret; 108 local ret;
109 local log_query = logger.init("unbound.query"..new_id());
107 local function callback_wrapper(a, err) 110 local function callback_wrapper(a, err)
108 local gotdataat = gettime(); 111 local gotdataat = gettime();
109 waiting_queries[ret] = nil; 112 waiting_queries[ret] = nil;
110 if a then 113 if a then
111 prep_answer(a); 114 prep_answer(a);
112 log("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status, 115 log_query("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status,
113 a.secure and "Secure" or a.bogus or "Insecure", gotdataat - startedat); -- Insecure as in unsigned 116 a.secure and "Secure" or a.bogus or "Insecure", gotdataat - startedat); -- Insecure as in unsigned
114 else 117 else
115 log("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err)); 118 log_query("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err));
116 end 119 end
117 local ok, cerr = pcall(callback, a, err); 120 local ok, cerr = pcall(callback, a, err);
118 if not ok then log("error", "Error in callback: %s", cerr); end 121 if not ok then log_query("error", "Error in callback: %s", cerr); end
119 end 122 end
120 log("debug", "Resolve %s %s %s", qname, qclass, qtype); 123 log_query("debug", "Resolve %s %s %s", qname, qclass, qtype);
121 local err; 124 local err;
122 ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass); 125 ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass);
123 if ret then 126 if ret then
124 waiting_queries[ret] = callback; 127 waiting_queries[ret] = callback;
125 else 128 else
126 log("warn", "Resolver error: %s", err); 129 log_query("warn", "Resolver error: %s", err);
127 end 130 end
128 return ret, err; 131 return ret, err;
129 end 132 end
130 133
131 local function lookup_sync(qname, qtype, qclass) 134 local function lookup_sync(qname, qtype, qclass)