|
14 | 14 |
|
15 | 15 | //! Reusable label templates for metric extension. |
16 | 16 | //! |
17 | | -//! [`LabelNames<N>`] holds label *names* at compile time; pair them with |
18 | | -//! values via the [`label_values!`] macro to get a [`Labels<N>`] that |
19 | | -//! the `labels:` macro arm can consume. This avoids repeating the same |
20 | | -//! label names at every call site and lets a single `Labels<N>` be |
21 | | -//! shared across counter, gauge, and histogram extensions. |
| 17 | +//! Labels can be created in two ways: |
| 18 | +//! |
| 19 | +//! - **[`labels!`]** — inline key-value pairs. Keys are always static |
| 20 | +//! literals; values can be static literals (const-compatible, zero |
| 21 | +//! allocation) or dynamic expressions (`Into<SharedString>`). |
| 22 | +//! |
| 23 | +//! - **[`label_names!`] + [`label_values!`]** — separate the label *names* |
| 24 | +//! (a const [`LabelNames<N>`]) from the *values*. This avoids repeating |
| 25 | +//! the same label names at every call site and lets a single |
| 26 | +//! [`Labels<N>`] be shared across counter, gauge, and histogram |
| 27 | +//! extensions. |
22 | 28 |
|
23 | 29 | use metrics::SharedString; |
24 | 30 |
|
@@ -69,15 +75,21 @@ macro_rules! label_values { |
69 | 75 | }; |
70 | 76 | } |
71 | 77 |
|
72 | | -/// Creates a const `Labels` from all-static key-value pairs. |
| 78 | +/// Creates a [`Labels<N>`] from key-value pairs. |
73 | 79 | /// |
74 | | -/// Every key and value must be `&'static str` literals. The result is a |
75 | | -/// `const` value — zero allocation, zero runtime cost. |
| 80 | +/// Keys are always `&'static str` literals. Values can be either |
| 81 | +/// string literals (const-compatible, zero allocation) or arbitrary |
| 82 | +/// expressions convertible to `SharedString` (dynamic, allocated at |
| 83 | +/// runtime). |
76 | 84 | /// |
77 | | -/// # Example |
| 85 | +/// # Examples |
78 | 86 | /// |
79 | 87 | /// ```rust,ignore |
| 88 | +/// // All-static — const-compatible, zero allocation: |
80 | 89 | /// const LABELS: Labels<2> = labels!("env" => "prod", "region" => "us-east-1"); |
| 90 | +/// |
| 91 | +/// // Dynamic values — any expression that implements Into<SharedString>: |
| 92 | +/// let labels = labels!("env" => "prod", "node_id" => node_id.to_string()); |
81 | 93 | /// ``` |
82 | 94 | #[macro_export] |
83 | 95 | macro_rules! labels { |
|
0 commit comments