diff --git a/prometric-derive/src/expand.rs b/prometric-derive/src/expand.rs index 3be9db1..1154777 100644 --- a/prometric-derive/src/expand.rs +++ b/prometric-derive/src/expand.rs @@ -193,7 +193,7 @@ impl MetricBuilder { if let MetricType::Histogram(_) = &self.ty { let buckets = if let Some(buckets_expr) = buckets { - quote! { Some(#buckets_expr) } + quote! { Some(#buckets_expr.into()) } } else { quote! { None } }; diff --git a/prometric-derive/src/lib.rs b/prometric-derive/src/lib.rs index caee130..34e185f 100644 --- a/prometric-derive/src/lib.rs +++ b/prometric-derive/src/lib.rs @@ -153,11 +153,13 @@ mod utils; /// the current process. /// /// ```rust +/// # #[cfg(feature = "process")] { /// use prometric::process::ProcessCollector; /// use prometric_derive::metrics; /// /// let mut collector = ProcessCollector::default(); /// collector.collect(); +/// # } /// ``` /// /// #### Output diff --git a/prometric-derive/tests/macro.rs b/prometric-derive/tests/macro.rs index 435a329..7a8472e 100644 --- a/prometric-derive/tests/macro.rs +++ b/prometric-derive/tests/macro.rs @@ -193,3 +193,28 @@ fn bucket_expressions_work() { assert!(output.contains("test_hist")); } + +#[test] +fn bucket_defaults_work() { + #[prometric_derive::metrics(scope = "test")] + struct BucketMetrics { + /// Test histogram metric with bucket expression. + #[metric] + hist: prometric::Histogram, + } + + let registry = prometheus::default_registry(); + let app_metrics = BucketMetrics::builder().with_registry(registry).build(); + + let duration = Duration::from_secs(1); + app_metrics.hist().observe(duration.as_secs_f64()); + + let encoder = prometheus::TextEncoder::new(); + let metric_families = registry.gather(); // Wait, need to expose registry + + let mut buffer = vec![]; + encoder.encode(&metric_families, &mut buffer).unwrap(); + let output = String::from_utf8(buffer).unwrap(); + + assert!(output.contains("test_hist")); +} diff --git a/prometric/src/lib.rs b/prometric/src/lib.rs index c72b3a5..dfd9030 100644 --- a/prometric/src/lib.rs +++ b/prometric/src/lib.rs @@ -245,15 +245,15 @@ impl Clone for Histogram { } impl Histogram { - pub fn new>>( + pub fn new( registry: &prometheus::Registry, name: &str, help: &str, labels: &[&str], const_labels: HashMap, - buckets: Option, + buckets: Option>, ) -> Self { - let buckets = buckets.map(Into::into).unwrap_or(prometheus::DEFAULT_BUCKETS.to_vec()); + let buckets = buckets.unwrap_or(prometheus::DEFAULT_BUCKETS.to_vec()); let opts = prometheus::HistogramOpts::new(name, help).const_labels(const_labels).buckets(buckets); let metric = prometheus::HistogramVec::new(opts, labels).unwrap();