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
use crate::{Event, SqliteExporter};
use metrics::{GaugeValue, Key, Recorder, Unit};
use std::time::SystemTime;

impl Recorder for SqliteExporter {
    // in future we could record these to the SQLite database for informational/metadata usage
    fn register_counter(&self, _key: Key, _unit: Option<Unit>, _description: Option<&'static str>) {
    }

    fn register_gauge(&self, _key: Key, _unit: Option<Unit>, _description: Option<&'static str>) {}

    fn register_histogram(
        &self,
        _key: Key,
        _unit: Option<Unit>,
        _description: Option<&'static str>,
    ) {
    }

    fn increment_counter(&self, key: Key, value: u64) {
        match SystemTime::UNIX_EPOCH.elapsed() {
            Ok(timestamp) => {
                if let Err(e) = self.sender.try_send(Event::IncrementCounter(timestamp, key, value)) {
                    error!("Error sending metric to SQLite thread: {}, dropping metric", e);
                }
            }
            Err(e) => {
                error!("Failed to get system time: {}, dropping metric", e);
            }
        }
    }

    fn update_gauge(&self, key: Key, value: GaugeValue) {
        match SystemTime::UNIX_EPOCH.elapsed() {
            Ok(timestamp) => {
                if let Err(e) = self.sender.try_send(Event::UpdateGauge(timestamp, key, value)) {
                    error!("Error sending metric to SQLite thread: {}, dropping metric", e);
                }
            }
            Err(e) => {
                error!("Failed to get system time: {}, dropping metric", e);
            }
        }
    }

    fn record_histogram(&self, key: Key, value: f64) {
        match SystemTime::UNIX_EPOCH.elapsed() {
            Ok(timestamp) => {
                if let Err(e) = self.sender.try_send(Event::UpdateHistogram(timestamp, key, value)) {
                    error!("Error sending metric to SQLite thread: {}, dropping metric", e);
                }
            }
            Err(e) => {
                error!("Failed to get system time: {}, dropping metric", e);
            }
        }
    }
}