metrix/instruments/
polled.rs

1//! Instruments that poll their values when a snapshot is requested.
2//!
3//! This is useful when you need to query values like currently opened DB
4//! connections etc.
5
6use crate::snapshot::*;
7use crate::util;
8use crate::{Descriptive, PutsSnapshot};
9
10/// Create an instrument that delivers metrics based on querying values
11/// when a `Snapshot` is requested.
12///
13/// The `Snapshot` can be generated from anything that
14/// implements `PutsSnapshot`.
15pub struct PollingInstrument<P> {
16    /// If `create_group_with_name` is true, this name will create a new named
17    /// group.
18    pub name: String,
19    pub title: Option<String>,
20    pub description: Option<String>,
21    pub poll: P,
22    pub create_group_with_name: bool,
23}
24
25impl<P> PollingInstrument<P>
26where
27    P: PutsSnapshot,
28{
29    pub fn new_with_defaults<T: Into<String>>(name: T, poll: P) -> PollingInstrument<P> {
30        PollingInstrument {
31            name: name.into(),
32            title: None,
33            description: None,
34            poll,
35            create_group_with_name: false,
36        }
37    }
38
39    /// Gets the name of this `PollingInstrument`
40    pub fn get_name(&self) -> &str {
41        &self.name
42    }
43
44    /// Set the name if this `PollingInstrument`.
45    ///
46    /// The name is a path segment within a `Snapshot` if
47    /// `self.create_group_with_name` is set to true.
48    pub fn set_name<T: Into<String>>(&mut self, name: T) {
49        self.name = name.into();
50    }
51
52    /// Returns whether a new group with the instruments name
53    /// shall be craeted in the snapshot.
54    pub fn create_group_with_name(&self) -> bool {
55        self.create_group_with_name
56    }
57
58    /// Set to `true` if you want to create a new group
59    /// with the name of this instrument when a `Snapshot`
60    /// is requested.
61    pub fn set_create_group_with_name(&mut self, create_group: bool) {
62        self.create_group_with_name = create_group;
63    }
64
65    /// Sets the `title` of this `PollingInstrument`.
66    ///
67    /// A title can be part of a descriptive `Snapshot`
68    pub fn set_title<T: Into<String>>(&mut self, title: T) {
69        self.title = Some(title.into())
70    }
71
72    /// Sets the `description` of this `PollingInstrument`.
73    ///
74    /// A description can be part of a descriptive `Snapshot`
75    pub fn set_description<T: Into<String>>(&mut self, description: T) {
76        self.description = Some(description.into())
77    }
78
79    fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
80        self.poll.put_snapshot(into, descriptive);
81    }
82}
83
84impl<P> Descriptive for PollingInstrument<P> {
85    fn title(&self) -> Option<&str> {
86        self.title.as_deref()
87    }
88
89    fn description(&self) -> Option<&str> {
90        self.description.as_deref()
91    }
92}
93
94impl<P> PutsSnapshot for PollingInstrument<P>
95where
96    P: PutsSnapshot,
97{
98    fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
99        util::put_postfixed_descriptives(self, &self.name, into, descriptive);
100        if self.create_group_with_name {
101            let mut new_level = Snapshot::default();
102            self.put_values_into_snapshot(&mut new_level, descriptive);
103            into.items
104                .push((self.name.clone(), ItemKind::Snapshot(new_level)));
105        } else {
106            self.put_values_into_snapshot(into, descriptive);
107        }
108    }
109}
110
111pub struct ConstantValue {
112    name: String,
113    title: Option<String>,
114    description: Option<String>,
115    value: ItemKind,
116}
117
118impl ConstantValue {
119    pub fn new<T: Into<String>, V: Into<ItemKind>>(name: T, value: V) -> Self {
120        ConstantValue {
121            name: name.into(),
122            title: None,
123            description: None,
124            value: value.into(),
125        }
126    }
127
128    /// Set the name if this `PollingInstrument`.
129    ///
130    /// The name is a path segment within a `Snapshot` if
131    /// `self.create_group_with_name` is set to true.
132    pub fn set_name<T: Into<String>>(&mut self, name: T) {
133        self.name = name.into();
134    }
135
136    /// Gets the name of this `PollingInstrument`
137    pub fn get_name(&self) -> &str {
138        &self.name
139    }
140
141    /// Sets the `title` of this `PollingInstrument`.
142    ///
143    /// A title can be part of a descriptive `Snapshot`
144    pub fn set_title<T: Into<String>>(&mut self, title: T) {
145        self.title = Some(title.into())
146    }
147
148    /// Sets the `description` of this `PollingInstrument`.
149    ///
150    /// A description can be part of a descriptive `Snapshot`
151    pub fn set_description<T: Into<String>>(&mut self, description: T) {
152        self.description = Some(description.into())
153    }
154}
155
156impl PutsSnapshot for ConstantValue {
157    fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
158        util::put_postfixed_descriptives(self, &self.name, into, descriptive);
159        into.items.push((self.name.clone(), self.value.clone()));
160    }
161}
162
163impl Descriptive for ConstantValue {
164    fn title(&self) -> Option<&str> {
165        self.title.as_deref()
166    }
167
168    fn description(&self) -> Option<&str> {
169        self.description.as_deref()
170    }
171}