autometrics 3.0.0

Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry.
Documentation
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::{constants::*, objectives::*, settings::get_settings};
#[cfg(prometheus_client)]
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue, LabelValueEncoder};

pub(crate) type Label = (&'static str, &'static str);
pub type ResultAndReturnTypeLabels = (&'static str, Option<&'static str>);

/// These are the labels used for the `build_info` metric.
#[cfg_attr(
    prometheus_client,
    derive(EncodeLabelSet, Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct BuildInfoLabels {
    pub(crate) branch: &'static str,
    pub(crate) commit: &'static str,
    pub(crate) version: &'static str,
    pub(crate) service_name: &'static str,
    pub(crate) repo_url: &'static str,
    pub(crate) repo_provider: &'static str,
    pub(crate) autometrics_version: &'static str,
}

impl BuildInfoLabels {
    pub fn new(version: &'static str, commit: &'static str, branch: &'static str) -> Self {
        Self {
            version,
            commit,
            branch,
            service_name: &get_settings().service_name,
            repo_url: &get_settings().repo_url,
            repo_provider: &get_settings().repo_provider,
            autometrics_version: AUTOMETRICS_SPEC_TARGET,
        }
    }

    pub fn to_vec(&self) -> Vec<Label> {
        vec![
            (COMMIT_KEY, self.commit),
            (VERSION_KEY, self.version),
            (BRANCH_KEY, self.branch),
            (SERVICE_NAME_KEY, self.service_name),
            (REPO_URL_KEY, self.repo_url),
            (REPO_PROVIDER_KEY, self.repo_provider),
            (AUTOMETRICS_VERSION_KEY, self.autometrics_version),
        ]
    }
}

/// These are the labels used for the `function.calls` metric.
#[cfg_attr(
    prometheus_client,
    derive(EncodeLabelSet, Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct CounterLabels {
    pub(crate) function: &'static str,
    pub(crate) module: &'static str,
    pub(crate) service_name: &'static str,
    pub(crate) caller_function: &'static str,
    pub(crate) caller_module: &'static str,
    pub(crate) result: Option<ResultLabel>,
    pub(crate) ok: Option<&'static str>,
    pub(crate) error: Option<&'static str>,
    pub(crate) objective_name: Option<&'static str>,
    pub(crate) objective_percentile: Option<ObjectivePercentile>,
}

#[cfg_attr(prometheus_client, derive(Debug, Clone, PartialEq, Eq, Hash))]
pub(crate) enum ResultLabel {
    Ok,
    Error,
}

impl ResultLabel {
    pub(crate) const fn as_str(&self) -> &'static str {
        match self {
            ResultLabel::Ok => OK_KEY,
            ResultLabel::Error => ERROR_KEY,
        }
    }
}

#[cfg(prometheus_client)]
impl EncodeLabelValue for ResultLabel {
    fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
        match self {
            ResultLabel::Ok => EncodeLabelValue::encode(&OK_KEY, encoder),
            ResultLabel::Error => EncodeLabelValue::encode(&ERROR_KEY, encoder),
        }
    }
}

impl CounterLabels {
    pub fn new(
        function: &'static str,
        module: &'static str,
        caller_function: &'static str,
        caller_module: &'static str,
        result: Option<ResultAndReturnTypeLabels>,
        objective: Option<Objective>,
    ) -> Self {
        let (objective_name, objective_percentile) = if let Some(objective) = objective {
            if let Some(success_rate) = objective.success_rate {
                (Some(objective.name), Some(success_rate))
            } else {
                (None, None)
            }
        } else {
            (None, None)
        };
        let (result, ok, error) = if let Some((result, return_value_type)) = result {
            match result {
                OK_KEY => (Some(ResultLabel::Ok), return_value_type, None),
                ERROR_KEY => (Some(ResultLabel::Error), None, return_value_type),
                _ => (None, None, None),
            }
        } else {
            (None, None, None)
        };
        Self {
            function,
            module,
            service_name: &get_settings().service_name,
            caller_function,
            caller_module,
            objective_name,
            objective_percentile,
            result,
            ok,
            error,
        }
    }

    pub fn to_vec(&self) -> Vec<Label> {
        let mut labels = vec![
            (FUNCTION_KEY, self.function),
            (MODULE_KEY, self.module),
            (SERVICE_NAME_KEY, self.service_name),
            (CALLER_FUNCTION_KEY, self.caller_function),
            (CALLER_MODULE_KEY, self.caller_module),
        ];
        if let Some(result) = &self.result {
            labels.push((RESULT_KEY, result.as_str()));
        }
        if let Some(ok) = self.ok {
            labels.push((OK_KEY, ok));
        }
        if let Some(error) = self.error {
            labels.push((ERROR_KEY, error));
        }
        if let Some(objective_name) = self.objective_name {
            labels.push((OBJECTIVE_NAME, objective_name));
        }
        if let Some(objective_percentile) = &self.objective_percentile {
            labels.push((OBJECTIVE_PERCENTILE, objective_percentile.as_str()));
        }

        labels
    }
}

/// These are the labels used for the `function.calls.duration` metric.
#[cfg_attr(
    prometheus_client,
    derive(EncodeLabelSet, Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct HistogramLabels {
    pub(crate) function: &'static str,
    pub(crate) module: &'static str,
    pub(crate) service_name: &'static str,
    pub(crate) objective_name: Option<&'static str>,
    pub(crate) objective_percentile: Option<ObjectivePercentile>,
    pub(crate) objective_latency_threshold: Option<ObjectiveLatency>,
}

impl HistogramLabels {
    pub fn new(function: &'static str, module: &'static str, objective: Option<Objective>) -> Self {
        let (objective_name, objective_percentile, objective_latency_threshold) =
            if let Some(objective) = objective {
                if let Some((latency, percentile)) = objective.latency {
                    (Some(objective.name), Some(percentile), Some(latency))
                } else {
                    (None, None, None)
                }
            } else {
                (None, None, None)
            };

        Self {
            function,
            module,
            service_name: &get_settings().service_name,
            objective_name,
            objective_percentile,
            objective_latency_threshold,
        }
    }

    pub fn to_vec(&self) -> Vec<Label> {
        let mut labels = vec![
            (FUNCTION_KEY, self.function),
            (MODULE_KEY, self.module),
            (SERVICE_NAME_KEY, self.service_name),
        ];

        if let Some(objective_name) = self.objective_name {
            labels.push((OBJECTIVE_NAME, objective_name));
        }
        if let Some(objective_percentile) = &self.objective_percentile {
            labels.push((OBJECTIVE_PERCENTILE, objective_percentile.as_str()));
        }
        if let Some(objective_latency_threshold) = &self.objective_latency_threshold {
            labels.push((
                OBJECTIVE_LATENCY_THRESHOLD,
                objective_latency_threshold.as_str(),
            ));
        }

        labels
    }
}

/// These are the labels used for the `function.calls.concurrent` metric.
#[cfg_attr(
    prometheus_client,
    derive(EncodeLabelSet, Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct GaugeLabels {
    pub(crate) function: &'static str,
    pub(crate) module: &'static str,
    pub(crate) service_name: &'static str,
}

impl GaugeLabels {
    pub fn new(function: &'static str, module: &'static str) -> Self {
        Self {
            function,
            module,
            service_name: &get_settings().service_name,
        }
    }

    pub fn to_array(&self) -> Vec<Label> {
        vec![
            (FUNCTION_KEY, self.function),
            (MODULE_KEY, self.module),
            (SERVICE_NAME_KEY, self.service_name),
        ]
    }
}

// The following is a convoluted way to figure out if the return type resolves to a Result
// or not. We cannot simply parse the code using syn to figure out if it's a Result
// because syn doesn't do type resolution and thus would count any renamed version
// of Result as a different type. Instead, we define two traits with intentionally
// conflicting method names and use a trick based on the order in which Rust resolves
// method names to return a different value based on whether the return value is
// a Result or anything else.
// This approach is based on dtolnay's answer to this question:
// https://users.rust-lang.org/t/how-to-check-types-within-macro/33803/5
// and this answer explains why it works:
// https://users.rust-lang.org/t/how-to-check-types-within-macro/33803/8

/// A trait to override the inferred label for the "result" of a function call.
pub trait GetLabels {
    fn __autometrics_get_labels(&self) -> Option<&'static str>;
}

/// Implement the given trait for &T and all primitive types.
macro_rules! impl_trait_for_types {
    ($trait:ident) => {
        impl<T> $trait for &T {}
        impl $trait for i8 {}
        impl $trait for i16 {}
        impl $trait for i32 {}
        impl $trait for i64 {}
        impl $trait for i128 {}
        impl $trait for isize {}
        impl $trait for u8 {}
        impl $trait for u16 {}
        impl $trait for u32 {}
        impl $trait for u64 {}
        impl $trait for u128 {}
        impl $trait for usize {}
        impl $trait for f32 {}
        impl $trait for f64 {}
        impl $trait for char {}
        impl $trait for bool {}
        impl $trait for str {}
        impl $trait for () {}
        impl<A> $trait for (A,) {}
        impl<A, B> $trait for (A, B) {}
        impl<A, B, C> $trait for (A, B, C) {}
        impl<A, B, C, D> $trait for (A, B, C, D) {}
        impl<A, B, C, D, E> $trait for (A, B, C, D, E) {}
        impl<A, B, C, D, E, F> $trait for (A, B, C, D, E, F) {}
        impl<A, B, C, D, E, F, G> $trait for (A, B, C, D, E, F, G) {}
        impl<A, B, C, D, E, F, G, H> $trait for (A, B, C, D, E, F, G, H) {}
        impl<A, B, C, D, E, F, G, H, I> $trait for (A, B, C, D, E, F, G, H, I) {}
        impl<A, B, C, D, E, F, G, H, I, J> $trait for (A, B, C, D, E, F, G, H, I, J) {}
        impl<A, B, C, D, E, F, G, H, I, J, K> $trait for (A, B, C, D, E, F, G, H, I, J, K) {}
        impl<A, B, C, D, E, F, G, H, I, J, K, L> $trait for (A, B, C, D, E, F, G, H, I, J, K, L) {}
    };
}

pub trait GetStaticStrFromIntoStaticStr<'a> {
    fn __autometrics_static_str(&'a self) -> Option<&'static str>;
}

impl<'a, T: 'a> GetStaticStrFromIntoStaticStr<'a> for T
where
    &'static str: From<&'a T>,
{
    fn __autometrics_static_str(&'a self) -> Option<&'static str> {
        Some(self.into())
    }
}

pub trait GetStaticStr {
    fn __autometrics_static_str(&self) -> Option<&'static str> {
        None
    }
}
impl_trait_for_types!(GetStaticStr);

/// Return the value of labels to use for the "result" counter according to
/// the value's exact type and attributes.
///
/// The macro uses the autoref specialization trick through spez to get the labels for the type in a variety of circumstances.
/// Specifically, if the value is a Result, it will add the ok or error label accordingly unless one or both of the types that
/// the Result<T, E> is generic over implements the GetLabels trait. The label allows to override the inferred label, and the
/// [`ResultLabels`](crate::ResultLabels) macro implements the GetLabels trait for the user using annotations.
///
/// The macro is meant to be called with a reference as argument: `get_result_labels_for_value(&return_value)`
///
/// See: <https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md>
#[doc(hidden)]
#[macro_export]
macro_rules! get_result_labels_for_value {
    ($e:expr) => {{
        use $crate::__private::{
            GetLabels, GetStaticStr, ResultAndReturnTypeLabels, ERROR_KEY, OK_KEY,
        };
        $crate::__private::spez! {
            for val = $e;

            match<T, E> &::std::result::Result<T, E> where T: GetLabels, E: GetLabels -> ::std::option::Option<ResultAndReturnTypeLabels> {
                match val {
                    Ok(ok) => Some((
                        ok.__autometrics_get_labels().unwrap_or(OK_KEY),
                        ok.__autometrics_static_str(),
                    )),
                    Err(err) => Some((
                        err.__autometrics_get_labels().unwrap_or(ERROR_KEY),
                        err.__autometrics_static_str(),
                    )),
                }
            }

            match<T, E> &::std::result::Result<T, E> where E: GetLabels -> ::std::option::Option<ResultAndReturnTypeLabels> {
                match val {
                    Ok(ok) => Some((
                        OK_KEY,
                        ok.__autometrics_static_str(),
                    )),
                    Err(err) => Some((
                        err.__autometrics_get_labels().unwrap_or(ERROR_KEY),
                        err.__autometrics_static_str(),
                    )),
                }
            }

            match<T, E> &::std::result::Result<T, E> where T: GetLabels -> ::std::option::Option<ResultAndReturnTypeLabels> {
                match val {
                    Ok(ok) => Some((
                        ok.__autometrics_get_labels().unwrap_or(OK_KEY),
                        ok.__autometrics_static_str(),
                    )),
                    Err(err) => Some((
                        ERROR_KEY,
                        err.__autometrics_static_str(),
                    )),
                }
            }

            match<T, E> &::std::result::Result<T, E> -> ::std::option::Option<ResultAndReturnTypeLabels> {
                match val {
                    Ok(ok) => Some((
                        OK_KEY,
                        ok.__autometrics_static_str(),
                    )),
                    Err(err) => Some((
                        ERROR_KEY,
                        err.__autometrics_static_str(),
                    )),
                }
            }

            match<T> &T where T: GetLabels -> ::std::option::Option<ResultAndReturnTypeLabels> {
                val.__autometrics_get_labels().map(|label| (label, val.__autometrics_static_str()))
            }

            match<T> T -> ::std::option::Option<ResultAndReturnTypeLabels> {
                None
            }
        }
    }};
}