1
0
Fork 0
mirror of https://codeberg.org/icewind/prometheus-mdns-rs.git synced 2026-06-03 09:54:21 +02:00

add hostname as label

This commit is contained in:
Robin Appelman 2020-01-25 16:58:12 +01:00
commit cb2cca7701

View file

@ -36,7 +36,6 @@ impl<'a> From<&'a Service> for PrometheusService<'a> {
const TIMEOUT: Duration = Duration::from_secs(360); const TIMEOUT: Duration = Duration::from_secs(360);
const INTERVAL: Duration = Duration::from_secs(120); const INTERVAL: Duration = Duration::from_secs(120);
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), main_error::MainError> { async fn main() -> Result<(), main_error::MainError> {
let out = env::args() let out = env::args()
@ -49,11 +48,15 @@ async fn main() -> Result<(), main_error::MainError> {
let mut services: HashMap<IpAddr, Service> = HashMap::new(); let mut services: HashMap<IpAddr, Service> = HashMap::new();
while let Some(Ok(response)) = stream.next().await { while let Some(Ok(response)) = stream.next().await {
let addr = response.records().find_map(self::to_ip_addr); let addr: Option<IpAddr> = response.records().find_map(self::to_ip_addr);
let port = response.records().find_map(self::to_port); let port: Option<u16> = response.records().find_map(self::to_port);
let labels = response.records().find_map(self::to_labels); let labels: Option<HashMap<String, String>> = response.records().find_map(self::to_labels);
let hostname: Option<String> = response.records().find_map(self::to_hostname);
if let (Some(addr), Some(labels), Some(port)) = (addr, labels, port) { if let (Some(addr), Some(mut labels), Some(port), Some(hostname)) =
(addr, labels, port, hostname)
{
labels.insert("hostname".to_string(), hostname);
let service = Service { let service = Service {
labels, labels,
addr, addr,
@ -89,6 +92,13 @@ async fn main() -> Result<(), main_error::MainError> {
Ok(()) Ok(())
} }
fn to_hostname(record: &Record) -> Option<String> {
match &record.kind {
RecordKind::PTR(id) => id.split('.').next().map(|s| s.to_string()),
_ => None,
}
}
fn to_ip_addr(record: &Record) -> Option<IpAddr> { fn to_ip_addr(record: &Record) -> Option<IpAddr> {
match record.kind { match record.kind {
RecordKind::A(addr) => Some(addr.into()), RecordKind::A(addr) => Some(addr.into()),