mirror of
https://codeberg.org/icewind/prometheus-mdns-rs.git
synced 2026-06-03 09:54:21 +02:00
foreward all txt entries as labels
This commit is contained in:
parent
1647e0baba
commit
a98390ff24
3 changed files with 30 additions and 32 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -246,11 +246,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "mdns"
|
||||
version = "0.3.1"
|
||||
|
|
@ -389,7 +384,6 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"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)",
|
||||
"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)",
|
||||
"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)",
|
||||
|
|
@ -892,7 +886,6 @@ dependencies = [
|
|||
"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 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 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"
|
||||
|
|
|
|||
|
|
@ -14,5 +14,4 @@ futures = "0.1.28"
|
|||
tokio = "0.1.21"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
maplit = "1.0"
|
||||
atomicwrites = "0.2"
|
||||
50
src/main.rs
50
src/main.rs
|
|
@ -1,8 +1,7 @@
|
|||
use atomicwrites::{AllowOverwrite, AtomicFile};
|
||||
use futures::{Future, Stream};
|
||||
use maplit::hashmap;
|
||||
use mdns::{Record, RecordKind};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::io::Write;
|
||||
|
|
@ -17,25 +16,23 @@ use std::{net::IpAddr, time::Duration};
|
|||
const SERVICE_NAME: &'static str = "_prometheus-http._tcp.local";
|
||||
|
||||
struct Service {
|
||||
name: String,
|
||||
labels: HashMap<String, String>,
|
||||
addr: IpAddr,
|
||||
port: u16,
|
||||
last_seen: Instant,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PrometheusService {
|
||||
#[derive(Serialize)]
|
||||
struct PrometheusService<'a> {
|
||||
targets: Vec<String>,
|
||||
labels: HashMap<String, String>,
|
||||
labels: &'a HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl From<&Service> for PrometheusService {
|
||||
fn from(service: &Service) -> Self {
|
||||
impl<'a> From<&'a Service> for PrometheusService<'a> {
|
||||
fn from(service: &'a Service) -> Self {
|
||||
PrometheusService {
|
||||
targets: vec![format!("{}:{}", service.addr, service.port)],
|
||||
labels: hashmap! {
|
||||
"name".to_string() => service.name.clone()
|
||||
},
|
||||
labels: &service.labels,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -98,11 +95,11 @@ fn discover(tx: Sender<Service>) {
|
|||
{
|
||||
let addr = response.records().filter_map(self::to_ip_addr).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 {
|
||||
name,
|
||||
labels,
|
||||
addr,
|
||||
port,
|
||||
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 record.name.contains(SERVICE_NAME) {
|
||||
if let RecordKind::TXT(txt) = &record.kind {
|
||||
for pair in txt {
|
||||
Some(
|
||||
txt.iter()
|
||||
.flat_map(|pair| {
|
||||
let mut parts = pair.split('=');
|
||||
if let (Some(key), Some(value)) = (parts.next(), parts.next()) {
|
||||
if key == "name" {
|
||||
return Some(value.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some((key.to_string(), value.to_string()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue