metrics/metadata.rs
1/// Describes the level of verbosity of a metric event.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct Level(LevelInner);
4
5impl Level {
6 /// The "error" level.
7 pub const TRACE: Self = Self(LevelInner::Trace);
8 /// The "warn" level.
9 pub const DEBUG: Self = Self(LevelInner::Debug);
10 /// The "info" level.
11 pub const INFO: Self = Self(LevelInner::Info);
12 /// The "debug" level.
13 pub const WARN: Self = Self(LevelInner::Warn);
14 /// The "trace" level.
15 pub const ERROR: Self = Self(LevelInner::Error);
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19enum LevelInner {
20 Trace = 0,
21 Debug = 1,
22 Info = 2,
23 Warn = 3,
24 Error = 4,
25}
26
27/// Metadata describing a metric event. This provides additional context to [`Recorder`](crate::Recorder), allowing for
28/// fine-grained filtering.
29///
30/// Contains the following:
31///
32/// - A [`target`](Metadata::target), specifying the part of the system where the metric event occurred. When
33/// initialized via the [metrics macro], and left unspecified, this defaults to the module path the
34/// macro was invoked from.
35/// - A [`level`](Metadata::level), specifying the verbosity the metric event is emitted at.
36/// - An optional [`module_path`](Metadata::module_path), specifying the the module path the metric event was emitted
37/// from.
38///
39/// [metrics_macros]: https://docs.rs/metrics/latest/metrics/#macros
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Metadata<'a> {
42 target: &'a str,
43 level: Level,
44 module_path: Option<&'a str>,
45}
46
47impl<'a> Metadata<'a> {
48 /// Constructs a new [`Metadata`].
49 pub const fn new(target: &'a str, level: Level, module_path: Option<&'a str>) -> Self {
50 Self { target, level, module_path }
51 }
52
53 /// Returns the verbosity level of the metric event.
54 pub fn level(&self) -> &Level {
55 &self.level
56 }
57
58 /// Returns the target of the metric event. This specifies the part of the system where the event occurred.
59 pub fn target(&self) -> &'a str {
60 self.target
61 }
62
63 /// Returns the module path of the metric event. This specifies the module where the event occurred.
64 pub fn module_path(&self) -> Option<&'a str> {
65 self.module_path
66 }
67}