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

foreward all txt entries as labels

This commit is contained in:
Robin Appelman 2019-09-10 18:43:38 +02:00
commit a98390ff24
3 changed files with 30 additions and 32 deletions

7
Cargo.lock generated
View file

@ -246,11 +246,6 @@ dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "mdns" name = "mdns"
version = "0.3.1" version = "0.3.1"
@ -389,7 +384,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"atomicwrites 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "atomicwrites 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"mdns 0.3.1 (git+https://github.com/bkchr/mdns?branch=tokio_rewrite)", "mdns 0.3.1 (git+https://github.com/bkchr/mdns?branch=tokio_rewrite)",
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
@ -892,7 +886,6 @@ dependencies = [
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
"checksum mdns 0.3.1 (git+https://github.com/bkchr/mdns?branch=tokio_rewrite)" = "<none>" "checksum mdns 0.3.1 (git+https://github.com/bkchr/mdns?branch=tokio_rewrite)" = "<none>"
"checksum memoffset 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce6075db033bbbb7ee5a0bbd3a3186bbae616f57fb001c485c7ff77955f8177f" "checksum memoffset 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce6075db033bbbb7ee5a0bbd3a3186bbae616f57fb001c485c7ff77955f8177f"
"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23" "checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23"

View file

@ -14,5 +14,4 @@ futures = "0.1.28"
tokio = "0.1.21" tokio = "0.1.21"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
maplit = "1.0"
atomicwrites = "0.2" atomicwrites = "0.2"

View file

@ -1,8 +1,7 @@
use atomicwrites::{AllowOverwrite, AtomicFile}; use atomicwrites::{AllowOverwrite, AtomicFile};
use futures::{Future, Stream}; use futures::{Future, Stream};
use maplit::hashmap;
use mdns::{Record, RecordKind}; use mdns::{Record, RecordKind};
use serde::{Deserialize, Serialize}; use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::io::Write; use std::io::Write;
@ -17,25 +16,23 @@ use std::{net::IpAddr, time::Duration};
const SERVICE_NAME: &'static str = "_prometheus-http._tcp.local"; const SERVICE_NAME: &'static str = "_prometheus-http._tcp.local";
struct Service { struct Service {
name: String, labels: HashMap<String, String>,
addr: IpAddr, addr: IpAddr,
port: u16, port: u16,
last_seen: Instant, last_seen: Instant,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize)]
struct PrometheusService { struct PrometheusService<'a> {
targets: Vec<String>, targets: Vec<String>,
labels: HashMap<String, String>, labels: &'a HashMap<String, String>,
} }
impl From<&Service> for PrometheusService { impl<'a> From<&'a Service> for PrometheusService<'a> {
fn from(service: &Service) -> Self { fn from(service: &'a Service) -> Self {
PrometheusService { PrometheusService {
targets: vec![format!("{}:{}", service.addr, service.port)], targets: vec![format!("{}:{}", service.addr, service.port)],
labels: hashmap! { labels: &service.labels,
"name".to_string() => service.name.clone()
},
} }
} }
} }
@ -98,11 +95,11 @@ fn discover(tx: Sender<Service>) {
{ {
let addr = response.records().filter_map(self::to_ip_addr).next(); let addr = response.records().filter_map(self::to_ip_addr).next();
let port = response.records().filter_map(self::to_port).next(); let port = response.records().filter_map(self::to_port).next();
let name = response.records().filter_map(self::to_name).next(); let labels = response.records().filter_map(self::to_labels).next();
if let (Some(addr), Some(name), Some(port)) = (addr, name, port) { if let (Some(addr), Some(labels), Some(port)) = (addr, labels, port) {
let _ = tx.send(Service { let _ = tx.send(Service {
name, labels,
addr, addr,
port, port,
last_seen: Instant::now(), last_seen: Instant::now(),
@ -131,16 +128,25 @@ fn to_port(record: &Record) -> Option<u16> {
} }
} }
fn to_name(record: &Record) -> Option<String> { fn to_labels(record: &Record) -> Option<HashMap<String, String>> {
if let RecordKind::TXT(txt) = &record.kind { if record.name.contains(SERVICE_NAME) {
for pair in txt { if let RecordKind::TXT(txt) = &record.kind {
let mut parts = pair.split('='); Some(
if let (Some(key), Some(value)) = (parts.next(), parts.next()) { txt.iter()
if key == "name" { .flat_map(|pair| {
return Some(value.to_string()); let mut parts = pair.split('=');
} if let (Some(key), Some(value)) = (parts.next(), parts.next()) {
} Some((key.to_string(), value.to_string()))
} else {
None
}
})
.collect(),
)
} else {
None
} }
} else {
None
} }
None
} }