Skip to content

Commit

Permalink
Derive Debug trait for all structs and enums (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka authored Sep 5, 2024
1 parent 55273f0 commit a56fc2f
Show file tree
Hide file tree
Showing 30 changed files with 128 additions and 17 deletions.
1 change: 1 addition & 0 deletions metrics-exporter-prometheus/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub enum BuildError {
ZeroBucketDuration,
}

#[derive(Debug)]
pub struct Snapshot {
pub counters: HashMap<String, HashMap<Vec<String>, u64>>,
pub gauges: HashMap<String, HashMap<Vec<String>, f64>>,
Expand Down
6 changes: 3 additions & 3 deletions metrics-exporter-prometheus/src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DEFAULT_SUMMARY_BUCKET_COUNT: NonZeroU32 = match NonZeroU32::new(3) {
const DEFAULT_SUMMARY_BUCKET_DURATION: Duration = Duration::from_secs(20);

/// Distribution type.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum Distribution {
/// A Prometheus histogram.
///
Expand Down Expand Up @@ -137,14 +137,14 @@ impl DistributionBuilder {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
struct Bucket {
begin: Instant,
summary: Summary,
}

/// A `RollingSummary` manages a list of [Summary] so that old results can be expired.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct RollingSummary {
// Buckets are ordered with the latest buckets first. The buckets are kept in alignment based
// on the instant of the first added bucket and the bucket_duration. There may be gaps in the
Expand Down
1 change: 1 addition & 0 deletions metrics-exporter-prometheus/src/exporter/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use super::ExporterConfig;
use super::ExporterFuture;

/// Builder for creating and installing a Prometheus recorder/exporter.
#[derive(Debug)]
pub struct PrometheusBuilder {
#[cfg_attr(not(any(feature = "http-listener", feature = "push-gateway")), allow(dead_code))]
exporter_config: ExporterConfig,
Expand Down
1 change: 1 addition & 0 deletions metrics-exporter-prometheus/src/exporter/http_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum ListenerType {
}

/// Error type for HTTP listening.
#[derive(Debug)]
pub enum HttpListeningError {
Hyper(hyper::Error),
Io(std::io::Error),
Expand Down
5 changes: 3 additions & 2 deletions metrics-exporter-prometheus/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use hyper::Uri;

/// Error types possible from an exporter
#[cfg(any(feature = "http-listener", feature = "push-gateway"))]
#[derive(Debug)]
pub enum ExporterError {
#[cfg(feature = "http-listener")]
HttpListener(HttpListeningError),
Expand All @@ -24,14 +25,14 @@ pub enum ExporterError {
pub type ExporterFuture = Pin<Box<dyn Future<Output = Result<(), ExporterError>> + Send + 'static>>;

#[cfg(feature = "http-listener")]
#[derive(Clone)]
#[derive(Clone, Debug)]
enum ListenDestination {
Tcp(SocketAddr),
#[cfg(feature = "uds-listener")]
Uds(std::path::PathBuf),
}

#[derive(Clone)]
#[derive(Clone, Debug)]
enum ExporterConfig {
// Run an HTTP listener on the given `listen_address`.
#[cfg(feature = "http-listener")]
Expand Down
4 changes: 3 additions & 1 deletion metrics-exporter-prometheus/src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::formatting::{
};
use crate::registry::GenerationalAtomicStorage;

#[derive(Debug)]
pub(crate) struct Inner {
pub registry: Registry<Key, GenerationalAtomicStorage>,
pub recency: Recency<Key>,
Expand Down Expand Up @@ -214,6 +215,7 @@ impl Inner {
/// Most users will not need to interact directly with the recorder, and can simply deal with the
/// builder methods on [`PrometheusBuilder`](crate::PrometheusBuilder) for building and installing
/// the recorder/exporter.
#[derive(Debug)]
pub struct PrometheusRecorder {
inner: Arc<Inner>,
}
Expand Down Expand Up @@ -275,7 +277,7 @@ impl Recorder for PrometheusRecorder {
/// handled directly by the HTTP listener, or push gateway background task. [`PrometheusHandle`]
/// allows rendering a snapshot of the current metrics stored by an installed [`PrometheusRecorder`]
/// as a payload conforming to the Prometheus exposition format.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct PrometheusHandle {
inner: Arc<Inner>,
}
Expand Down
2 changes: 2 additions & 0 deletions metrics-exporter-prometheus/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use quanta::Instant;
pub type GenerationalAtomicStorage = GenerationalStorage<AtomicStorage>;

/// Atomic metric storage for the prometheus exporter.
#[derive(Debug)]
pub struct AtomicStorage;

impl<K> metrics_util::registry::Storage<K> for AtomicStorage {
Expand All @@ -28,6 +29,7 @@ impl<K> metrics_util::registry::Storage<K> for AtomicStorage {
}

/// An `AtomicBucket` newtype wrapper that tracks the time of value insertion.
#[derive(Debug)]
pub struct AtomicBucketInstant<T> {
inner: AtomicBucket<(T, Instant)>,
}
Expand Down
4 changes: 4 additions & 0 deletions metrics-exporter-tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl std::error::Error for Error {
}
}

#[derive(Debug)]
struct State {
client_count: AtomicUsize,
should_send: AtomicBool,
Expand Down Expand Up @@ -188,6 +189,7 @@ impl State {
}
}

#[derive(Debug)]
struct Handle {
key: Key,
state: Arc<State>,
Expand Down Expand Up @@ -230,11 +232,13 @@ impl HistogramFn for Handle {
}

/// A TCP recorder.
#[derive(Debug)]
pub struct TcpRecorder {
state: Arc<State>,
}

/// Builder for creating and installing a TCP recorder/exporter.
#[derive(Debug)]
pub struct TcpBuilder {
listen_addr: SocketAddr,
buffer_size: Option<usize>,
Expand Down
2 changes: 2 additions & 0 deletions metrics-tracing-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ use tracing_integration::Map;
pub use tracing_integration::{Labels, MetricsLayer};

/// [`TracingContextLayer`] provides an implementation of a [`Layer`] for [`TracingContext`].
#[derive(Debug)]
pub struct TracingContextLayer<F> {
label_filter: F,
}
Expand Down Expand Up @@ -156,6 +157,7 @@ where
}

/// [`TracingContext`] is a [`metrics::Recorder`] that injects labels from [`tracing::Span`]s.
#[derive(Debug)]
pub struct TracingContext<R, F> {
inner: R,
label_filter: F,
Expand Down
2 changes: 1 addition & 1 deletion metrics-tracing-context/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ fn test_nested_spans() {
);
}

#[derive(Clone)]
#[derive(Clone, Debug)]
struct OnlyUser;

impl LabelFilter for OnlyUser {
Expand Down
5 changes: 4 additions & 1 deletion metrics-util/src/debugging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl CompositeKeyName {
}

/// A point-in-time snapshot of all metrics in [`DebuggingRecorder`].
#[derive(Debug)]
pub struct Snapshot(Vec<(CompositeKey, Option<Unit>, Option<SharedString>, DebugValue)>);

impl Snapshot {
Expand Down Expand Up @@ -67,6 +68,7 @@ pub enum DebugValue {
Histogram(Vec<OrderedFloat<f64>>),
}

#[derive(Debug)]
struct Inner {
registry: Registry<Key, AtomicStorage>,
seen: Mutex<IndexMap<CompositeKey, ()>>,
Expand All @@ -84,7 +86,7 @@ impl Inner {
}

/// Captures point-in-time snapshots of [`DebuggingRecorder`].
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Snapshotter {
inner: Arc<Inner>,
}
Expand Down Expand Up @@ -138,6 +140,7 @@ impl Snapshotter {
///
/// Callers can easily take snapshots of the metrics at any given time and get access
/// to the raw values.
#[derive(Debug)]
pub struct DebuggingRecorder {
inner: Arc<Inner>,
}
Expand Down
21 changes: 20 additions & 1 deletion metrics-util/src/layers/fanout.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::sync::Arc;
use std::{fmt, sync::Arc};

use metrics::{
Counter, CounterFn, Gauge, GaugeFn, Histogram, HistogramFn, Key, KeyName, Metadata, Recorder,
SharedString, Unit,
};

#[derive(Debug)]
struct FanoutCounter {
counters: Vec<Counter>,
}
Expand Down Expand Up @@ -35,6 +36,7 @@ impl From<FanoutCounter> for Counter {
}
}

#[derive(Debug)]
struct FanoutGauge {
gauges: Vec<Gauge>,
}
Expand Down Expand Up @@ -71,6 +73,7 @@ impl From<FanoutGauge> for Gauge {
}
}

#[derive(Debug)]
struct FanoutHistogram {
histograms: Vec<Histogram>,
}
Expand Down Expand Up @@ -100,6 +103,14 @@ pub struct Fanout {
recorders: Vec<Box<dyn Recorder>>,
}

impl fmt::Debug for Fanout {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Fanout")
.field("recorders_len", &self.recorders.len())
.finish_non_exhaustive()
}
}

impl Recorder for Fanout {
fn describe_counter(&self, key_name: KeyName, unit: Option<Unit>, description: SharedString) {
for recorder in &self.recorders {
Expand Down Expand Up @@ -155,6 +166,14 @@ pub struct FanoutBuilder {
recorders: Vec<Box<dyn Recorder>>,
}

impl fmt::Debug for FanoutBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FanoutBuilder")
.field("recorders_len", &self.recorders.len())
.finish_non_exhaustive()
}
}

impl FanoutBuilder {
/// Adds a recorder to the fanout list.
pub fn add_recorder<R>(mut self, recorder: R) -> FanoutBuilder
Expand Down
3 changes: 2 additions & 1 deletion metrics-util/src/layers/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, Share
/// Filters and discards metrics matching certain name patterns.
///
/// More information on the behavior of the layer can be found in [`FilterLayer`].
#[derive(Debug)]
pub struct Filter<R> {
inner: R,
automaton: AhoCorasick,
Expand Down Expand Up @@ -73,7 +74,7 @@ impl<R: Recorder> Recorder for Filter<R> {
/// DFA, or case sensitivity.
///
/// [ahocorasick]: https://en.wikipedia.org/wiki/Aho–Corasick_algorithm
#[derive(Default)]
#[derive(Default, Debug)]
pub struct FilterLayer {
patterns: Vec<String>,
case_insensitive: bool,
Expand Down
5 changes: 3 additions & 2 deletions metrics-util/src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! # use metrics::NoopRecorder as BasicRecorder;
//! # use metrics_util::layers::{Layer, Stack, PrefixLayer};
//! // A simple layer that denies any metrics that have "stairway" or "heaven" in their name.
//! #[derive(Default)]
//! #[derive(Default, Debug)]
//! pub struct StairwayDeny<R>(pub(crate) R);
//!
//! impl<R> StairwayDeny<R> {
Expand Down Expand Up @@ -75,7 +75,7 @@
//! }
//! }
//!
//! #[derive(Default)]
//! #[derive(Debug, Default)]
//! pub struct StairwayDenyLayer;
//!
//! impl<R> Layer<R> for StairwayDenyLayer {
Expand Down Expand Up @@ -137,6 +137,7 @@ pub trait Layer<R> {
}

/// Builder for composing layers together in a top-down/inside-out order.
#[derive(Debug)]
pub struct Stack<R> {
inner: R,
}
Expand Down
2 changes: 2 additions & 0 deletions metrics-util/src/layers/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, Share
/// Applies a prefix to every metric key.
///
/// Keys will be prefixed in the format of `<prefix>.<remaining>`.
#[derive(Debug)]
pub struct Prefix<R> {
prefix: SharedString,
inner: R,
Expand Down Expand Up @@ -64,6 +65,7 @@ impl<R: Recorder> Recorder for Prefix<R> {
/// A layer for applying a prefix to every metric key.
///
/// More information on the behavior of the layer can be found in [`Prefix`].
#[derive(Debug)]
pub struct PrefixLayer(&'static str);

impl PrefixLayer {
Expand Down
26 changes: 26 additions & 0 deletions metrics-util/src/layers/router.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit};
use radix_trie::{Trie, TrieCommon};

Expand All @@ -15,6 +17,17 @@ pub struct Router {
histogram_routes: Trie<String, usize>,
}

impl fmt::Debug for Router {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Router")
.field("global_mask", &self.global_mask)
.field("targets_len", &self.targets.len())
.field("counter_routes", &self.counter_routes)
.field("gauge_routes", &self.gauge_routes)
.field("histogram_routes", &self.histogram_routes)
.finish_non_exhaustive()
}
}
impl Router {
fn route(
&self,
Expand Down Expand Up @@ -87,6 +100,18 @@ pub struct RouterBuilder {
histogram_routes: Trie<String, usize>,
}

impl fmt::Debug for RouterBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RouterBuilder")
.field("global_mask", &self.global_mask)
.field("targets_len", &self.targets.len())
.field("counter_routes", &self.counter_routes)
.field("gauge_routes", &self.gauge_routes)
.field("histogram_routes", &self.histogram_routes)
.finish_non_exhaustive()
}
}

impl RouterBuilder {
/// Creates a [`RouterBuilder`] from a [`Recorder`].
///
Expand Down Expand Up @@ -175,6 +200,7 @@ mod tests {
};

mock! {
#[derive(Debug)]
pub TestRecorder {
}

Expand Down
Loading

0 comments on commit a56fc2f

Please sign in to comment.