reool 0.30.0

An asynchrounous connection pool for Redis based on tokio and redis-rs
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::time::Duration;

use metrix::cockpit::Cockpit;
use metrix::instruments::*;
use metrix::processor::{AggregatesProcessors, TelemetryProcessor};
use metrix::{Decrement, Increment, TelemetryTransmitter, TimeUnit, TransmitsTelemetryData};

use super::{Instrumentation, PoolId};

/// A configuration for instrumenting with `metrix`
pub struct MetrixConfig {
    /// When a `Duration` is set all metrics will not report
    /// any values once nothing changed for the given
    /// duration.
    ///
    /// Default is `None`
    pub inactivity_limit: Option<Duration>,
    /// When `inactivity_limit` was enabled and
    /// reset is enabled the histogram will reset once it becomes
    /// active again
    ///
    /// Default is `false`
    pub reset_histograms_after_inactivity: bool,
    /// If a `Duration` is set the peak and bottom values within the given
    /// duration will be reported.
    ///
    /// Default is enabled with 30 seconds
    pub track_extrema_in_gauges: Option<Duration>,
    /// Sets the `Duration` for how long a triggered alert stays `on`
    ///
    /// Default is 60 seconds
    pub alert_duration: Duration,
}

impl MetrixConfig {
    /// When a `Duration` is set all metrics will not report
    /// any values once nothing changed for the given
    /// duration.
    ///
    /// Default is `None`
    pub fn inactivity_limit(mut self, v: Duration) -> Self {
        self.inactivity_limit = Some(v);
        self
    }

    /// When `inactivity_limit` was enabled and
    /// reset is enabled the histogram will reset once it becomes
    /// active again
    ///
    /// Default is `false`
    pub fn reset_histograms_after_inactivity(mut self, v: bool) -> Self {
        self.reset_histograms_after_inactivity = v;
        self
    }

    /// If a `Duration` is set the peak and bottom values within the given
    /// duration will be reported.
    ///
    /// Default is enabled with 30 seconds
    pub fn track_extrema_in_gauges(mut self, v: Duration) -> Self {
        self.track_extrema_in_gauges = Some(v);
        self
    }

    /// Sets the `Duration` for how long a triggered alert stays `on`
    ///
    /// Default is 60 seconds
    pub fn alert_duration(mut self, v: Duration) -> Self {
        self.alert_duration = v;
        self
    }

    fn configure_gauge(&self, gauge: &mut Gauge) {
        if let Some(ext_dur) = self.track_extrema_in_gauges {
            gauge.set_tracking(ext_dur.as_secs() as usize);
        }
    }

    fn configure_histogram(&self, histogram: &mut Histogram, display_unit: TimeUnit) {
        if let Some(inactivity_limit) = self.inactivity_limit {
            histogram.set_inactivity_limit(inactivity_limit);
            histogram.set_reset_after_inactivity(self.reset_histograms_after_inactivity);
        }
        histogram.set_display_time_unit(display_unit);
    }

    fn add_alert<L>(&self, panel: &mut Panel<L>)
    where
        L: Eq + Send + 'static,
    {
        let mut alert = StaircaseTimer::new_with_defaults("alert");
        alert.set_switch_off_after(self.alert_duration);
        panel.add_instrument(alert);
    }
}

impl Default for MetrixConfig {
    fn default() -> Self {
        Self {
            inactivity_limit: None,
            reset_histograms_after_inactivity: false,
            track_extrema_in_gauges: Some(Duration::from_secs(30)),
            alert_duration: Duration::from_secs(60),
        }
    }
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum Metric {
    CheckOutConnection,
    Fulfillment,
    CheckedInReturnedConnection,
    CheckedInNewConnection,
    ConnectionDropped,
    ConnectionCreated,
    ConnectionCreatedTotalTime,
    ReservationAdded,
    ReservationsChanged,
    ReservationFulfilled,
    ReservationNotFulfilled,
    ReservationLimitReached,
    ConnectionFactoryFailed,
    LifeTime,
    ConnectionsChanged,
    InFlightConnectionsChanged,
    IdleConnectionsChanged,
    PoolCountChanged,

    InternalMessageReceived,
    CheckoutMessageReceived,
    ProcessedRelevantMessage,
}

#[derive(Clone)]
pub struct MetrixInstrumentation {
    transmitter: TelemetryTransmitter<Metric>,
}

impl MetrixInstrumentation {
    pub fn new<A: AggregatesProcessors>(
        aggregates_processors: &mut A,
        config: MetrixConfig,
    ) -> Self {
        create(aggregates_processors, config)
    }
}

impl Instrumentation for MetrixInstrumentation {
    fn pool_added(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::PoolCountChanged, Increment);
    }

    fn pool_removed(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::PoolCountChanged, Decrement);
    }

    fn checked_out_connection(
        &self,
        idle_for: Duration,
        time_since_checkout_request: Duration,
        _pool: PoolId,
    ) {
        self.transmitter
            .observed_one_duration_now(Metric::CheckOutConnection, idle_for)
            .observed_one_duration_now(Metric::Fulfillment, time_since_checkout_request);
    }

    fn checked_in_returned_connection(&self, flight_time: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(Metric::CheckedInReturnedConnection, flight_time);
    }

    fn checked_in_new_connection(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_now(Metric::CheckedInNewConnection)
            .observed_one_value_now(Metric::ConnectionsChanged, Increment);
    }

    fn connection_dropped(&self, flight_time: Option<Duration>, lifetime: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(
                Metric::ConnectionDropped,
                flight_time.unwrap_or_else(|| Duration::from_secs(0)),
            )
            .observed_one_duration_now(Metric::LifeTime, lifetime)
            .observed_one_value_now(Metric::ConnectionsChanged, Decrement);
    }

    fn connection_created(&self, connected_after: Duration, total_time: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(Metric::ConnectionCreated, connected_after)
            .observed_one_duration_now(Metric::ConnectionCreatedTotalTime, total_time);
    }

    fn idle_inc(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::IdleConnectionsChanged, Increment);
    }

    fn idle_dec(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::IdleConnectionsChanged, Decrement);
    }

    fn in_flight_inc(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::InFlightConnectionsChanged, Increment);
    }

    fn in_flight_dec(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_value_now(Metric::InFlightConnectionsChanged, Decrement);
    }

    fn reservation_added(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_now(Metric::ReservationAdded)
            .observed_one_value_now(Metric::ReservationsChanged, Increment);
    }

    fn reservation_fulfilled(
        &self,
        reservation_time: Duration,
        checkout_request_time: Duration,
        _pool: PoolId,
    ) {
        self.transmitter
            .observed_one_duration_now(Metric::ReservationFulfilled, reservation_time)
            .observed_one_duration_now(Metric::Fulfillment, checkout_request_time)
            .observed_one_value_now(Metric::ReservationsChanged, Decrement);
    }

    fn reservation_not_fulfilled(
        &self,
        reservation_time: Duration,
        _checkout_request_time: Duration,
        _pool: PoolId,
    ) {
        self.transmitter
            .observed_one_duration_now(Metric::ReservationNotFulfilled, reservation_time)
            .observed_one_value_now(Metric::ReservationsChanged, Decrement);
    }

    fn reservation_limit_reached(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_now(Metric::ReservationLimitReached);
    }

    fn connection_factory_failed(&self, _pool: PoolId) {
        self.transmitter
            .observed_one_now(Metric::ConnectionFactoryFailed);
    }

    fn internal_message_received(&self, latency: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(Metric::InternalMessageReceived, latency);
    }

    fn checkout_message_received(&self, latency: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(Metric::CheckoutMessageReceived, latency);
    }

    fn relevant_message_processed(&self, processing_time: Duration, _pool: PoolId) {
        self.transmitter
            .observed_one_duration_now(Metric::ProcessedRelevantMessage, processing_time);
    }
}

fn create<A: AggregatesProcessors>(
    aggregates_processors: &mut A,
    config: MetrixConfig,
) -> MetrixInstrumentation {
    let mut cockpit = Cockpit::without_name();

    let mut panel = Panel::named(Metric::CheckOutConnection, "checked_out_connections");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("idle_time_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::Fulfillment, "fulfillment");
    let mut histogram = Histogram::new_with_defaults("after_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(
        Metric::CheckedInReturnedConnection,
        "checked_in_returned_connections",
    );
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("flight_time_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::CheckedInNewConnection, "checked_in_new_connections");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ConnectionDropped, "connections_dropped");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("flight_time_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    config.add_alert(&mut panel);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ConnectionCreated, "connections_created");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("connect_time_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(
        Metric::ConnectionCreatedTotalTime,
        "connections_created_total",
    );
    let mut histogram = Histogram::new("time_ms");
    config.configure_histogram(&mut histogram, TimeUnit::Milliseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ReservationAdded, "reservations_added");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ReservationFulfilled, "reservations_fulfilled");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("fulfilled_after_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(
        Metric::ReservationNotFulfilled,
        "reservations_not_fulfilled",
    );
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("not_fulfilled_after_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ReservationLimitReached, "reservation_limit_reached");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    config.add_alert(&mut panel);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ConnectionFactoryFailed, "connection_factory_failed");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    config.add_alert(&mut panel);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::LifeTime, "life_times");
    panel.add_meter(Meter::new_with_defaults("lifes_ended_per_second"));
    panel.add_histogram(
        Histogram::new_with_defaults("life_time_ms").display_time_unit(TimeUnit::Milliseconds),
    );
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ConnectionsChanged, "connections");
    let mut gauge = Gauge::new_with_defaults("count");
    gauge.set(0.into());
    config.configure_gauge(&mut gauge);
    panel.add_gauge(gauge);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::IdleConnectionsChanged, "idle");
    let mut gauge = Gauge::new_with_defaults("count");
    gauge.set(0.into());
    config.configure_gauge(&mut gauge);
    panel.add_gauge(gauge);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::InFlightConnectionsChanged, "in_flight");
    let mut gauge = Gauge::new_with_defaults("count");
    gauge.set(0.into());
    config.configure_gauge(&mut gauge);
    panel.add_gauge(gauge);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ReservationsChanged, "reservations");
    let mut gauge = Gauge::new_with_defaults("count");
    gauge.set(0.into());
    config.configure_gauge(&mut gauge);
    panel.add_gauge(gauge);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::PoolCountChanged, "pools");
    let mut gauge = Gauge::new_with_defaults("count");
    config.configure_gauge(&mut gauge);
    gauge.set(0.into());
    panel.add_gauge(gauge);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::InternalMessageReceived, "internal_messages");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("latency_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::CheckoutMessageReceived, "checkout_messages");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("latency_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let mut panel = Panel::named(Metric::ProcessedRelevantMessage, "processed_messages");
    panel.add_meter(Meter::new_with_defaults("per_second"));
    let mut histogram = Histogram::new_with_defaults("latency_us");
    config.configure_histogram(&mut histogram, TimeUnit::Microseconds);
    panel.add_histogram(histogram);
    cockpit.add_panel(panel);

    let (tx, mut rx) = TelemetryProcessor::new_pair_without_name();
    rx.add_cockpit(cockpit);

    if let Some(inactivity_limit) = config.inactivity_limit {
        rx.set_inactivity_limit(inactivity_limit)
    }

    aggregates_processors.add_processor(rx);

    MetrixInstrumentation { transmitter: tx }
}