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
use quote::{quote, ToTokens};
use regex::Regex;
use std::sync::LazyLock;
use syn::{
    punctuated::Punctuated, Attribute, Data, DeriveInput, Error, Expr, Field, Lit, LitBool, LitStr,
    Meta, MetaNameValue, Result, Token,
};

use crate::{metric::Metric, with_attrs::WithAttrs};

/// Metric name regex according to Prometheus data model
///
/// See <https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels>
static METRIC_NAME_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^[a-zA-Z_:.][a-zA-Z0-9_:.]*$").unwrap());

/// Supported metrics separators
const SUPPORTED_SEPARATORS: &[&str] = &[".", "_", ":"];

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 describe_doc = quote! {
        /// Describe all exposed metrics. Internally calls `describe_*` macros from
        /// the metrics crate according to the metric type.
        ///
        /// See <https://docs.rs/metrics/0.20.1/metrics/index.html#macros>
    };
    let register_and_describe = match &metrics_attr.scope {
        MetricsScope::Static(scope) => {
            let (defaults, labeled_defaults, describes): (Vec<_>, Vec<_>, Vec<_>) = metric_fields
                .iter()
                .map(|metric| {
                    let field_name = &metric.field().ident;
                    match metric {
                        MetricField::Included(metric) => {
                            let metric_name = format!(
                                "{}{}{}",
                                scope.value(),
                                metrics_attr.separator(),
                                metric.name()
                            );
                            let registrar = metric.register_stmt()?;
                            let describe = metric.describe_stmt()?;
                            let description = &metric.description;
                            Ok((
                                quote! {
                                    #field_name: #registrar(#metric_name),
                                },
                                quote! {
                                    #field_name: #registrar(#metric_name, labels.clone()),
                                },
                                Some(quote! {
                                    #describe(#metric_name, #description);
                                }),
                            ))
                        }
                        MetricField::Skipped(_) => Ok((
                            quote! {
                                #field_name: Default::default(),
                            },
                            quote! {
                                #field_name: Default::default(),
                            },
                            None,
                        )),
                    }
                })
                .collect::<Result<Vec<_>>>()?
                .into_iter()
                .fold((vec![], vec![], vec![]), |mut acc, x| {
                    acc.0.push(x.0);
                    acc.1.push(x.1);
                    if let Some(describe) = x.2 {
                        acc.2.push(describe);
                    }
                    acc
                });

            quote! {
                impl Default for #ty {
                    fn default() -> Self {
                        #ty::describe();

                        Self {
                            #(#defaults)*
                        }
                    }
                }

                impl #ty {
                    /// Create new instance of metrics with provided labels.
                    #vis fn new_with_labels(labels: impl metrics::IntoLabels + Clone) -> Self {
                        Self {
                            #(#labeled_defaults)*
                        }
                    }

                    #describe_doc
                    #vis fn describe() {
                        #(#describes)*
                    }
                }
            }
        }
        MetricsScope::Dynamic => {
            let (defaults, labeled_defaults, describes): (Vec<_>, Vec<_>, Vec<_>) = metric_fields
                .iter()
                .map(|metric| {
                    let field_name = &metric.field().ident;
                    match metric {
                        MetricField::Included(metric) => {
                            let name = metric.name();
                            let separator = metrics_attr.separator();
                            let metric_name = quote! {
                                format!("{}{}{}", scope, #separator, #name)
                            };

                            let registrar = metric.register_stmt()?;
                            let describe = metric.describe_stmt()?;
                            let description = &metric.description;

                            Ok((
                                quote! {
                                    #field_name: #registrar(#metric_name),
                                },
                                quote! {
                                    #field_name: #registrar(#metric_name, labels.clone()),
                                },
                                Some(quote! {
                                    #describe(#metric_name, #description);
                                }),
                            ))
                        }
                        MetricField::Skipped(_) => Ok((
                            quote! {
                                #field_name: Default::default(),
                            },
                            quote! {
                                #field_name: Default::default(),
                            },
                            None,
                        )),
                    }
                })
                .collect::<Result<Vec<_>>>()?
                .into_iter()
                .fold((vec![], vec![], vec![]), |mut acc, x| {
                    acc.0.push(x.0);
                    acc.1.push(x.1);
                    if let Some(describe) = x.2 {
                        acc.2.push(describe);
                    }
                    acc
                });

            quote! {
                impl #ty {
                    /// Create new instance of metrics with provided scope.
                    #vis fn new(scope: &str) -> Self {
                        #ty::describe(scope);

                        Self {
                            #(#defaults)*
                        }
                    }

                    /// Create new instance of metrics with provided labels.
                    #vis fn new_with_labels(scope: &str, labels: impl metrics::IntoLabels + Clone) -> Self {
                        Self {
                            #(#labeled_defaults)*
                        }
                    }

                    #describe_doc
                    #vis fn describe(scope: &str) {
                        #(#describes)*
                    }
                }
            }
        }
    };
    Ok(quote! {
        #register_and_describe

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

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

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) = (None, None, None);
    for kv in parsed {
        let lit = match kv.value {
            Expr::Lit(ref expr) => &expr.lit,
            _ => continue,
        };
        if kv.path.is_ident("scope") {
            if scope.is_some() {
                return Err(Error::new_spanned(kv, "Duplicate `scope` value provided."))
            }
            let scope_lit = parse_str_lit(lit)?;
            validate_metric_name(&scope_lit)?;
            scope = Some(scope_lit);
        } else if kv.path.is_ident("separator") {
            if separator.is_some() {
                return Err(Error::new_spanned(kv, "Duplicate `separator` value provided."))
            }
            let separator_lit = parse_str_lit(lit)?;
            if !SUPPORTED_SEPARATORS.contains(&&*separator_lit.value()) {
                return Err(Error::new_spanned(
                    kv,
                    format!(
                        "Unsupported `separator` value. Supported: {}.",
                        SUPPORTED_SEPARATORS
                            .iter()
                            .map(|sep| format!("`{sep}`"))
                            .collect::<Vec<_>>()
                            .join(", ")
                    ),
                ))
            }
            separator = Some(separator_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.",
            ))
        }
    };

    Ok(MetricsAttr { scope, separator })
}

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 skip) = (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) => {
                        let lit = match kv.value {
                            Expr::Lit(ref expr) => &expr.lit,
                            _ => continue,
                        };
                        if kv.path.is_ident("describe") {
                            if describe.is_some() {
                                return Err(Error::new_spanned(
                                    kv,
                                    "Duplicate `describe` value provided.",
                                ))
                            }
                            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 rename_lit = parse_str_lit(lit)?;
                            validate_metric_name(&rename_lit)?;
                            rename = Some(rename_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.",
                    ))
                }
            },
        };

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

    Ok(metrics)
}

fn validate_metric_name(name: &LitStr) -> Result<()> {
    if METRIC_NAME_RE.is_match(&name.value()) {
        Ok(())
    } else {
        Err(Error::new_spanned(name, format!("Value must match regex {}", METRIC_NAME_RE.as_str())))
    }
}

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 string literal.")),
    }
}