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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Types for implementing custom instrumentation
use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant};

pub trait Instruments {
    // === GLOBAL ===
    fn global_batches_in_flight_inc(&self);
    fn global_batches_in_flight_dec(&self);
    fn global_batches_in_flight_dec_by(&self, by: usize);

    // === STREAM ===

    fn stream_chunk_received(&self, n_bytes: usize);
    fn stream_frame_received(&self, n_bytes: usize);
    fn stream_tick_emitted(&self);

    // === CONTROLLER ===
    fn controller_batch_received(&self, frame_received_at: Instant, events_bytes: usize);
    fn controller_info_received(&self, frame_received_at: Instant);
    fn controller_keep_alive_received(&self, frame_received_at: Instant);

    // === DISPATCHER ===

    // === WORKERS ===

    fn worker_batch_processed(&self, n_bytes: usize, n_events: Option<usize>, time: Duration);
    fn worker_deserialization_time(&self, n_bytes: usize, time: Duration);

    // === HANDLERS ===

    // === COMMITTER ===

    fn committer_cursors_committed(&self, n_cursors: usize, time: Duration);
    fn committer_cursors_not_committed(&self, n_cursors: usize, time: Duration);
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum MetricsDetailLevel {
    Low,
    Medium,
    High,
}

impl Default for MetricsDetailLevel {
    fn default() -> Self {
        MetricsDetailLevel::Medium
    }
}

#[derive(Clone)]
pub struct Instrumentation {
    instr: InstrumentationSelection,
    detail: MetricsDetailLevel,
}

impl Instrumentation {
    pub fn off() -> Self {
        {
            Instrumentation {
                instr: InstrumentationSelection::Off,
                detail: MetricsDetailLevel::default(),
            }
        }
    }

    pub fn new<I>(instruments: I, detail: MetricsDetailLevel) -> Self
    where
        I: Instruments + Send + Sync + 'static,
    {
        Instrumentation {
            instr: InstrumentationSelection::Custom(Arc::new(instruments)),
            detail,
        }
    }
}

impl Default for Instrumentation {
    fn default() -> Self {
        Instrumentation::off()
    }
}

impl fmt::Debug for Instrumentation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Instrumentation")?;
        Ok(())
    }
}

impl Instruments for Instrumentation {
    // === GLOBAL ===

    fn global_batches_in_flight_inc(&self) {}
    fn global_batches_in_flight_dec(&self) {}
    fn global_batches_in_flight_dec_by(&self, by: usize) {}

    // === STREAM ===

    fn stream_chunk_received(&self, n_bytes: usize) {
        if self.detail == MetricsDetailLevel::High {}
    }
    fn stream_frame_received(&self, n_bytes: usize) {
        if self.detail >= MetricsDetailLevel::Medium {}
    }
    fn stream_tick_emitted(&self) {
        if self.detail >= MetricsDetailLevel::Medium {}
    }

    // === CONTROLLER ===
    fn controller_batch_received(&self, frame_received_at: Instant, events_bytes: usize) {}
    fn controller_info_received(&self, frame_received_at: Instant) {}
    fn controller_keep_alive_received(&self, frame_received_at: Instant) {}

    // === DISPATCHER ===

    // === WORKERS ===

    fn worker_batch_processed(&self, n_bytes: usize, n_events: Option<usize>, time: Duration) {}
    fn worker_deserialization_time(&self, n_bytes: usize, time: Duration) {
        if self.detail >= MetricsDetailLevel::Medium {}
    }

    // === HANDLERS ===

    // === COMMITTER ===

    fn committer_cursors_committed(&self, n_cursors: usize, time: Duration) {}
    fn committer_cursors_not_committed(&self, n_cursors: usize, time: Duration) {}
}

#[derive(Clone)]
enum InstrumentationSelection {
    Off,
    Custom(Arc<dyn Instruments + Send + Sync>),
}