dns_exporter.exporter

dns_exporter.exporter contains the DNSExporter class.

The config.py module contains configuration related stuff, metrics.py contains the metric definitions, collector.py has the Collector and and this exporter.py module contains most of the rest of the code.

Made with love by Thomas Steen Rasmussen/Tykling, 2023.

class dns_exporter.exporter.DNSExporter(request, client_address, server)

Primary dns_exporter class.

MetricsHandler subclass for incoming scrape requests. Initiated on each request as a handler by http.server.HTTPServer().

The configure() classmethod can optionally be called to load modules before use.

Attributes:

modules: A dict of dns_exporter.config.Config instances to be used in scrape requests.

build_final_config(qs: dict[str, str]) None

Construct the final effective scrape config from defaults and values from the querystring.

static check_ip_family(ip: IPv4Address | IPv6Address, family: str) bool

Make sure the IP matches the address family.

classmethod configure(modules: dict[str, ConfigDict] | None = None) bool

Validate and create Config objects.

Takes a dict of ConfigDict objects and runs cls.prepare_config() on each before creating a Config object and adding it to cls.modules

If an error is encountered the process stops, but modules loaded until the failure can still be used in cls.modules.

Args:

modules: A dict of names and corresponding ConfigDict objects.

Returns:

bool: True if all ConfigDict objects was validated and loaded OK, False

if an error was encountered.

do_GET() None

Handle incoming HTTP GET requests.

static handle_failure(fail_registry: CollectorRegistry, failure: str, labels: dict[str, str]) None

Handle various failure cases that can occur before the DNSCollector is called.

handle_query_request() None

Handle incoming HTTP GET requests to /query or /config.

parse_querystring() tuple[SplitResult, dict[str, str]]

Parse the incoming url and then the querystring.

static parse_server(server: str, protocol: str) SplitResult

Parse the server, add scheme (to make urllib.parse play ball), make port explicit.

The server at this point can be:
  • a v4 IP

  • a v6 IP

  • a v4 ip:port

  • a v6 ip:port

  • a hostname

  • a hostname:port

  • a https:// url with an IP and no port

  • a https:// url with an IP:port

  • a https:// url with a hostname and no port

  • a https:// url with a hostname:port

In the DoH https:// cases the url can be with or without a path.

Parse it with urllib.parse.urlsplit, add explicit port if needed, and return the result.

classmethod prepare_config(config: ConfigDict) ConfigDict

Make sure the configdict has the right types and objects.

This method is called from:
  • DNSExporter.configure() (before class initialisation, optional)

  • During each scrape request

Args:

config: A ConfigDict instance

Returns:

A ConfigDict instance

Raises:

ConfigError: If any issues are found with the ConfigDict

classmethod prepare_config_bools(config: ConfigDict) ConfigDict

Parse and create bool objects for the config.

classmethod prepare_config_floats(config: ConfigDict) ConfigDict

Parse and create float objects for the config.

classmethod prepare_config_integers(config: ConfigDict) ConfigDict

Parse and create integer objects for the config.

classmethod prepare_config_ip(config: ConfigDict) ConfigDict

Parse IP address if needed.

classmethod prepare_config_proxy(config: ConfigDict) ConfigDict

Parse proxy into a SplitResult and return it.

classmethod prepare_config_rfvalidator(config: ConfigDict) ConfigDict

Parse and create RFValidator object for the config.

classmethod prepare_config_rrvalidators(config: ConfigDict) ConfigDict

Parse and create RRValidator objects for the config.

classmethod prepare_config_server(config: ConfigDict) ConfigDict

Parse server into a SplitResult and return it.

resolve_ip_getaddrinfo(hostname: str, family: str) str

Resolve the IP of a DNS server hostname.

send_metric_response(registry: CollectorRegistry | RestrictedRegistry, query: dict[str, str]) None

Bake and send output from the provided registry and querystring.

validate_config() None

Validate various aspects of the config.

validate_server_ip() None

Validate the server and resolve IP if needed.