Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f4d8848
discovery/dns: add DnsSdConfig, ResolvConf, and target group mapping
bwplotka Nov 10, 2026
73ef6cc
discovery/dns: add async UDP/EDNS0 and TCP wire resolver
bwplotka Nov 10, 2026
b50d284
discovery/dns: accept null fields in serde and reject empty record type
bwplotka Nov 13, 2026
eee7ca3
discovery/dns: make internal wire and target group helpers private
bwplotka Nov 13, 2026
6c5e0bb
discovery/dns: reject embedded dots and empty segments in DNS labels
bwplotka Nov 13, 2026
9da7aac
discovery/dns: bound TCP fallback exchange with a single timeout
bwplotka Nov 13, 2026
f856730
discovery/dns: check EDNS0 extended RCODE and tighten name decoding b…
bwplotka Nov 13, 2026
31481a6
tests: expand DNS wire decoder, serde null, and search-path fallback …
bwplotka Nov 13, 2026
8b4be47
discovery/dns: document Prometheus discovery/dns meta label constants
bwplotka Nov 13, 2026
300763e
discovery/dns: resolve configured DNS names concurrently in refresh
bwplotka Nov 14, 2026
51f33c4
discovery/dns: allow non-hostname characters in owner names and filte…
bwplotka Nov 14, 2026
8c53150
discovery/dns: support zoned IPv6 nameservers in ResolvConf::parse
bwplotka Nov 14, 2026
0267e56
discovery/dns: format IPv4-mapped AAAA records as IPv4 addresses
bwplotka Nov 14, 2026
58762bb
discovery/dns: document ResolvConf per-server timeout difference from Go
bwplotka Nov 14, 2026
13e6208
discovery/dns: document nameserver ip:port and 255-octet search candi…
bwplotka Nov 14, 2026
b14208c
benches: add benchfmt.rs DNS discovery benchmarks
bwplotka Nov 14, 2026
4d76f7d
discovery/dns: uppercase DnsRecordType and DnsRecord variants
bwplotka Nov 18, 2026
bcb5ef6
discovery/dns: remove join_host_port and format IP targets via Socket…
bwplotka Nov 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ reqwest = { version = "0.13", default-features = false, features = ["http2", "ru
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["rt", "sync", "time"] }
tokio = { version = "1", default-features = false, features = ["io-util", "net", "rt", "sync", "time"] }

[workspace.lints.rust]
missing_docs = "warn"
Expand Down
6 changes: 5 additions & 1 deletion crates/scrape-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ tokio = { workspace = true }

[dev-dependencies]
benchfmt = { workspace = true }
tokio = { workspace = true, features = ["io-util", "macros", "net", "rt-multi-thread", "test-util"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "test-util"] }

[[bench]]
name = "discovery_dns"
harness = false

[[bench]]
name = "scrape"
Expand Down
320 changes: 320 additions & 0 deletions crates/scrape-manager/benches/discovery_dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
// Copyright 2026 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Throughput and allocation benchmarks for `prometheus_scrape_manager::discovery::dns`
//! covering `resolv.conf` parsing, target-group label construction (`refresh_with`), and
//! end-to-end concurrent UDP wire resolution (`refresh`).
//!
//! Recommended CLI invocation:
//! ```sh
//! cargo bench -p prometheus-scrape-manager --bench discovery_dns -- -bench '^BenchmarkDns' -benchtime 500ms -count 6 -cpu 2 -benchmem | tee ${bench}.txt
//! ```

use std::hint::black_box;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::{self, JoinHandle};
use std::time::Duration;

use benchfmt::{CountingAlloc, Suite};
use prometheus_scrape_manager::discovery::dns::{DnsDiscovery, DnsRecord, DnsSdConfig, ResolvConf};

#[global_allocator]
static ALLOC: CountingAlloc = CountingAlloc::new();

const SAMPLE_RESOLV_CONF: &str = "\
# Generated by NetworkManager
; Cluster DNS configuration
nameserver 10.96.0.10
nameserver 2001:4860:4860::8888
nameserver [fe80::1%2]:5353
search monitoring.svc.cluster.local svc.cluster.local cluster.local example.internal
options ndots:5 timeout:2 attempts:2
";

fn build_srv_records(count: usize) -> Vec<DnsRecord> {
(0..count)
.map(|i| DnsRecord::SRV {
priority: 10,
weight: 20,
port: 9100,
target: format!("node-exporter-{i:03}.monitoring.svc.cluster.local."),
})
.collect()
}

fn build_a_records(count: usize) -> Vec<DnsRecord> {
(0..count)
.map(|i| DnsRecord::A(Ipv4Addr::new(10, 244, (i / 256) as u8, (i % 256) as u8)))
.collect()
}

fn build_aaaa_records(count: usize) -> Vec<DnsRecord> {
(0..count)
.map(|i| DnsRecord::AAAA(Ipv6Addr::new(0xfd00, 0, 0, 1, 0, 0, 0, (i + 1) as u16)))
.collect()
}

fn encode_wire_name(name: &str) -> Vec<u8> {
let mut out = Vec::new();
let trimmed = name.trim_end_matches('.');
if !trimmed.is_empty() {
for label in trimmed.split('.') {
out.push(label.len() as u8);
out.extend_from_slice(label.as_bytes());
}
}
out.push(0);
out
}

/// Builds a wire-format DNS response containing `record_count` `SRV` answer records for `qname`
/// plus an EDNS0 OPT additional record.
fn build_wire_srv_response(qname: &str, record_count: usize) -> Vec<u8> {
let mut pkt = Vec::with_capacity(512);
// Header: tx_id=0 (overwritten per request), flags=0x8180 (QR=1, RD=1, RA=1),
// QDCOUNT=1, ANCOUNT=record_count, NSCOUNT=0, ARCOUNT=1 (EDNS0 OPT).
pkt.extend_from_slice(&0u16.to_be_bytes());
pkt.extend_from_slice(&0x8180u16.to_be_bytes());
pkt.extend_from_slice(&1u16.to_be_bytes());
pkt.extend_from_slice(&(record_count as u16).to_be_bytes());
pkt.extend_from_slice(&0u16.to_be_bytes());
pkt.extend_from_slice(&1u16.to_be_bytes());

// Question section: QNAME (at offset 12), QTYPE=SRV (33), QCLASS=IN (1).
let qname_wire = encode_wire_name(qname);
pkt.extend_from_slice(&qname_wire);
pkt.extend_from_slice(&33u16.to_be_bytes());
pkt.extend_from_slice(&1u16.to_be_bytes());

// Answer section: use RFC 1035 compression pointer `0xC00C` (offset 12) for the owner name.
for i in 0..record_count {
pkt.extend_from_slice(&0xC00Cu16.to_be_bytes());
pkt.extend_from_slice(&33u16.to_be_bytes()); // TYPE = SRV
pkt.extend_from_slice(&1u16.to_be_bytes()); // CLASS = IN
pkt.extend_from_slice(&60u32.to_be_bytes()); // TTL = 60s

let target_wire = encode_wire_name(&format!("pod-{i:02}.monitoring.svc.cluster.local."));
let rdlength = (6 + target_wire.len()) as u16;
pkt.extend_from_slice(&rdlength.to_be_bytes());
pkt.extend_from_slice(&10u16.to_be_bytes()); // priority
pkt.extend_from_slice(&20u16.to_be_bytes()); // weight
pkt.extend_from_slice(&9100u16.to_be_bytes()); // port
pkt.extend_from_slice(&target_wire);
}

// Additional section: EDNS0 OPT pseudo-RR (NAME=root, TYPE=41, UDP payload=4096).
pkt.push(0);
pkt.extend_from_slice(&41u16.to_be_bytes());
pkt.extend_from_slice(&4096u16.to_be_bytes());
pkt.extend_from_slice(&0u32.to_be_bytes());
pkt.extend_from_slice(&0u16.to_be_bytes());

pkt
}

/// Allocation-free loopback UDP responder running on a dedicated OS thread so that
/// `CountingAlloc` measures only `DnsDiscovery::refresh` allocations.
struct LoopbackUdpDnsServer {
addr: SocketAddr,
stop: Arc<AtomicBool>,
handle: Option<JoinHandle<()>>,
}

impl LoopbackUdpDnsServer {
fn start(prebuilt_response: Vec<u8>, qname_wire_len: usize) -> Self {
let sock = UdpSocket::bind("127.0.0.1:0").expect("bind loopback udp");
sock.set_read_timeout(Some(Duration::from_millis(50)))
.expect("set read timeout");
let addr = sock.local_addr().expect("local addr");
let stop = Arc::new(AtomicBool::new(false));
let stop_flag = Arc::clone(&stop);

assert!(
prebuilt_response.len() <= 1024,
"prebuilt wire response must fit in fixed 1024-byte stack buffer"
);
let resp_len = prebuilt_response.len();
let mut resp_template = [0u8; 1024];
resp_template[..resp_len].copy_from_slice(&prebuilt_response);
let qname_end = 12 + qname_wire_len;

let handle = thread::spawn(move || {
let mut req_buf = [0u8; 512];
let mut resp_buf = resp_template;
while !stop_flag.load(Ordering::Relaxed) {
if let Ok((n, peer)) = sock.recv_from(&mut req_buf)
&& n >= qname_end
{
resp_buf[0] = req_buf[0];
resp_buf[1] = req_buf[1];
resp_buf[12..qname_end].copy_from_slice(&req_buf[12..qname_end]);
let _ = sock.send_to(&resp_buf[..resp_len], peer);
}
}
});

Self {
addr,
stop,
handle: Some(handle),
}
}
}

impl Drop for LoopbackUdpDnsServer {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
// Send a dummy wake byte so `recv_from` unblocks immediately without waiting for timeout.
if let Ok(wake) = UdpSocket::bind("127.0.0.1:0") {
let _ = wake.send_to(&[0u8], self.addr);
}
if let Some(handle) = self.handle.take() {
let _ = handle.join();
}
}
}

fn main() {
let mut suite = Suite::from_env("prometheus-scrape-manager").with_alloc(&ALLOC);

suite = suite.bench("Dns/op=resolv_conf_parse", |b| {
b.set_bytes(SAMPLE_RESOLV_CONF.len() as u64);
b.report_allocs();
while b.keep_running() {
let conf = ResolvConf::parse(black_box(SAMPLE_RESOLV_CONF));
black_box(conf);
}
});

for count in [16usize, 64] {
let records = Arc::new(build_srv_records(count));
suite = suite.bench(
format!("Dns/op=refresh_with/type=SRV/records={count}"),
move |b| {
let cfg = DnsSdConfig {
names: vec!["_metrics._tcp.monitoring.svc.cluster.local.".to_owned()],
..DnsSdConfig::default()
};
let discovery = DnsDiscovery::new(cfg).expect("valid SRV config");
let records = Arc::clone(&records);

b.report_allocs();
while b.keep_running() {
let groups = discovery
.refresh_with(|_name, _rtype| Ok(black_box(records.as_slice()).to_vec()))
.expect("refresh_with SRV");
black_box(groups);
}
},
);
}

{
let records = Arc::new(build_a_records(64));
suite = suite.bench("Dns/op=refresh_with/type=A/records=64", move |b| {
let cfg = DnsSdConfig {
names: vec!["headless.monitoring.svc.cluster.local.".to_owned()],
refresh_interval: Duration::from_secs(30),
record_type: "A".to_owned(),
port: 9100,
};
let discovery = DnsDiscovery::new(cfg).expect("valid A config");
let records = Arc::clone(&records);

b.report_allocs();
while b.keep_running() {
let groups = discovery
.refresh_with(|_name, _rtype| Ok(black_box(records.as_slice()).to_vec()))
.expect("refresh_with A");
black_box(groups);
}
});
}

{
let records = Arc::new(build_aaaa_records(64));
suite = suite.bench("Dns/op=refresh_with/type=AAAA/records=64", move |b| {
let cfg = DnsSdConfig {
names: vec!["headless-v6.monitoring.svc.cluster.local.".to_owned()],
refresh_interval: Duration::from_secs(30),
record_type: "AAAA".to_owned(),
port: 9100,
};
let discovery = DnsDiscovery::new(cfg).expect("valid AAAA config");
let records = Arc::clone(&records);

b.report_allocs();
while b.keep_running() {
let groups = discovery
.refresh_with(|_name, _rtype| Ok(black_box(records.as_slice()).to_vec()))
.expect("refresh_with AAAA");
black_box(groups);
}
});
}

for names_count in [1usize, 8] {
suite = suite.bench(
format!("Dns/op=refresh_udp/names={names_count}/records=8"),
move |b| {
let template_qname = "_metrics-0._tcp.monitoring.svc.cluster.local.";
let qname_wire_len = encode_wire_name(template_qname).len();
let wire_resp = build_wire_srv_response(template_qname, 8);
let wire_bytes = (wire_resp.len() * names_count) as u64;
let server = LoopbackUdpDnsServer::start(wire_resp, qname_wire_len);

let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("current_thread runtime");

let names: Vec<String> = (0..names_count)
.map(|i| format!("_metrics-{i}._tcp.monitoring.svc.cluster.local."))
.collect();
let cfg = DnsSdConfig {
names,
..DnsSdConfig::default()
};
let resolv_conf = ResolvConf {
servers: vec![server.addr],
search: Vec::new(),
ndots: 5,
timeout: Duration::from_secs(5),
};
let discovery =
DnsDiscovery::with_resolv_conf(cfg, resolv_conf).expect("valid UDP config");

// Warm up runtime and socket resources once and verify all records resolve.
rt.block_on(async {
let groups = discovery.refresh().await.expect("warm refresh");
assert_eq!(groups.len(), names_count);
assert!(groups.iter().all(|g| g.targets.len() == 8));
});

b.set_bytes(wire_bytes);
b.report_allocs();
while b.keep_running() {
rt.block_on(async {
let groups = discovery.refresh().await.expect("refresh udp");
black_box(groups);
});
}
},
);
}

suite.run();
}
Loading
Loading