metrics-derive 0.1.2

Proc macro for automatically describing and initializing metrics
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use crate::{
    metric::{LabelPair, Metric},
    with_attrs::WithAttrs,
};
use quote::{quote, ToTokens};
use syn::{
    punctuated::Punctuated, Attribute, Data, DeriveInput, Error, Expr, ExprTuple, Field, Lit,
    LitBool, LitStr, Meta, MetaNameValue, Result, Token,
};

enum MetricField<'a> {
    Included(Metric<'a>),
    Skipped(&'a Field),
}

impl<'a> MetricField<'a> {
    const fn field(&self) -> &'a Field {
        match self {
            MetricField::Included(Metric { field, .. }) | MetricField::Skipped(field) => field,
        }
    }
}

pub(crate) fn derive(node: &DeriveInput) -> Result<proc_macro2::TokenStream> {
    let ty = &node.ident;
    let vis = &node.vis;
    let ident_name = ty.to_string();

    let metrics_attr = parse_metrics_attr(node)?;
    let metric_fields = parse_metric_fields(node)?;

    let global_labels_init = if metrics_attr.labels.is_empty() {
        quote! {}
    } else {
        let label_keys = metrics_attr.labels.iter().map(|(k, _)| k);
        let label_values = metrics_attr.labels.iter().map(|(_, v)| v);
        quote! {
            __labels.extend([
                #(metrics::Label::new(#label_keys, #label_values)),*
            ]);
        }
    };

    let register_and_describe = match &metrics_attr.scope {
        MetricsScope::Static(scope) => {
            let mut field_inits = Vec::with_capacity(metric_fields.len());
            let mut describes = Vec::with_capacity(metric_fields.len());
            let mut partial_clone_inits = Vec::with_capacity(metric_fields.len());

            for metric in &metric_fields {
                let field_name = &metric.field().ident;
                match metric {
                    MetricField::Included(metric) => {
                        let metric_name = format!(
                            "{}{}{}",
                            scope.value(),
                            metrics_attr.separator(),
                            metric.name()
                        );
                        let register_method = metric.register_method()?;
                        let describe_method = metric.describe_method()?;
                        let description = &metric.description;

                        let field_init = if metric.labels.is_empty() {
                            quote! {
                                __recorder.#register_method(
                                    &metrics::Key::from_parts(#metric_name, __labels.clone()),
                                    __metadata,
                                )
                            }
                        } else {
                            let label_keys = metric.labels.iter().map(|(k, _)| k);
                            let label_values = metric.labels.iter().map(|(_, v)| v);
                            quote! {
                                {
                                    let mut __field_labels = __labels.clone();
                                    __field_labels.extend([
                                        #(metrics::Label::new(#label_keys, #label_values)),*
                                    ]);
                                    __recorder.#register_method(
                                        &metrics::Key::from_parts(#metric_name, __field_labels),
                                        __metadata,
                                    )
                                }
                            }
                        };

                        field_inits.push(quote! {
                            #field_name: #field_init,
                        });
                        describes.push(quote! {
                            __recorder.#describe_method(
                                ::core::convert::Into::into(#metric_name),
                                ::core::option::Option::None,
                                ::core::convert::Into::into(#description),
                            );
                        });
                        partial_clone_inits.push(quote! {
                            #field_name: ::core::clone::Clone::clone(&self.#field_name),
                        });
                    }
                    MetricField::Skipped(_) => {
                        field_inits.push(quote! {
                            #field_name: Default::default(),
                        });
                        partial_clone_inits.push(quote! {
                            #field_name: Default::default(),
                        });
                    }
                }
            }

            quote! {
                impl Default for #ty {
                    /// Creates a new instance of the metrics.
                    ///
                    /// This initializes all metrics and registers them with the global recorder.
                    /// Metrics are described only once; see [`Self::describe`].
                    ///
                    /// The result is cached in a static `OnceLock`, so subsequent calls will
                    /// return a clone of the same instance.
                    fn default() -> Self {
                        static __ONCE: ::std::sync::OnceLock<#ty> = ::std::sync::OnceLock::new();
                        __ONCE.get_or_init(|| {
                            Self::_new_with_labels(::std::vec::Vec::<metrics::Label>::new())
                        })._partial_clone()
                    }
                }

                impl #ty {
                    /// Creates a new instance of the metrics with the provided labels.
                    ///
                    /// Unlike [`Self::default`], this does not cache the result, so it can be used
                    /// to re-register metrics or register with different labels.
                    #vis fn new_with_labels(labels: impl metrics::IntoLabels) -> Self {
                        Self::_new_with_labels(labels.into_labels())
                    }

                    fn _new_with_labels(labels: ::std::vec::Vec<metrics::Label>) -> Self {
                        Self::describe();
                        metrics::with_recorder(|__recorder| {
                            static __METADATA: metrics::Metadata<'static> = metrics::Metadata::new(
                                module_path!(),
                                metrics::Level::INFO,
                                ::core::option::Option::Some(module_path!()),
                            );
                            let __metadata = &__METADATA;
                            #[allow(unused_mut)]
                            let mut __labels = labels;
                            #global_labels_init
                            Self {
                                #(#field_inits)*
                            }
                        })
                    }

                    /// Describe all exposed metrics.
                    ///
                    /// Internally, this calls the `describe_*` macros from the metrics crate
                    /// according to the metric type.
                    ///
                    /// This is done only once, to avoid multiple `describe` calls to the same
                    /// recorder. If this is not preferred, you can call [`Self::force_describe`]
                    /// directly.
                    ///
                    /// See: <https://docs.rs/metrics/latest/metrics/index.html#macros>
                    #vis fn describe() {
                        static __DESCRIBE_ONCE: ::std::sync::Once = ::std::sync::Once::new();
                        __DESCRIBE_ONCE.call_once(Self::force_describe);
                    }

                    /// Unconditionally describes all metrics.
                    ///
                    /// See [`Self::describe`].
                    #vis fn force_describe() {
                        metrics::with_recorder(|__recorder| {
                            #(#describes)*
                        });
                    }

                    fn _partial_clone(&self) -> Self {
                        Self {
                            #(#partial_clone_inits)*
                        }
                    }
                }
            }
        }
        MetricsScope::Dynamic => {
            let mut field_inits = Vec::with_capacity(metric_fields.len());
            let mut describes = Vec::with_capacity(metric_fields.len());

            for metric in &metric_fields {
                let field_name = &metric.field().ident;
                match metric {
                    MetricField::Included(metric) => {
                        let name = metric.name();
                        let separator = metrics_attr.separator();

                        let register_method = metric.register_method()?;
                        let describe_method = metric.describe_method()?;
                        let description = &metric.description;

                        let field_init = if metric.labels.is_empty() {
                            quote! {
                                __recorder.#register_method(
                                    &metrics::Key::from_parts(
                                        format!("{}{}{}", __scope, #separator, #name),
                                        __labels.clone(),
                                    ),
                                    __metadata,
                                )
                            }
                        } else {
                            let label_keys = metric.labels.iter().map(|(k, _)| k);
                            let label_values = metric.labels.iter().map(|(_, v)| v);
                            quote! {
                                {
                                    let mut __field_labels = __labels.clone();
                                    __field_labels.extend([
                                        #(metrics::Label::new(#label_keys, #label_values)),*
                                    ]);
                                    __recorder.#register_method(
                                        &metrics::Key::from_parts(
                                            format!("{}{}{}", __scope, #separator, #name),
                                            __field_labels,
                                        ),
                                        __metadata,
                                    )
                                }
                            }
                        };

                        field_inits.push(quote! {
                            #field_name: #field_init,
                        });
                        describes.push(quote! {
                            __recorder.#describe_method(
                                ::core::convert::Into::into(format!("{}{}{}", __scope, #separator, #name)),
                                ::core::option::Option::None,
                                ::core::convert::Into::into(#description),
                            );
                        });
                    }
                    MetricField::Skipped(_) => {
                        field_inits.push(quote! {
                            #field_name: Default::default(),
                        });
                    }
                }
            }

            quote! {
                impl #ty {
                    /// Creates a new instance of the metrics with the provided scope.
                    ///
                    /// This initializes all metrics and registers them with the global recorder.
                    #vis fn new(scope: &str) -> Self {
                        Self::_new_with_labels(scope, ::std::vec::Vec::<metrics::Label>::new())
                    }

                    /// Creates a new instance of the metrics with the provided scope and labels.
                    #vis fn new_with_labels(scope: &str, labels: impl metrics::IntoLabels) -> Self {
                        Self::_new_with_labels(scope, labels.into_labels())
                    }

                    fn _new_with_labels(scope: &str, labels: ::std::vec::Vec<metrics::Label>) -> Self {
                        Self::describe(scope);
                        metrics::with_recorder(|__recorder| {
                            static __METADATA: metrics::Metadata<'static> = metrics::Metadata::new(
                                module_path!(),
                                metrics::Level::INFO,
                                ::core::option::Option::Some(module_path!()),
                            );
                            let __metadata = &__METADATA;
                            let __scope = scope;
                            #[allow(unused_mut)]
                            let mut __labels = labels;
                            #global_labels_init
                            Self {
                                #(#field_inits)*
                            }
                        })
                    }

                    /// Describe all exposed metrics. Internally calls `describe_*` macros from
                    /// the metrics crate according to the metric type.
                    ///
                    /// See: <https://docs.rs/metrics/latest/metrics/index.html#macros>
                    #vis fn describe(scope: &str) {
                        metrics::with_recorder(|__recorder| {
                            let __scope = scope;
                            #(#describes)*
                        });
                    }
                }
            }
        }
    };

    Ok(quote! {
        #register_and_describe

        impl ::core::fmt::Debug for #ty {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                f.debug_struct(#ident_name).finish()
            }
        }
    })
}

pub(crate) struct MetricsAttr {
    pub(crate) scope: MetricsScope,
    pub(crate) separator: Option<LitStr>,
    pub(crate) labels: Vec<LabelPair>,
}

impl MetricsAttr {
    const DEFAULT_SEPARATOR: &'static str = ".";

    fn separator(&self) -> String {
        match &self.separator {
            Some(sep) => sep.value(),
            None => Self::DEFAULT_SEPARATOR.to_owned(),
        }
    }
}

pub(crate) enum MetricsScope {
    Static(LitStr),
    Dynamic,
}

fn parse_metrics_attr(node: &DeriveInput) -> Result<MetricsAttr> {
    let metrics_attr = parse_single_required_attr(node, "metrics")?;
    let parsed =
        metrics_attr.parse_args_with(Punctuated::<MetaNameValue, Token![,]>::parse_terminated)?;
    let (mut scope, mut separator, mut dynamic, mut labels) = (None, None, None, None);
    for kv in parsed {
        if kv.path.is_ident("labels") {
            if labels.is_some() {
                return Err(Error::new_spanned(kv, "duplicate `labels` value provided"));
            }
            labels = Some(parse_labels_expr(&kv.value)?);
            continue;
        }

        let lit = match kv.value {
            Expr::Lit(ref expr) => &expr.lit,
            _ => return Err(Error::new_spanned(&kv.value, "value must be a literal")),
        };
        if kv.path.is_ident("scope") {
            if scope.is_some() {
                return Err(Error::new_spanned(kv, "duplicate `scope` value provided"));
            }
            scope = Some(parse_str_lit(lit)?);
        } else if kv.path.is_ident("separator") {
            if separator.is_some() {
                return Err(Error::new_spanned(kv, "duplicate `separator` value provided"));
            }
            separator = Some(parse_str_lit(lit)?);
        } else if kv.path.is_ident("dynamic") {
            if dynamic.is_some() {
                return Err(Error::new_spanned(kv, "duplicate `dynamic` flag provided"));
            }
            dynamic = Some(parse_bool_lit(lit)?.value);
        } else {
            return Err(Error::new_spanned(kv, "unsupported attribute entry"));
        }
    }

    let scope = match (scope, dynamic) {
        (Some(scope), None | Some(false)) => MetricsScope::Static(scope),
        (None, Some(true)) => MetricsScope::Dynamic,
        (Some(_), Some(_)) => {
            return Err(Error::new_spanned(node, "`scope = ..` conflicts with `dynamic = true`"))
        }
        _ => {
            return Err(Error::new_spanned(
                node,
                "either `scope = ..` or `dynamic = true` must be set",
            ))
        }
    };

    let labels = labels.unwrap_or_default();
    Ok(MetricsAttr { scope, separator, labels })
}

fn parse_metric_fields(node: &DeriveInput) -> Result<Vec<MetricField<'_>>> {
    let Data::Struct(ref data) = node.data else {
        return Err(Error::new_spanned(node, "only structs are supported"));
    };

    let mut metrics = Vec::with_capacity(data.fields.len());
    for field in &data.fields {
        let (mut describe, mut rename, mut labels, mut skip) = (None, None, None, false);
        if let Some(metric_attr) = parse_single_attr(field, "metric")? {
            let parsed =
                metric_attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
            for meta in parsed {
                match meta {
                    Meta::Path(path) if path.is_ident("skip") => skip = true,
                    Meta::NameValue(kv) => {
                        if kv.path.is_ident("labels") {
                            if labels.is_some() {
                                return Err(Error::new_spanned(
                                    kv,
                                    "duplicate `labels` value provided",
                                ));
                            }
                            labels = Some(parse_labels_expr(&kv.value)?);
                        } else if kv.path.is_ident("describe") {
                            if describe.is_some() {
                                return Err(Error::new_spanned(
                                    kv,
                                    "duplicate `describe` value provided",
                                ));
                            }
                            let lit = parse_expr_lit(&kv.value)?;
                            describe = Some(parse_str_lit(lit)?);
                        } else if kv.path.is_ident("rename") {
                            if rename.is_some() {
                                return Err(Error::new_spanned(
                                    kv,
                                    "duplicate `rename` value provided",
                                ));
                            }
                            let lit = parse_expr_lit(&kv.value)?;
                            rename = Some(parse_str_lit(lit)?);
                        } else {
                            return Err(Error::new_spanned(kv, "unsupported attribute entry"));
                        }
                    }
                    _ => return Err(Error::new_spanned(meta, "unsupported attribute entry")),
                }
            }
        }

        if skip {
            metrics.push(MetricField::Skipped(field));
            continue;
        }

        let description = match describe {
            Some(lit_str) => lit_str.value(),
            // Parse docs only if `describe` attribute was not provided
            None => match parse_docs_to_string(field)? {
                Some(docs_str) => docs_str,
                None => {
                    return Err(Error::new_spanned(
                        field,
                        "either doc comment or `describe = ..` must be set",
                    ))
                }
            },
        };

        let labels = labels.unwrap_or_default();
        metrics.push(MetricField::Included(Metric::new(field, description, rename, labels)));
    }

    Ok(metrics)
}

fn parse_single_attr<'a, T: WithAttrs + ToTokens>(
    token: &'a T,
    ident: &str,
) -> Result<Option<&'a Attribute>> {
    let mut attr_iter = token.attrs().iter().filter(|a| a.path().is_ident(ident));
    if let Some(attr) = attr_iter.next() {
        if let Some(next_attr) = attr_iter.next() {
            Err(Error::new_spanned(
                next_attr,
                format!("duplicate `#[{ident}(..)]` attribute provided"),
            ))
        } else {
            Ok(Some(attr))
        }
    } else {
        Ok(None)
    }
}

fn parse_single_required_attr<'a, T: WithAttrs + ToTokens>(
    token: &'a T,
    ident: &str,
) -> Result<&'a Attribute> {
    if let Some(attr) = parse_single_attr(token, ident)? {
        Ok(attr)
    } else {
        Err(Error::new_spanned(token, format!("`#[{ident}(..)]` attribute must be provided")))
    }
}

fn parse_docs_to_string<T: WithAttrs>(token: &T) -> Result<Option<String>> {
    let mut doc_str = None;
    for attr in token.attrs() {
        if let syn::Meta::NameValue(ref meta) = attr.meta {
            if let Expr::Lit(ref lit) = meta.value {
                if let Lit::Str(ref doc) = lit.lit {
                    let doc_value = doc.value().trim().to_string();
                    doc_str = Some(
                        doc_str
                            .map(|prev_doc_value| format!("{prev_doc_value} {doc_value}"))
                            .unwrap_or(doc_value),
                    );
                }
            }
        }
    }
    Ok(doc_str)
}

fn parse_str_lit(lit: &Lit) -> Result<LitStr> {
    match lit {
        Lit::Str(lit_str) => Ok(lit_str.to_owned()),
        _ => Err(Error::new_spanned(lit, "value must be a string literal")),
    }
}

fn parse_bool_lit(lit: &Lit) -> Result<LitBool> {
    match lit {
        Lit::Bool(lit_bool) => Ok(lit_bool.to_owned()),
        _ => Err(Error::new_spanned(lit, "value must be a boolean literal")),
    }
}

fn parse_expr_lit(expr: &Expr) -> Result<&Lit> {
    match expr {
        Expr::Lit(expr_lit) => Ok(&expr_lit.lit),
        _ => Err(Error::new_spanned(expr, "value must be a literal")),
    }
}

/// Parses `labels = [("key", expr), ...]` into a vector of label pairs.
/// Keys must be string literals; values can be arbitrary expressions.
fn parse_labels_expr(expr: &Expr) -> Result<Vec<LabelPair>> {
    let Expr::Array(arr) = expr else {
        return Err(Error::new_spanned(
            expr,
            "labels must be an array of tuples, e.g. `labels = [(\"key\", \"value\")]`",
        ));
    };

    let mut labels = Vec::with_capacity(arr.elems.len());
    let mut seen_keys = std::collections::HashSet::new();

    for elem in &arr.elems {
        let Expr::Tuple(ExprTuple { elems, .. }) = elem else {
            return Err(Error::new_spanned(
                elem,
                "each label must be a tuple, e.g. `(\"key\", \"value\")` or `(\"key\", CONST)`",
            ));
        };

        if elems.len() != 2 {
            return Err(Error::new_spanned(
                elem,
                "each label tuple must have exactly two elements: (key, value)",
            ));
        }

        // Key must be a string literal
        let key = parse_str_lit(parse_expr_lit(&elems[0])?)?;

        // Check for duplicate keys
        let key_str = key.value();
        if !seen_keys.insert(key_str.clone()) {
            return Err(Error::new_spanned(&elems[0], format!("duplicate label key `{key_str}`")));
        }

        // Value can be any expression (string literal, constant, etc.)
        let value = elems[1].clone();
        labels.push((key, value));
    }

    Ok(labels)
}