prometheus / client_rust

Prometheus / OpenMetrics client library in Rust
Apache License 2.0
484 stars 80 forks source link

Derive macro to register struct of metrics #218

Open Hackzzila opened 3 months ago

Hackzzila commented 3 months ago

Resolves #140 cc @mxinden

Here is a rough implementation. It does not have docs yet

Example:

#[derive(Register, Default)]
struct Metrics {
    /// This is my counter
    my_counter: Counter,
    nested: NestedMetrics,
    #[register(skip)]
    skipped: Counter,
    #[register(unit = "bytes")]
    custom_unit: Counter,
    #[register(name = "my_custom_name")]
    custom_name: Counter,
    /// This will get ignored
    #[register(help = "my custom help")]
    custom_help: Counter,
}

#[derive(Register, Default)]
struct NestedMetrics {
    /// This is my gauge
    my_gauge: Gauge,
}
// Only if Metrics implements default
let metrics = Metrics::register_default(&mut registry);

let metrics = Metrics::default();
metrics.register(&mut registry);