From 7a42a99cc2fe3092286f73eb656b995e54b81755 Mon Sep 17 00:00:00 2001 From: Victor Nordam Suadicani Date: Fri, 8 Jul 2022 11:50:03 +0200 Subject: [PATCH 1/4] Add Debug impls and get rid of Clippy warnings Signed-off-by: Victor Nordam Suadicani --- benches/encoding/text.rs | 5 ++--- benches/family.rs | 6 +++--- derive-text-encode/tests/lib.rs | 14 +++++++------- src/encoding/text.rs | 10 +++++++--- src/lib.rs | 1 + src/metrics.rs | 2 +- src/metrics/counter.rs | 1 + src/metrics/exemplar.rs | 5 +++++ src/metrics/family.rs | 1 + src/metrics/gauge.rs | 1 + src/metrics/histogram.rs | 2 ++ src/metrics/info.rs | 1 + src/registry.rs | 8 ++++++-- 13 files changed, 38 insertions(+), 19 deletions(-) diff --git a/benches/encoding/text.rs b/benches/encoding/text.rs index 9c3b450d..78c2c4f3 100644 --- a/benches/encoding/text.rs +++ b/benches/encoding/text.rs @@ -7,7 +7,6 @@ use prometheus_client::metrics::family::Family; use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; use prometheus_client::registry::Registry; use std::io::Write; -use std::sync::atomic::AtomicU64; pub fn text(c: &mut Criterion) { c.bench_function("encode", |b| { @@ -23,7 +22,7 @@ pub fn text(c: &mut Criterion) { Get, #[allow(dead_code)] Put, - }; + } #[derive(Clone, Hash, PartialEq, Eq)] enum Status { @@ -32,7 +31,7 @@ pub fn text(c: &mut Criterion) { Four, #[allow(dead_code)] Five, - }; + } impl Encode for Status { fn encode(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> { diff --git a/benches/family.rs b/benches/family.rs index 4fc50c6c..a409cc43 100644 --- a/benches/family.rs +++ b/benches/family.rs @@ -1,4 +1,4 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{criterion_group, criterion_main, Criterion}; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; @@ -28,7 +28,7 @@ pub fn family(c: &mut Criterion) { Get, #[allow(dead_code)] Put, - }; + } #[derive(Clone, Hash, PartialEq, Eq)] enum Status { @@ -37,7 +37,7 @@ pub fn family(c: &mut Criterion) { Four, #[allow(dead_code)] Five, - }; + } let family = Family::::default(); b.iter(|| { diff --git a/derive-text-encode/tests/lib.rs b/derive-text-encode/tests/lib.rs index 00bc7617..59aa9103 100644 --- a/derive-text-encode/tests/lib.rs +++ b/derive-text-encode/tests/lib.rs @@ -10,14 +10,14 @@ fn basic_flow() { struct Labels { method: Method, path: String, - }; + } #[derive(Clone, Hash, PartialEq, Eq, Encode)] enum Method { - GET, + Get, #[allow(dead_code)] - PUT, - }; + Put, + } let family = Family::::default(); registry.register("my_counter", "This is my counter", family.clone()); @@ -25,7 +25,7 @@ fn basic_flow() { // Record a single HTTP GET request. family .get_or_create(&Labels { - method: Method::GET, + method: Method::Get, path: "/metrics".to_string(), }) .inc(); @@ -52,13 +52,13 @@ fn remap_keyword_identifiers() { // Test makes sure `r#type` is replaced by `type` in the OpenMetrics // output. r#type: u64, - }; + } let labels = Labels { r#type: 42 }; let mut buffer = vec![]; - labels.encode(&mut buffer); + labels.encode(&mut buffer).unwrap(); assert_eq!( "type=\"42\"".to_string(), diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 742032d6..c3a48844 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -225,6 +225,7 @@ impl Encode for () { // objects can not use type parameters. // // TODO: Alternative solutions to the above are very much appreciated. +#[allow(missing_debug_implementations)] pub struct Encoder<'a, 'b> { writer: &'a mut dyn Write, name: &'a str, @@ -303,6 +304,7 @@ impl<'a, 'b> Encoder<'a, 'b> { } } +#[allow(missing_debug_implementations)] #[must_use] pub struct BucketEncoder<'a> { writer: &'a mut dyn Write, @@ -342,6 +344,7 @@ impl<'a> BucketEncoder<'a> { } } +#[allow(missing_debug_implementations)] #[must_use] pub struct ValueEncoder<'a> { writer: &'a mut dyn Write, @@ -359,6 +362,7 @@ impl<'a> ValueEncoder<'a> { } } +#[allow(missing_debug_implementations)] #[must_use] pub struct ExemplarEncoder<'a> { writer: &'a mut dyn Write, @@ -616,7 +620,7 @@ mod tests { fn encode_counter() { let counter: Counter = Counter::default(); let mut registry = Registry::default(); - registry.register("my_counter", "My counter", counter.clone()); + registry.register("my_counter", "My counter", counter); let mut encoded = Vec::new(); @@ -629,7 +633,7 @@ mod tests { fn encode_counter_with_unit() { let mut registry = Registry::default(); let counter: Counter = Counter::default(); - registry.register_with_unit("my_counter", "My counter", Unit::Seconds, counter.clone()); + registry.register_with_unit("my_counter", "My counter", Unit::Seconds, counter); let mut encoded = Vec::new(); encode(&mut encoded, ®istry).unwrap(); @@ -677,7 +681,7 @@ mod tests { fn encode_gauge() { let mut registry = Registry::default(); let gauge: Gauge = Gauge::default(); - registry.register("my_gauge", "My gauge", gauge.clone()); + registry.register("my_gauge", "My gauge", gauge); let mut encoded = Vec::new(); diff --git a/src/lib.rs b/src/lib.rs index add69623..b5edaac2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![forbid(unsafe_code)] #![deny(unused)] #![deny(dead_code)] +#![warn(missing_debug_implementations)] //! Client library implementation of the [Open Metrics //! specification](https://github.com/OpenObservability/OpenMetrics). Allows diff --git a/src/metrics.rs b/src/metrics.rs index 5a21654c..e09504e3 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -12,7 +12,7 @@ pub trait TypedMetric { const TYPE: MetricType = MetricType::Unknown; } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub enum MetricType { Counter, Gauge, diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index c8ad9461..5bb8db71 100644 --- a/src/metrics/counter.rs +++ b/src/metrics/counter.rs @@ -39,6 +39,7 @@ use std::sync::Arc; /// let _value: f64 = counter.get(); /// ``` #[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] +#[derive(Debug)] pub struct Counter { value: Arc, phantom: PhantomData, diff --git a/src/metrics/exemplar.rs b/src/metrics/exemplar.rs index 66a043bb..9749340f 100644 --- a/src/metrics/exemplar.rs +++ b/src/metrics/exemplar.rs @@ -12,6 +12,7 @@ use std::sync::atomic::AtomicU32; use std::sync::atomic::AtomicU64; use std::sync::{Arc, RwLock, RwLockReadGuard}; +#[derive(Debug)] pub struct Exemplar { pub(crate) label_set: S, pub(crate) value: V, @@ -30,6 +31,7 @@ pub struct Exemplar { /// let _value: (u64, _) = counter_with_exemplar.get(); /// ``` #[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] +#[derive(Debug)] pub struct CounterWithExemplar { pub(crate) inner: Arc>>, } @@ -47,6 +49,7 @@ impl Clone for CounterWithExemplar { } } +#[derive(Debug)] pub struct CounterWithExemplarInner { pub(crate) exemplar: Option>, pub(crate) counter: Counter, @@ -118,6 +121,7 @@ type RwLockGuardedCounterWithExemplar<'a, S, N, A> = /// let histogram = HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10)); /// histogram.observe(4.2, Some(vec![("user_id".to_string(), "42".to_string())])); /// ``` +#[derive(Debug)] pub struct HistogramWithExemplars { // TODO: Not ideal, as Histogram has a Mutex as well. pub(crate) inner: Arc>>, @@ -131,6 +135,7 @@ impl Clone for HistogramWithExemplars { } } +#[derive(Debug)] pub struct HistogramWithExemplarsInner { pub(crate) exemplars: HashMap>, pub(crate) histogram: Histogram, diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 1610881b..be945075 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -97,6 +97,7 @@ use std::sync::{Arc, RwLock, RwLockReadGuard}; /// # assert_eq!(expected, String::from_utf8(buffer).unwrap()); /// ``` // TODO: Consider exposing hash algorithm. +#[derive(Debug)] pub struct Family M> { metrics: Arc>>, /// Function that when called constructs a new metric. diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index 504d5bd9..6d4b8531 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -39,6 +39,7 @@ use std::sync::Arc; /// let _value: f64 = gauge.get(); /// ``` #[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] +#[derive(Debug)] pub struct Gauge { value: Arc, phantom: PhantomData, diff --git a/src/metrics/histogram.rs b/src/metrics/histogram.rs index 3f2ae9ff..c025c4e8 100644 --- a/src/metrics/histogram.rs +++ b/src/metrics/histogram.rs @@ -31,6 +31,7 @@ use std::sync::{Arc, Mutex, MutexGuard}; /// ``` // TODO: Consider using atomics. See // https://github.com/tikv/rust-prometheus/pull/314. +#[derive(Debug)] pub struct Histogram { inner: Arc>, } @@ -43,6 +44,7 @@ impl Clone for Histogram { } } +#[derive(Debug)] pub(crate) struct Inner { // TODO: Consider allowing integer observe values. sum: f64, diff --git a/src/metrics/info.rs b/src/metrics/info.rs index ffe52910..8457207f 100644 --- a/src/metrics/info.rs +++ b/src/metrics/info.rs @@ -12,6 +12,7 @@ use crate::metrics::{MetricType, TypedMetric}; /// /// let _info = Info::new(vec![("os", "GNU/linux")]); /// ``` +#[derive(Debug)] pub struct Info(pub(crate) S); impl Info { diff --git a/src/registry.rs b/src/registry.rs index 94ee0e5b..dcb01926 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -57,6 +57,7 @@ use std::borrow::Cow; /// # "# EOF\n"; /// # assert_eq!(expected, String::from_utf8(buffer).unwrap()); /// ``` +#[derive(Debug)] pub struct Registry> { prefix: Option, labels: Vec<(Cow<'static, str>, Cow<'static, str>)>, @@ -250,6 +251,7 @@ impl Registry { /// Iterator iterating both the metrics registered directly with the registry as /// well as all metrics registered with sub-registries. +#[derive(Debug)] pub struct RegistryIterator<'a, M> { metrics: std::slice::Iter<'a, (Descriptor, M)>, sub_registries: std::slice::Iter<'a, Registry>, @@ -280,7 +282,7 @@ impl<'a, M> Iterator for RegistryIterator<'a, M> { } } -#[derive(Clone)] +#[derive(Clone, Debug)] struct Prefix(String); impl From for Prefix { @@ -295,6 +297,7 @@ impl From for String { } } +#[derive(Debug)] pub struct Descriptor { name: String, help: String, @@ -323,6 +326,7 @@ impl Descriptor { /// Metric units recommended by Open Metrics. /// /// See [`Unit::Other`] to specify alternative units. +#[derive(Debug)] pub enum Unit { Amperes, Bytes, @@ -345,7 +349,7 @@ mod tests { fn register_and_iterate() { let mut registry: Registry = Registry::default(); let counter = Counter::default(); - registry.register("my_counter", "My counter", counter.clone()); + registry.register("my_counter", "My counter", counter); assert_eq!(1, registry.iter().count()) } From 8efa0663995c1e97097cd4743d69b2a06a887957 Mon Sep 17 00:00:00 2001 From: Victor Nordam Suadicani Date: Mon, 11 Jul 2022 09:47:02 +0200 Subject: [PATCH 2/4] Include workspace and tests in clippy linting Signed-off-by: Victor Nordam Suadicani --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f38e40ed..864faad5 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -118,7 +118,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: clippy - args: -- -D warnings + args: --workspace --all-targets -- -D warnings check-rustdoc-links: name: Check rustdoc intra-doc links From 12b6fffadde9be66d54c8948c9dfd29aa8d40e76 Mon Sep 17 00:00:00 2001 From: Victor Nordam Suadicani Date: Thu, 14 Jul 2022 13:12:19 +0200 Subject: [PATCH 3/4] Added to changelog Signed-off-by: Victor Nordam Suadicani --- CHANGELOG.md | 2 ++ src/metrics/counter.rs | 1 + src/metrics/exemplar.rs | 1 + src/metrics/gauge.rs | 1 + 4 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80383cfe..4cb400ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added a `with_prefix` method to `Registry` to allow initializing a registry with a prefix. See [PR 70]. +- Added `Debug` implementations on most public types that were missing them. See [PR 71]. ### Removed - Remove `Add` trait implementation for a private type which lead to compile time conflicts with existing `Add` implementations e.g. on `String`. See [PR 69]. @@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [PR 65]: https://github.com/prometheus/client_rust/pull/65 [PR 69]: https://github.com/prometheus/client_rust/pull/69 [PR 70]: https://github.com/prometheus/client_rust/pull/70 +[PR 71]: https://github.com/prometheus/client_rust/pull/71 ## [0.16.0] diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index 5bb8db71..689723d8 100644 --- a/src/metrics/counter.rs +++ b/src/metrics/counter.rs @@ -46,6 +46,7 @@ pub struct Counter { } #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] +#[derive(Debug)] pub struct Counter { value: Arc, phantom: PhantomData, diff --git a/src/metrics/exemplar.rs b/src/metrics/exemplar.rs index 9749340f..485712f6 100644 --- a/src/metrics/exemplar.rs +++ b/src/metrics/exemplar.rs @@ -37,6 +37,7 @@ pub struct CounterWithExemplar { } #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] +#[derive(Debug)] pub struct CounterWithExemplar { pub(crate) inner: Arc>>, } diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index 6d4b8531..a8e41140 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -46,6 +46,7 @@ pub struct Gauge { } #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] +#[derive(Debug)] pub struct Gauge { value: Arc, phantom: PhantomData, From 01514cfc5072f21b4138b90beaec4885c8c7a53f Mon Sep 17 00:00:00 2001 From: Victor Nordam Suadicani Date: Thu, 14 Jul 2022 14:50:06 +0200 Subject: [PATCH 4/4] Fix test Signed-off-by: Victor Nordam Suadicani --- derive-text-encode/tests/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/derive-text-encode/tests/lib.rs b/derive-text-encode/tests/lib.rs index 59aa9103..2e4cbbfc 100644 --- a/derive-text-encode/tests/lib.rs +++ b/derive-text-encode/tests/lib.rs @@ -36,7 +36,7 @@ fn basic_flow() { let expected = "# HELP my_counter This is my counter.\n".to_owned() + "# TYPE my_counter counter\n" - + "my_counter_total{method=\"GET\",path=\"/metrics\"} 1\n" + + "my_counter_total{method=\"Get\",path=\"/metrics\"} 1\n" + "# EOF\n"; assert_eq!(expected, String::from_utf8(buffer).unwrap()); }