Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 954c915

Browse files
committed
Add debug logging for DNS
1 parent 61134a8 commit 954c915

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

‎localstack/dns/server.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import textwrap
77
import threading
8+
import time
89
from datetime import datetime
910
from functools import cache
1011
from ipaddress import IPv4Address, IPv4Interface
@@ -374,20 +375,37 @@ def resolve(self, request: DNSRecord, handler: DNSHandler) -> DNSRecord | None:
374375
reply = request.reply()
375376
found = False
376377

378+
LOG.debug(
379+
"[%s] received dns query: %s (type %s)",
380+
request.header.id,
381+
request.q.qname,
382+
QTYPE.get(request.q.qtype, "??"),
383+
)
384+
377385
try:
378386
if not self._skip_local_resolution(request):
379387
found = self._resolve_name(request, reply, handler.client_address)
380388
except Exception as e:
381389
LOG.info("Unable to get DNS result: %s", e)
382390

383391
if found:
392+
LOG.debug("[%s] found name locally", request.header.id)
384393
return reply
385394

395+
LOG.debug("[%s] name not found locally, querying upstream", request.header.id)
386396
# If we did not find a matching record in our local zones, we forward to our upstream dns
387397
try:
398+
start_time = time.perf_counter()
388399
req_parsed = dns.message.from_wire(bytes(request.pack()))
389400
r = dns.query.udp(req_parsed, self.upstream_dns, timeout=REQUEST_TIMEOUT_SECS)
390401
result = self._map_response_dnspython_to_dnslib(r)
402+
end_time = time.perf_counter()
403+
LOG.debug(
404+
"[%s] name resolved upstream in %d ms (rcode %s)",
405+
request.header.id,
406+
(end_time - start_time) / 1000.0,
407+
RCODE.get(result.header.rcode, "??"),
408+
)
391409
return result
392410
except Exception as e:
393411
LOG.info(
@@ -401,11 +419,12 @@ def resolve(self, request: DNSRecord, handler: DNSHandler) -> DNSRecord | None:
401419
if not reply.rr and reply.header.get_rcode == RCODE.NOERROR:
402420
# setting this return code will cause commands like 'host' to try the next nameserver
403421
reply.header.set_rcode(RCODE.SERVFAIL)
422+
LOG.debug("[%s] failed to resolve name", request.header.id)
404423
return None
405424

406425
return reply
407426

408-
def _skip_local_resolution(self, request) -> bool:
427+
def _skip_local_resolution(self, request: DNSRecord) -> bool:
409428
"""
410429
Check whether we should skip local resolution for the given request, and directly contact upstream
411430
@@ -415,6 +434,7 @@ def _skip_local_resolution(self, request) -> bool:
415434
request_name = to_str(str(request.q.qname))
416435
for p in self.skip_patterns:
417436
if re.match(p, request_name):
437+
LOG.debug("[%s] skipping local resolution of query", request.header.id)
418438
return True
419439
return False
420440

@@ -449,7 +469,7 @@ def _resolve_name(
449469
self, request: DNSRecord, reply: DNSRecord, client_address: ClientAddress
450470
) -> bool:
451471
if alias_found := self._resolve_alias(request, reply, client_address):
452-
LOG.debug("Alias found: %s", request.q.qname)
472+
LOG.debug("[%s] alias found: %s", request.header.id, request.q.qname)
453473
return alias_found
454474
return self._resolve_name_from_zones(request, reply, client_address)
455475

@@ -671,7 +691,7 @@ def do_run(self):
671691
self.servers = self._get_servers()
672692
for server in self.servers:
673693
server.start_thread()
674-
LOG.debug("DNS Server started")
694+
LOG.info("DNS Server started")
675695
for server in self.servers:
676696
server.thread.join()
677697

@@ -780,7 +800,7 @@ def get_available_dns_server():
780800
pass
781801

782802
if result:
783-
LOG.debug("Determined fallback dns: %s", result)
803+
LOG.info("Determined fallback dns: %s", result)
784804
else:
785805
LOG.info(
786806
"Unable to determine fallback DNS. Please check if '%s' is reachable by your configured DNS servers"
@@ -864,10 +884,10 @@ def start_server(upstream_dns: str, host: str, port: int = config.DNS_PORT):
864884

865885
if DNS_SERVER:
866886
# already started - bail
867-
LOG.debug("DNS servers are already started. Avoid starting again.")
887+
LOG.info("DNS servers are already started. Avoid starting again.")
868888
return
869889

870-
LOG.debug("Starting DNS servers (tcp/udp port %s on %s)..." % (port, host))
890+
LOG.info("Starting DNS servers (tcp/udp port %s on %s)..." % (port, host))
871891
dns_server = DnsServer(port, protocols=["tcp", "udp"], host=host, upstream_dns=upstream_dns)
872892

873893
for name in NAME_PATTERNS_POINTING_TO_LOCALSTACK:
@@ -904,7 +924,7 @@ def stop_servers():
904924

905925
def start_dns_server_as_sudo(port: int):
906926
global DNS_SERVER
907-
LOG.debug(
927+
LOG.info(
908928
"Starting the DNS on its privileged port (%s) needs root permissions. Trying to start DNS with sudo.",
909929
config.DNS_PORT,
910930
)
@@ -929,7 +949,7 @@ def start_dns_server(port: int, asynchronous: bool = False, standalone: bool = F
929949

930950
# check if DNS server is disabled
931951
if not config.use_custom_dns():
932-
LOG.debug("Not starting DNS. DNS_ADDRESS=%s", config.DNS_ADDRESS)
952+
LOG.info("Not starting DNS. DNS_ADDRESS=%s", config.DNS_ADDRESS)
933953
return
934954

935955
upstream_dns = get_fallback_dns_server()
@@ -949,7 +969,7 @@ def start_dns_server(port: int, asynchronous: bool = False, standalone: bool = F
949969
return
950970

951971
if standalone:
952-
LOG.debug("Already in standalone mode and port binding still fails.")
972+
LOG.warning("Already in standalone mode and port binding still fails.")
953973
return
954974

955975
start_dns_server_as_sudo(port)

0 commit comments

Comments
 (0)