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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
use crate::{Event, RegisterType, 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>) {
        if let Err(e) = self.sender.try_send(Event::RegisterKey(
            RegisterType::Counter,
            key,
            unit,
            description,
        )) {
            error!("Error sending metric registration: {:?}", e);
        }
    }

    fn register_gauge(&self, key: Key, unit: Option<Unit>, description: Option<&'static str>) {
        if let Err(e) = self.sender.try_send(Event::RegisterKey(
            RegisterType::Gauge,
            key,
            unit,
            description,
        )) {
            error!("Error sending metric registration: {:?}", e);
        }
    }

    fn register_histogram(&self, key: Key, unit: Option<Unit>, description: Option<&'static str>) {
        if let Err(e) = self.sender.try_send(Event::RegisterKey(
            RegisterType::Histogram,
            key,
            unit,
            description,
        )) {
            error!("Error sending metric registration: {:?}", e);
        }
    }

    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))
                {
                    #[cfg(feature = "log_dropped_metrics")]
                    error!(
                        "Error sending metric to SQLite thread: {}, dropping metric",
                        _e
                    );
                }
            }
            Err(_e) => {
                #[cfg(feature = "log_dropped_metrics")]
                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))
                {
                    #[cfg(feature = "log_dropped_metrics")]
                    error!(
                        "Error sending metric to SQLite thread: {}, dropping metric",
                        _e
                    );
                }
            }
            Err(_e) => {
                #[cfg(feature = "log_dropped_metrics")]
                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))
                {
                    #[cfg(feature = "log_dropped_metrics")]
                    error!(
                        "Error sending metric to SQLite thread: {}, dropping metric",
                        _e
                    );
                }
            }
            Err(_e) => {
                #[cfg(feature = "log_dropped_metrics")]
                error!("Failed to get system time: {}, dropping metric", _e);
            }
        }
    }
}