Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude = ["tests/otap-compat"]
[features]
default = ["arrow", "dto"]
arrow = ["dep:arrow-array", "dep:arrow-buffer", "dep:arrow-schema"]
discovery = ["dep:serde", "dep:serde_json"]
discovery = ["dep:serde", "dep:serde_json", "dep:tokio"]
dto = ["dep:prost", "dep:prost-types"]

[dependencies]
Expand All @@ -27,11 +27,13 @@ prost-types = { version = "0.14", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["io-util", "net", "time"], optional = true }

[dev-dependencies]
benchfmt = { git = "https://github.com/bwplotka/benchfmt.rs" }
prost-build = "=0.14.4"
protox = "0.10"
tokio = { version = "1", default-features = false, features = ["macros", "rt"] }

[[bench]]
name = "decode"
Expand Down
41 changes: 40 additions & 1 deletion src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Prometheus-compatible target discovery configuration and target group types.
//! Prometheus-compatible target discovery (`static_configs` and `dns_sd_configs`).

pub mod dns;

use std::collections::BTreeMap;
use std::time::Duration;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub use dns::{DnsDiscovery, DnsRecord, DnsRecordType, DnsSdConfig, ResolvConf};

/// Reserved Prometheus label holding the `<host>:<port>` scrape target address.
pub const ADDRESS_LABEL: &str = "__address__";

Expand All @@ -33,6 +37,9 @@ pub enum DiscoveryError {
/// I/O or filesystem error.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// DNS protocol or lookup error.
#[error("DNS error: {0}")]
Dns(String),
/// JSON payload decoding error.
#[error("JSON decode error: {0}")]
Json(#[from] serde_json::Error),
Expand Down Expand Up @@ -129,11 +136,17 @@ pub struct DiscoveryConfigs {
/// Static target group configurations (`static_configs`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub static_configs: Vec<StaticConfig>,
/// DNS service discovery configurations (`dns_sd_configs`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub dns_sd_configs: Vec<DnsSdConfig>,
}

impl DiscoveryConfigs {
/// Validates all configured service discovery sections according to Prometheus rules.
pub fn validate(&mut self) -> Result<(), DiscoveryError> {
for dns_cfg in &mut self.dns_sd_configs {
dns_cfg.validate()?;
}
Ok(())
}

Expand All @@ -143,6 +156,15 @@ impl DiscoveryConfigs {
}
}

/// Joins a host and port into `"host:port"` or `"[ipv6]:port"`, matching Go `net.JoinHostPort`.
pub(crate) fn join_host_port(host: &str, port: &str) -> String {
if host.contains(':') {
format!("[{host}]:{port}")
} else {
format!("{host}:{port}")
}
}

/// Parses a Prometheus duration string (such as `"30s"`, `"1m"`, `"500ms"`, `"1h30m"`, `"7d"`,
/// `"1w"`, `"1y"`, or `"0"`) into a [`Duration`], matching `prometheus/common/model.ParseDuration`.
pub fn parse_duration(s: &str) -> Result<Duration, DiscoveryError> {
Expand Down Expand Up @@ -248,3 +270,20 @@ pub fn format_duration(d: Duration) -> String {

out
}

pub(crate) mod serde_duration {
use std::time::Duration;

use serde::{Deserialize, Deserializer, Serializer};

use super::{format_duration, parse_duration};

pub fn serialize<S: Serializer>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&format_duration(*duration))
}

pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
let s = String::deserialize(deserializer)?;
parse_duration(&s).map_err(serde::de::Error::custom)
}
}
Loading
Loading