1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Easily registered distributed metrics.
//!
//! You should usually be using the [`metriken`] crate instead. This crate
//! contains the core distributed slice used by [`metriken`] so that multiple
//! major versions of [`metriken`]` can coexist.
//!
//! [`metriken`]: https://docs.rs/metriken

use std::any::Any;
use std::borrow::Cow;

/// A helper macro for marking imports as being used.
///
/// This is meant to be used for when a reference is made to an item from a doc
/// comment but that item isn't actually used for code anywhere.
macro_rules! used_in_docs {
    ($($item:ident),* $(,)?) => {
        const _: () = {
            #[allow(unused_imports)]
            mod _docs {
                $( use super::$item; )*
            }
        };
    };
}

pub mod dynmetrics;
mod formatter;
mod metadata;
mod metrics;
mod null;

pub use crate::formatter::{default_formatter, Format};
pub use crate::metadata::{Metadata, MetadataIter};
pub use crate::metrics::{metrics, DynMetricsIter, Metrics, MetricsIter};

/// Global interface to a metric.
///
/// Most use of metrics should use the directly declared constants.
pub trait Metric: Send + Sync + 'static {
    /// Indicate whether this metric has been set up.
    ///
    /// Generally, if this returns `false` then the other methods on this
    /// trait should return `None`.
    fn is_enabled(&self) -> bool {
        self.as_any().is_some()
    }

    /// Get the current metric as an [`Any`] instance. This is meant to allow
    /// custom processing for known metric types.
    ///
    /// [`Any`]: std::any::Any
    fn as_any(&self) -> Option<&dyn Any>;

    /// Get the value of the current metric, should it be enabled.
    ///
    /// # Note to Implementors
    /// If your metric's value does not correspond to one of the variants of
    /// [`Value`] then return [`Value::Other`] and metric consumers can use
    /// [`as_any`](crate::Metric::as_any) to specifically handle your metric.
    fn value(&self) -> Option<Value>;
}

/// The value of a metric.
///
/// See [`Metric::value`].
#[non_exhaustive]
pub enum Value<'a> {
    /// A counter value.
    Counter(u64),

    /// A gauge value.
    Gauge(i64),

    /// The value of the metric could not be represented using the other `Value`
    /// variants.
    ///
    /// Use [`Metric::as_any`] to specifically handle the type of this metric.
    Other(&'a dyn Any),
}

/// A statically declared metric entry.
pub struct MetricEntry {
    metric: *const dyn Metric,
    name: Cow<'static, str>,
    description: Option<Cow<'static, str>>,
    metadata: Metadata,
    formatter: fn(&Self, Format) -> String,
}

impl MetricEntry {
    /// Get a reference to the metric that this entry corresponds to.
    pub fn metric(&self) -> &dyn Metric {
        unsafe { &*self.metric }
    }

    /// Get the name of this metric.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the description of this metric.
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    /// Access the [`Metadata`] associated with this metrics entry.
    pub fn metadata(&self) -> &Metadata {
        &self.metadata
    }

    /// Format the metric into a string with the given format.
    pub fn formatted(&self, format: Format) -> String {
        (self.formatter)(self, format)
    }

    /// Checks whether `metric` is the metric for this entry.
    ///
    /// This checks both the type id and the address. Note that it may have
    /// false positives if `metric` is a ZST since multiple ZSTs may share
    /// the same address.
    pub fn is(&self, metric: &dyn Metric) -> bool {
        if self.metric().type_id() != metric.type_id() {
            return false;
        }

        let a = self.metric() as *const _ as *const ();
        let b = metric as *const _ as *const ();
        a == b
    }
}

unsafe impl Send for MetricEntry {}
unsafe impl Sync for MetricEntry {}

impl std::ops::Deref for MetricEntry {
    type Target = dyn Metric;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.metric()
    }
}

impl std::fmt::Debug for MetricEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MetricEntry")
            .field("name", &self.name())
            .field("metric", &"<dyn Metric>")
            .finish()
    }
}

/// Implementation detail exports for use by the `#[metric]`
#[doc(hidden)]
pub mod export {
    use crate::{Metadata, Metric};

    pub extern crate linkme;
    pub extern crate phf;

    #[linkme::distributed_slice]
    pub static METRICS: [crate::MetricEntry] = [..];

    pub const fn entry_v1(
        metric: &'static dyn Metric,
        name: &'static str,
        description: Option<&'static str>,
        metadata: &'static phf::Map<&'static str, &'static str>,
        formatter: fn(&crate::MetricEntry, crate::Format) -> String,
    ) -> crate::MetricEntry {
        use std::borrow::Cow;

        crate::MetricEntry {
            metric,
            name: Cow::Borrowed(name),
            description: match description {
                Some(desc) => Some(Cow::Borrowed(desc)),
                None => None,
            },
            metadata: Metadata::new_static(metadata),
            formatter,
        }
    }
}

/// Declare a new metric.
#[macro_export]
macro_rules! declare_metric_v1 {
    {
        metric: $metric:expr,
        name: $name:expr,
        description: $description:expr,
        metadata: { $( $key:expr => $value:expr ),* $(,)? },
        formatter: $formatter:expr $(,)?
    } => {
        const _: () = {
            use $crate::export::phf;

            static __METADATA: $crate::export::phf::Map<&'static str, &'static str> =
                $crate::export::phf::phf_map! { $( $key => $value, )* };

            #[$crate::export::linkme::distributed_slice($crate::export::METRICS)]
            #[linkme(crate = $crate::export::linkme)]
            static __ENTRY: $crate::MetricEntry = $crate::export::entry_v1(
                &$metric,
                $name,
                $description,
                &__METADATA,
                $formatter
            );
        };
    }
}