encore/metrics.rs
1//! Metrics: counters, gauges and histograms of what an app does.
2//!
3//! A metric is declared as a `static` and used wherever the thing it measures
4//! happens. Its doc comment is its description:
5//!
6//! ```ignore
7//! use encore::metrics::{Counter, Gauge, Histogram, UpDownCounter};
8//!
9//! /// Orders placed.
10//! static ORDERS: Counter = Counter::new("orders_placed");
11//!
12//! /// Jobs waiting for a worker.
13//! static QUEUED: UpDownCounter = UpDownCounter::new("jobs_queued").unit("{job}");
14//!
15//! /// The temperature of the oven.
16//! static OVEN: Gauge = Gauge::new("oven_temperature").unit("Cel");
17//!
18//! /// How long an order takes to place.
19//! static LATENCY: Histogram = Histogram::new("order_latency").unit("s");
20//!
21//! ORDERS.increment();
22//! QUEUED.add(12);
23//! OVEN.set(180.5);
24//! LATENCY.record(started.elapsed().as_secs_f64());
25//! ```
26//!
27//! Each is an instrument of the [OpenTelemetry Metrics API](crate::opentelemetry::metrics),
28//! of the meter `encore`, named as declared, a service's own prefixed with
29//! its service's name (`orders_placed` of the service `shop` is exported as
30//! `shop.orders_placed`), created when it is first recorded to. Its data is
31//! exported with the rest of the app's: the process's resource names the
32//! service recording it. Instruments of the API itself made with Encore's
33//! [meter provider](meter_provider) are exported alongside: observable
34//! instruments are made with it. Encore installs no global meter provider:
35//!
36//! ```ignore
37//! use encore::opentelemetry::metrics::MeterProvider as _;
38//! let meter = encore::metrics::meter_provider().meter("my-app");
39//! let requests = meter.u64_counter("requests").build();
40//! requests.add(1, &[]);
41//! ```
42//!
43//! | Metric | What it is | Values |
44//! |---|---|---|
45//! | [`Counter`] | a total that only goes up | `u64`, `f64` |
46//! | [`UpDownCounter`] | a total that goes up and down | `i64`, `f64` |
47//! | [`Gauge`] | what something is right now | `f64`, `i64`, `u64` |
48//! | [`Histogram`] | how values are distributed | `f64` |
49//!
50//! The first type in each list is the default. A unit is given as it is to be
51//! exported: [UCUM](https://ucum.org/ucum) is the convention (`"s"`, `"By"`,
52//! `"{request}"`). A description given with `.description(…)`, where there is
53//! no doc comment, is the instrument's too.
54//!
55//! # Scope
56//!
57//! How a metric is declared says who records to it, as with
58//! [secrets](crate::secrets#scope):
59//!
60//! - `Counter::new("requests")`, `Gauge::new(…)`, … belongs to the service that
61//! declares it, and only that service records to it. Its name is unique in
62//! the service: two services may each declare a `requests` counter, exported
63//! as `<service>.requests`. It must be declared inside a service.
64//! - `Counter::new("signups").shared()` is shared between services, under one
65//! name in the app. Each service that records to it is recorded as using it.
66//! - `Counter::new("requests").global()` is the whole app's. Any code may
67//! record to it — a library outside every service too — and who does is not
68//! recorded.
69//!
70//! The groups, below, are scoped the same way: `CounterGroup::new("…").shared()`.
71//! The scope is chained with the other options in any order, and written out
72//! as the name is: Encore reads both from the source.
73//!
74//! ```ignore
75//! /// Users signed up, whichever service signed them up.
76//! static SIGNUPS: Counter = Counter::new("signups").shared().unit("{user}");
77//! ```
78//!
79//! A shared or global metric is declared once in the app, as a [shared
80//! secret](crate::secrets#scope) is. Other code records to it by importing
81//! the declaration, or, from another service's crate or another language, by
82//! binding it by name:
83//!
84//! ```ignore
85//! static SERVED: CounterGroup<Served> = CounterGroup::named("greetings.served");
86//! ```
87//!
88//! A named metric is of the declaration's kind and labels, and takes its
89//! scope, unit, description and buckets; it is exported under the name as
90//! written, never prefixed with a service's. To hand a metric to other code,
91//! pass [`reference()`](Counter::reference): it counts as recording to it.
92//!
93//! # Labels
94//!
95//! A metric that is one number is a [`Counter`], [`Gauge`], … A *group* —
96//! [`CounterGroup`], [`GaugeGroup`], … — is one number for each combination of
97//! its labels, which are the fields of a struct, each an attribute of what is
98//! recorded:
99//!
100//! ```ignore
101//! use encore::metrics::{CounterGroup, Labels};
102//!
103//! #[derive(Labels)]
104//! struct Outcome {
105//! method: &'static str,
106//! status: u16,
107//! }
108//!
109//! /// Requests handled, by method and status.
110//! static REQUESTS: CounterGroup<Outcome> = CounterGroup::new("requests_handled");
111//!
112//! REQUESTS.with(&Outcome { method: "GET", status: 200 }).increment();
113//! ```
114//!
115//! Every distinct combination is a time series of its own: a label is for a
116//! handful of values — a status, a region — and not for a user id. For
117//! attributes of any other shape, record with the OpenTelemetry API.
118//!
119//! # Histograms
120//!
121//! A [`Histogram`] counts what it records in buckets, to say how it is
122//! distributed: the median latency, the 99th percentile. Its
123//! [`buckets`](Histogram::buckets) are part of its definition, exported as
124//! they are declared.
125//!
126//! # Under test
127//!
128//! Under `encore test`, as wherever the runtime exports no metrics, metrics
129//! record nothing.
130
131use std::marker::PhantomData;
132use std::sync::{Arc, OnceLock};
133
134use crate::opentelemetry::metrics::{self as otel, Meter, MeterProvider as _};
135use crate::opentelemetry::{InstrumentationScope, KeyValue, Value};
136use encore_sdk_core::telemetry::MeterProvider as CoreMeterProvider;
137
138use sealed::Declaration;
139
140/// Derives [`Labels`] for a struct with named fields: each field is a label,
141/// named as the field is, whose type is a [`LabelValue`].
142pub use encore_macros::Labels;
143
144/// Encore's meter provider, an OpenTelemetry
145/// [`MeterProvider`](otel::MeterProvider), which Encore metrics record
146/// through. Cheap to clone.
147#[derive(Debug, Clone)]
148pub struct MeterProvider(Option<CoreMeterProvider>);
149
150impl otel::MeterProvider for MeterProvider {
151 fn meter_with_scope(&self, scope: InstrumentationScope) -> Meter {
152 match &self.0 {
153 Some(provider) => provider.meter_with_scope(scope),
154 None => Meter::new(Arc::new(NoInstruments)),
155 }
156 }
157}
158
159/// Makes instruments recording nothing.
160struct NoInstruments;
161
162impl otel::InstrumentProvider for NoInstruments {}
163
164impl MeterProvider {
165 /// Over `provider`, the runtime's; for none, recording nothing.
166 fn of(provider: Option<&CoreMeterProvider>) -> Self {
167 MeterProvider(provider.cloned())
168 }
169
170 /// Whether measurements are aggregated.
171 #[cfg(test)]
172 fn aggregates(&self) -> bool {
173 self.0.is_some()
174 }
175}
176
177/// Encore's meter provider: the runtime's, or, when the runtime exports no
178/// metrics, one recording nothing.
179pub fn meter_provider() -> MeterProvider {
180 MeterProvider::of(provider())
181}
182
183/// The meter every metric is an instrument of: `encore`, of this SDK's
184/// version, from Encore's meter provider.
185fn meter() -> Meter {
186 meter_provider().meter_with_scope(
187 InstrumentationScope::builder("encore")
188 .with_version(env!("CARGO_PKG_VERSION"))
189 .build(),
190 )
191}
192
193/// The runtime's meter provider; none when the runtime exports no metrics.
194#[cfg(not(test))]
195fn provider() -> Option<&'static CoreMeterProvider> {
196 crate::runtime::core().resources().meter_provider()
197}
198
199// This crate's own tests run without a runtime.
200#[cfg(test)]
201fn provider() -> Option<&'static CoreMeterProvider> {
202 Some(tests::provider())
203}
204
205/// The service this process hosts, if any.
206#[cfg(not(test))]
207fn hosted_service() -> Option<&'static str> {
208 crate::runtime::core()
209 .runtime_config()
210 .current_service
211 .as_deref()
212}
213
214// This crate's own tests host `svc`.
215#[cfg(test)]
216fn hosted_service() -> Option<&'static str> {
217 Some("svc")
218}
219
220/// What a metric's value types make its instruments with, and its
221/// declaration: neither nameable outside this module.
222mod sealed {
223 use std::borrow::Cow;
224
225 use super::{hosted_service, otel, Meter};
226
227 /// What a metric is declared with.
228 #[derive(Debug, Clone, Copy)]
229 pub struct Declaration {
230 pub(super) name: &'static str,
231 /// The declaring service's alone, exported as `<service>.<name>`.
232 pub(super) own: bool,
233 pub(super) unit: Option<&'static str>,
234 pub(super) description: Option<&'static str>,
235 pub(super) buckets: Option<&'static [f64]>,
236 }
237
238 impl Declaration {
239 pub(super) const fn new(name: &'static str, own: bool) -> Self {
240 Self {
241 name,
242 own,
243 unit: None,
244 description: None,
245 buckets: None,
246 }
247 }
248
249 /// The instrument name: a service's own metric's is
250 /// `<service>.<name>`, of the service this process hosts.
251 pub(super) fn exported_name(&self) -> Cow<'static, str> {
252 match hosted_service() {
253 Some(service) if self.own => format!("{service}.{}", self.name).into(),
254 _ => self.name.into(),
255 }
256 }
257 }
258
259 pub trait Counter: Sized {
260 const ONE: Self;
261 fn counter(meter: &Meter, declaration: &Declaration) -> otel::Counter<Self>;
262 }
263
264 pub trait UpDownCounter: Sized {
265 const ONE: Self;
266 fn up_down_counter(meter: &Meter, declaration: &Declaration) -> otel::UpDownCounter<Self>;
267 }
268
269 pub trait Gauge: Sized {
270 fn gauge(meter: &Meter, declaration: &Declaration) -> otel::Gauge<Self>;
271 }
272
273 pub trait Histogram: Sized {
274 fn histogram(meter: &Meter, declaration: &Declaration) -> otel::Histogram<Self>;
275 }
276}
277
278/// An instrument builder with a declaration's unit and description.
279macro_rules! describe {
280 ($builder:expr, $declaration:expr) => {{
281 let mut builder = $builder;
282 if let Some(unit) = $declaration.unit {
283 builder = builder.with_unit(unit);
284 }
285 if let Some(description) = $declaration.description {
286 builder = builder.with_description(description);
287 }
288 builder
289 }};
290}
291
292/// What a [`Counter`] counts in: `u64`, or `f64` for amounts that are not
293/// whole.
294pub trait CounterValue: sealed::Counter + Copy + Send + Sync + 'static {}
295
296/// What an [`UpDownCounter`] counts in: `i64`, or `f64` for amounts that are
297/// not whole.
298pub trait UpDownCounterValue:
299 sealed::UpDownCounter + Copy + std::ops::Neg<Output = Self> + Send + Sync + 'static
300{
301}
302
303/// What a [`Gauge`] holds: `f64`, `i64` or `u64`.
304pub trait GaugeValue: sealed::Gauge + Copy + Send + Sync + 'static {}
305
306/// What a [`Histogram`] records: `f64`.
307pub trait HistogramValue: sealed::Histogram + Copy + Send + Sync + 'static {}
308
309macro_rules! counter_value {
310 ($ty:ty, $one:expr, $counter:ident) => {
311 impl CounterValue for $ty {}
312
313 impl sealed::Counter for $ty {
314 const ONE: Self = $one;
315
316 fn counter(meter: &Meter, declaration: &Declaration) -> otel::Counter<Self> {
317 describe!(meter.$counter(declaration.exported_name()), declaration).build()
318 }
319 }
320 };
321}
322counter_value!(u64, 1, u64_counter);
323counter_value!(f64, 1.0, f64_counter);
324
325macro_rules! up_down_counter_value {
326 ($ty:ty, $one:expr, $counter:ident) => {
327 impl UpDownCounterValue for $ty {}
328
329 impl sealed::UpDownCounter for $ty {
330 const ONE: Self = $one;
331
332 fn up_down_counter(
333 meter: &Meter,
334 declaration: &Declaration,
335 ) -> otel::UpDownCounter<Self> {
336 describe!(meter.$counter(declaration.exported_name()), declaration).build()
337 }
338 }
339 };
340}
341up_down_counter_value!(i64, 1, i64_up_down_counter);
342up_down_counter_value!(f64, 1.0, f64_up_down_counter);
343
344macro_rules! gauge_value {
345 ($ty:ty, $gauge:ident) => {
346 impl GaugeValue for $ty {}
347
348 impl sealed::Gauge for $ty {
349 fn gauge(meter: &Meter, declaration: &Declaration) -> otel::Gauge<Self> {
350 describe!(meter.$gauge(declaration.exported_name()), declaration).build()
351 }
352 }
353 };
354}
355gauge_value!(f64, f64_gauge);
356gauge_value!(i64, i64_gauge);
357gauge_value!(u64, u64_gauge);
358
359impl HistogramValue for f64 {}
360
361impl sealed::Histogram for f64 {
362 fn histogram(meter: &Meter, declaration: &Declaration) -> otel::Histogram<Self> {
363 let mut builder = describe!(
364 meter.f64_histogram(declaration.exported_name()),
365 declaration
366 );
367 if let Some(buckets) = declaration.buckets {
368 builder = builder.with_boundaries(buckets.to_vec());
369 }
370 builder.build()
371 }
372}
373
374/// The labels of a metric group: a struct whose fields are the labels.
375/// Derive it:
376///
377/// ```ignore
378/// #[derive(Labels)]
379/// struct Outcome {
380/// method: &'static str,
381/// status: u16,
382/// }
383/// ```
384pub trait Labels {
385 /// Each label as an attribute, keyed by its name.
386 fn attributes(&self) -> Vec<KeyValue>;
387}
388
389/// A type a label's value can be: strings, integers, `bool` and `char` are.
390/// Implement it for an enum of your own to use it as a label:
391///
392/// ```ignore
393/// enum Plan { Free, Paid }
394///
395/// impl LabelValue for Plan {
396/// fn label_value(&self) -> Value {
397/// match self {
398/// Plan::Free => "free",
399/// Plan::Paid => "paid",
400/// }
401/// .into()
402/// }
403/// }
404/// ```
405pub trait LabelValue {
406 /// A string, an integer or a `bool`.
407 fn label_value(&self) -> Value;
408}
409
410impl LabelValue for str {
411 fn label_value(&self) -> Value {
412 self.to_string().into()
413 }
414}
415
416impl LabelValue for String {
417 fn label_value(&self) -> Value {
418 self.clone().into()
419 }
420}
421
422impl LabelValue for std::borrow::Cow<'_, str> {
423 fn label_value(&self) -> Value {
424 self.to_string().into()
425 }
426}
427
428impl LabelValue for char {
429 fn label_value(&self) -> Value {
430 self.to_string().into()
431 }
432}
433
434impl LabelValue for bool {
435 fn label_value(&self) -> Value {
436 Value::Bool(*self)
437 }
438}
439
440macro_rules! integer_label_values {
441 ($($ty:ty),*) => {$(
442 impl LabelValue for $ty {
443 /// An `i64`: one above `i64::MAX` is `i64::MAX`.
444 fn label_value(&self) -> Value {
445 Value::I64(i64::try_from(*self).unwrap_or(i64::MAX))
446 }
447 }
448 )*};
449}
450integer_label_values!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
451
452impl<T: LabelValue + ?Sized> LabelValue for &T {
453 fn label_value(&self) -> Value {
454 (**self).label_value()
455 }
456}
457
458/// The options every metric is declared with.
459macro_rules! options {
460 ($what:literal) => {
461 #[doc = concat!("Makes the ", $what, " shared between services, under one name in the app, rather than the declaring service's own: each service that records to it is recorded as using it. See [scope](self#scope).")]
462 pub const fn shared(mut self) -> Self {
463 self.declaration.own = false;
464 self
465 }
466
467 #[doc = concat!("Makes the ", $what, " the whole app's: any code may record to it, a library outside every service too, and who does is not recorded. See [scope](self#scope).")]
468 pub const fn global(mut self) -> Self {
469 self.declaration.own = false;
470 self
471 }
472
473 #[doc = concat!("What the ", $what, " measures in, as exported: `\"s\"`, `\"By\"`, `\"{request}\"`.")]
474 pub const fn unit(mut self, unit: &'static str) -> Self {
475 self.declaration.unit = Some(unit);
476 self
477 }
478
479 #[doc = concat!("What the ", $what, " measures, for one without a doc comment.")]
480 pub const fn description(mut self, description: &'static str) -> Self {
481 self.declaration.description = Some(description);
482 self
483 }
484
485 #[doc = concat!("Tags the ", $what, " with `key` and `value`: key/value classification, such as `.tag(\"data\", \"pii\")`, for policies to read. One call per tag, each with its own key. Encore reads them from the source, so both are string literals. A key is lowercase letters, digits, `_` and `-`, starting with a letter and not with `encore`, which is kept for Encore's own tags; a value is the same characters, or empty. Each is at most 63 characters, and a ", $what, " has at most 32 tags.")]
486 ///
487 /// Not to be confused with the labels a group's records carry: a tag
488 /// classifies the metric itself, and is never recorded.
489 pub const fn tag(self, _key: &'static str, _value: &'static str) -> Self {
490 self
491 }
492 };
493}
494
495/// The options a histogram is declared with, which a group's are too.
496macro_rules! histogram_options {
497 () => {
498 /// The upper bounds of the buckets, increasing: what is recorded is
499 /// counted in the first it is no greater than, or one past the last.
500 /// Without them, the OpenTelemetry SDK's defaults.
501 pub const fn buckets(mut self, bounds: &'static [f64]) -> Self {
502 self.declaration.buckets = Some(bounds);
503 self
504 }
505 };
506}
507
508/// A metric recorded to, its group, and their options.
509macro_rules! recorded {
510 (
511 $(#[$doc:meta])* $name:ident,
512 $(#[$group_doc:meta])* $group:ident,
513 $what:literal,
514 $value:ident = $default:ty,
515 $instrument:ident :: $make:ident,
516 { $($options:tt)* }
517 { $($record:tt)* }
518 ) => {
519 $(#[$doc])*
520 pub struct $name<N: $value = $default> {
521 declaration: Declaration,
522 attributes: Vec<KeyValue>,
523 instrument: OnceLock<otel::$instrument<N>>,
524 }
525
526 impl<N: $value> $name<N> {
527 #[doc = concat!("Declares the ", $what, " `name`: a letter, then letters, digits, `_`, `.`, `-` and `/`. It must be a string literal: Encore reads it from the source.")]
528 pub const fn new(name: &'static str) -> Self {
529 Self {
530 declaration: Declaration::new(name, true),
531 attributes: Vec::new(),
532 instrument: OnceLock::new(),
533 }
534 }
535
536 options!($what);
537 $($options)*
538
539 #[doc = concat!("The ", $what, " itself, to hand to other code: a function that takes a `&", stringify!($name), "` records to whichever it is given.")]
540 ///
541 /// A metric is otherwise used where it is declared, by name, which
542 /// is how Encore sees which service records to it. Passing one on
543 /// says so explicitly, and counts as recording to it.
544 pub fn reference(&self) -> &Self {
545 self
546 }
547
548 $($record)*
549
550 fn instrument(&self) -> &otel::$instrument<N> {
551 self.instrument
552 .get_or_init(|| N::$make(&meter(), &self.declaration))
553 }
554 }
555
556 $(#[$group_doc])*
557 pub struct $group<L: Labels, N: $value = $default> {
558 declaration: Declaration,
559 instrument: OnceLock<otel::$instrument<N>>,
560 _labels: PhantomData<fn(&L)>,
561 }
562
563 impl<L: Labels, N: $value> $group<L, N> {
564 #[doc = concat!("Declares the ", $what, " group `name`: a letter, then letters, digits, `_`, `.`, `-` and `/`. It must be a string literal: Encore reads it from the source.")]
565 pub const fn new(name: &'static str) -> Self {
566 Self {
567 declaration: Declaration::new(name, true),
568 instrument: OnceLock::new(),
569 _labels: PhantomData,
570 }
571 }
572
573 options!($what);
574 $($options)*
575
576 #[doc = concat!("The ", $what, " for `labels`. Worth keeping where it is used often: making it takes the labels' attributes.")]
577 pub fn with(&self, labels: &L) -> $name<N> {
578 let instrument = self
579 .instrument
580 .get_or_init(|| N::$make(&meter(), &self.declaration));
581 $name {
582 declaration: self.declaration,
583 attributes: labels.attributes(),
584 instrument: OnceLock::from(instrument.clone()),
585 }
586 }
587
588 #[doc = concat!("The group itself, to hand to other code: a function that takes a `&", stringify!($group), "<L>` records to whichever it is given.")]
589 ///
590 /// A metric is otherwise used where it is declared, by name, which
591 /// is how Encore sees which service records to it. Passing one on
592 /// says so explicitly, and counts as recording to it.
593 pub fn reference(&self) -> &Self {
594 self
595 }
596 }
597
598 named!($name, $group, $value, $what);
599 };
600}
601
602/// `named`, which every metric type and its group have: the bound metric's
603/// scope is its declaration's.
604macro_rules! named {
605 ($name:ident, $group:ident, $value:ident, $what:literal) => {
606 impl<N: $value> $name<N> {
607 #[doc = concat!("Binds the shared or global ", $what, " `name` declared elsewhere — by another service, possibly in another language — rather than declaring one. It is the declaration's kind and scope, and takes its unit, description and buckets: it has no options of its own. It is exported as `name`, never prefixed with a service's name. The name must be a string literal: Encore reads it from the source.")]
608 pub const fn named(name: &'static str) -> Self {
609 Self {
610 declaration: Declaration::new(name, false),
611 attributes: Vec::new(),
612 instrument: OnceLock::new(),
613 }
614 }
615 }
616
617 impl<L: Labels, N: $value> $group<L, N> {
618 #[doc = concat!("Binds the shared or global ", $what, " group `name` declared elsewhere — by another service, possibly in another language — rather than declaring one. Its labels and scope are the declaration's, and it takes its unit, description and buckets: it has no options of its own. It is exported as `name`, never prefixed with a service's name. The name must be a string literal: Encore reads it from the source.")]
619 pub const fn named(name: &'static str) -> Self {
620 Self {
621 declaration: Declaration::new(name, false),
622 instrument: OnceLock::new(),
623 _labels: PhantomData,
624 }
625 }
626 }
627 };
628}
629
630recorded!(
631 /// A number that only goes up: how many times something happened, or how
632 /// much of something there has been. See the [module documentation](self).
633 ///
634 /// Declared as a module-level `static`. It is the declaring service's own
635 /// unless declared [`shared`](Self::shared) or [`global`](Self::global):
636 /// see [scope](self#scope).
637 Counter,
638 /// A [`Counter`] for each combination of the labels `L`.
639 CounterGroup,
640 "counter",
641 CounterValue = u64,
642 Counter::counter,
643 {}
644 {
645 /// Adds one.
646 pub fn increment(&self) {
647 self.add(N::ONE);
648 }
649
650 /// Adds `amount`. A counter does not go down: an amount below zero is
651 /// ignored.
652 pub fn add(&self, amount: N) {
653 self.instrument().add(amount, &self.attributes);
654 }
655 }
656);
657
658recorded!(
659 /// A number that goes up and down by what is added to it: how many
660 /// requests are in flight, how many jobs are queued. See the [module
661 /// documentation](self).
662 ///
663 /// Declared as a module-level `static`. It is the declaring service's own
664 /// unless declared [`shared`](Self::shared) or [`global`](Self::global):
665 /// see [scope](self#scope).
666 UpDownCounter,
667 /// An [`UpDownCounter`] for each combination of the labels `L`.
668 UpDownCounterGroup,
669 "up-down counter",
670 UpDownCounterValue = i64,
671 UpDownCounter::up_down_counter,
672 {}
673 {
674 /// Adds one.
675 pub fn increment(&self) {
676 self.add(N::ONE);
677 }
678
679 /// Takes one away.
680 pub fn decrement(&self) {
681 self.add(-N::ONE);
682 }
683
684 /// Adds `amount`, which may be below zero.
685 pub fn add(&self, amount: N) {
686 self.instrument().add(amount, &self.attributes);
687 }
688 }
689);
690
691recorded!(
692 /// A number that is what it was last set to: what something is right now.
693 /// See the [module documentation](self).
694 ///
695 /// Declared as a module-level `static`. It is the declaring service's own
696 /// unless declared [`shared`](Self::shared) or [`global`](Self::global):
697 /// see [scope](self#scope).
698 Gauge,
699 /// A [`Gauge`] for each combination of the labels `L`.
700 GaugeGroup,
701 "gauge",
702 GaugeValue = f64,
703 Gauge::gauge,
704 {}
705 {
706 /// Sets the gauge to `value`.
707 pub fn set(&self, value: N) {
708 self.instrument().record(value, &self.attributes);
709 }
710 }
711);
712
713recorded!(
714 /// How what is recorded is distributed: how long requests take, how large
715 /// uploads are. See the [module documentation](self).
716 ///
717 /// Declared as a module-level `static`. It is the declaring service's own
718 /// unless declared [`shared`](Self::shared) or [`global`](Self::global):
719 /// see [scope](self#scope).
720 Histogram,
721 /// A [`Histogram`] for each combination of the labels `L`.
722 HistogramGroup,
723 "histogram",
724 HistogramValue = f64,
725 Histogram::histogram,
726 { histogram_options!(); }
727 {
728 /// Records `value`.
729 pub fn record(&self, value: N) {
730 self.instrument().record(value, &self.attributes);
731 }
732 }
733);
734
735#[cfg(test)]
736mod tests;