From b698699fdd90a1b9196c259fab069bdae80a5a27 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Sat, 12 Oct 2019 16:42:27 -0700 Subject: [PATCH 1/2] More helpful DNS failure message Previously if dns.lua failed to resolve a name you'd see the following in your logs: ``` 2019/10/12 23:39:34 [error] 41#41: *6474 [lua] dns.lua:121: dns_lookup(): failed to query the DNS server: server returned error code: 3: name error server returned error code: 3: name error, context: ngx.timer ``` Unfortunately this doesn't tell you what name is failing (so you have to start guessing). To alleviate the pain this simply adds the host name we are attempting to resolve to the log line so users don't have to guess. --- rootfs/etc/nginx/lua/util/dns.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rootfs/etc/nginx/lua/util/dns.lua b/rootfs/etc/nginx/lua/util/dns.lua index fcef837670..8ba5f3db1b 100644 --- a/rootfs/etc/nginx/lua/util/dns.lua +++ b/rootfs/etc/nginx/lua/util/dns.lua @@ -118,7 +118,7 @@ function _M.lookup(host) return addresses end - ngx_log(ngx_ERR, "failed to query the DNS server:\n" .. table_concat(dns_errors, "\n")) + ngx_log(ngx_ERR, "failed to query the DNS server for %s:\n%s", host, table_concat(dns_errors, "\n")) return { host } end @@ -147,7 +147,7 @@ function _M.lookup(host) end if #dns_errors > 0 then - ngx_log(ngx_ERR, "failed to query the DNS server:\n" .. table_concat(dns_errors, "\n")) + ngx_log(ngx_ERR, "failed to query the DNS server for %s:\n%s", host, table_concat(dns_errors, "\n")) end return { host } From 7fc442c7f11f0fe2a796fa6e7e7393850667cb1b Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Sat, 12 Oct 2019 20:03:11 -0700 Subject: [PATCH 2/2] update test cases --- rootfs/etc/nginx/lua/test/util/dns_test.lua | 13 ++++++------- rootfs/etc/nginx/lua/util/dns.lua | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/rootfs/etc/nginx/lua/test/util/dns_test.lua b/rootfs/etc/nginx/lua/test/util/dns_test.lua index 92df082d10..e753abe771 100644 --- a/rootfs/etc/nginx/lua/test/util/dns_test.lua +++ b/rootfs/etc/nginx/lua/test/util/dns_test.lua @@ -40,38 +40,37 @@ describe("dns.lookup", function() it("returns host when the query returns nil", function() helpers.mock_resty_dns_query(nil, nil, "oops!") assert.are.same({ "example.com" }, dns_lookup("example.com")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\noops!\noops!") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "example.com", ":\n", "oops!\noops!") end) it("returns host when the query returns empty answer", function() helpers.mock_resty_dns_query(nil, {}) assert.are.same({ "example.com" }, dns_lookup("example.com")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\nno A record resolved\nno AAAA record resolved") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "example.com", ":\n", "no A record resolved\nno AAAA record resolved") end) it("returns host when there's answer but with error", function() helpers.mock_resty_dns_query(nil, { errcode = 1, errstr = "format error" }) assert.are.same({ "example.com" }, dns_lookup("example.com")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\n" .. - "server returned error code: 1: format error\nserver returned error code: 1: format error") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "example.com", ":\n", "server returned error code: 1: format error\nserver returned error code: 1: format error") end) it("retuns host when there's answer but no A/AAAA record in it", function() helpers.mock_resty_dns_query(nil, { { name = "example.com", cname = "sub.example.com", ttl = 60 } }) assert.are.same({ "example.com" }, dns_lookup("example.com")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\nno A record resolved\nno AAAA record resolved") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "example.com", ":\n", "no A record resolved\nno AAAA record resolved") end) it("returns host when the query returns nil and number of dots is not less than configured ndots", function() helpers.mock_resty_dns_query(nil, nil, "oops!") assert.are.same({ "a.b.c.d.example.com" }, dns_lookup("a.b.c.d.example.com")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\noops!\noops!") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "a.b.c.d.example.com", ":\n", "oops!\noops!") end) it("returns host when the query returns nil for a fully qualified domain", function() helpers.mock_resty_dns_query("example.com.", nil, "oops!") assert.are.same({ "example.com." }, dns_lookup("example.com.")) - assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\noops!\noops!") + assert.spy(spy_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server for ", "example.com.", ":\n", "oops!\noops!") end) end) diff --git a/rootfs/etc/nginx/lua/util/dns.lua b/rootfs/etc/nginx/lua/util/dns.lua index 8ba5f3db1b..4c33228b92 100644 --- a/rootfs/etc/nginx/lua/util/dns.lua +++ b/rootfs/etc/nginx/lua/util/dns.lua @@ -118,7 +118,7 @@ function _M.lookup(host) return addresses end - ngx_log(ngx_ERR, "failed to query the DNS server for %s:\n%s", host, table_concat(dns_errors, "\n")) + ngx_log(ngx_ERR, "failed to query the DNS server for ", host, ":\n", table_concat(dns_errors, "\n")) return { host } end @@ -147,7 +147,7 @@ function _M.lookup(host) end if #dns_errors > 0 then - ngx_log(ngx_ERR, "failed to query the DNS server for %s:\n%s", host, table_concat(dns_errors, "\n")) + ngx_log(ngx_ERR, "failed to query the DNS server for ", host, ":\n", table_concat(dns_errors, "\n")) end return { host }