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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Types for implementing custom instrumentation
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::nakadi_types::Error;

use crate::components::{
    committer::{CommitError, CommitTrigger},
    connector::ConnectError,
    streams::EventStreamError,
};

#[cfg(feature = "metrix")]
mod metrix_impl;

#[cfg(feature = "metrix")]
pub use self::metrix_impl::{Metrix, MetrixConfig, MetrixGaugeTrackingSecs};
#[cfg(feature = "metrix")]
pub use metrix::{
    driver::{DriverBuilder, TelemetryDriver},
    processor::ProcessorMount,
    AggregatesProcessors,
};

/// An interface on which `Nakadion` exposes metrics
///
/// An implementor of this interface can be used with
/// `Instrumentation::new`
pub trait Instruments {
    fn consumer_started(&self);
    fn consumer_stopped(&self, ran_for: Duration);
    fn streaming_ended(&self, streamed_for: Duration);

    /// Triggered when a single connect attempt for a stream was successful
    ///
    /// `time` is the time for the request
    fn stream_connect_attempt_success(&self, time: Duration);
    /// Triggered when a single connect attempt for a stream failed
    ///
    /// `time` is the time for the request
    fn stream_connect_attempt_failed(&self, time: Duration);
    /// Triggered when a stream was finally connect after maybe multiple attempts
    ///
    /// `time` is the time for the whole cycle until a connection was made
    fn stream_connected(&self, time: Duration);
    /// Triggered when connecting to a stream finally failed after maybe multiple attempts
    ///
    /// `time` is the time for the whole cycle until a connection attempts finally failed
    fn stream_not_connected(&self, time: Duration, err: &ConnectError);

    /// A chunk of data with `n_bytes` was received over the network
    fn stream_chunk_received(&self, n_bytes: usize);
    /// Chunks have been assembled to a complete frame containing all required data
    fn stream_frame_completed(&self, n_bytes: usize, time: Duration);
    /// An internal tick signal has been emitted
    fn stream_tick_emitted(&self);

    /// The controller received a frame which contained info data
    ///
    /// The time it took from receiving the first chunk until it reached the controller are passed
    /// along with the complete bytes of the frame
    fn info_frame_received(&self, frame_started_at: Instant, frame_completed_at: Instant);
    /// The controller received a frame which contained no events
    ///
    /// The time it took from receiving the first chunk until it reached the controller are passed
    /// along with the complete bytes of the frame
    fn keep_alive_frame_received(&self, frame_started_at: Instant, frame_completed_at: Instant);
    /// A partition which was formerly not known to be active was sent
    /// data on

    /// The controller received a frame which contained events
    ///
    /// The time it took from receiving the first chunk until it reached the controller are passed
    /// along with the complete bytes of the frame
    fn batch_frame_received(
        &self,
        frame_started_at: Instant,
        frame_completed_at: Instant,
        events_bytes: usize,
    );

    /// The time elapsed between the reception of 2 batches with events
    fn batch_frame_gap(&self, gap: Duration);

    /// No frames have been received for the given time and the warning has already  threshold elapsed
    fn no_frames_warning(&self, no_frames_for: Duration);
    /// No events have been received for the given time and the warning threshold has already elapsed
    fn no_events_warning(&self, no_events_for: Duration);

    fn stream_dead(&self, after: Duration);

    /// The stream was aborted due to a streaming related error
    fn stream_error(&self, err: &EventStreamError);

    /// Tracks the number of unconsumed events.
    ///
    /// Only available if the `Consumer` was created with a clients that
    /// supports the `SubscriptionApi`
    fn stream_unconsumed_events(&self, n_unconsumed: usize);

    /// Triggered when a new batch with events was received
    fn batches_in_flight_inc(&self);
    /// Triggered when a batch with events was processed
    fn batches_in_flight_dec(&self);
    /// Usually triggered when there are still batches in flight and the stream aborts.
    ///
    /// This is a correction for the inflight metrics.
    fn batches_in_flight_dec_by(&self, by: usize);

    fn event_type_partition_activated(&self);

    /// A partition did not receive data for some time is is therefore considered inactive.
    fn event_type_partition_deactivated(&self, active_for: Duration);

    fn batch_processing_started(&self, frame_started_at: Instant, frame_completed_at: Instant);

    /// Events were processed.
    fn batch_processed(&self, n_bytes: usize, time: Duration);
    /// Events were processed.
    fn batch_processed_n_events(&self, n_events: usize);
    /// Events have been deserialized.
    ///
    /// The amount of bytes deserialized and the time it took are passed.
    fn batch_deserialized(&self, n_bytes: usize, time: Duration);

    /// Cursors to be committed have reached the commit stage.
    ///
    /// The time it took from receiving the first chunk until it reached the commit stage are passed
    fn cursor_to_commit_received(&self, frame_started_at: Instant, frame_completed_at: Instant);
    /// Cursors commit was triggered.
    fn cursors_commit_triggered(&self, trigger: CommitTrigger);
    /// Ages of the cursors when making a commit attempt to Nakadi
    ///
    /// This ages are measured before making an attempt to make a call to Nakadi.
    /// `first_cursor_age` is the critical age of the first cursor which might be commited
    /// with a later cursor which has the age `last_cursor_age`.
    ///
    /// `` should be true, when the first cursor reached a critical age which
    /// might endanger or even cause the commit to fail.
    fn cursor_ages_on_commit_attempt(
        &self,
        first_cursor_age: Duration,
        last_cursor_age: Duration,
        first_cursor_age_warning: bool,
    );
    /// Cursors were successfully committed.
    ///
    /// The time request took is passed
    fn cursors_committed(&self, n_cursors: usize, time: Duration);
    /// Cursors were not successfully committed.
    ///
    /// The time request took is passed
    fn cursors_not_committed(&self, n_cursors: usize, time: Duration, err: &CommitError);
    /// An attempt to commit cursors failed. There might still be a retry
    ///
    /// The time request took is passed
    fn commit_cursors_attempt_failed(&self, n_cursors: usize, time: Duration);
}

impl<T> crate::tools::subscription_stats::Instruments for T
where
    T: Instruments,
{
    fn unconsumed_events(&self, n_unconsumed: usize) {
        self.stream_unconsumed_events(n_unconsumed);
    }
}

/// This defines the level of metrics being collected
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum MetricsDetailLevel {
    Low,
    Medium,
    High,
}

impl MetricsDetailLevel {
    env_funs!("METRICS_DETAIL_LEVEL");
}

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

impl FromStr for MetricsDetailLevel {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_ref() {
            "low" => Ok(MetricsDetailLevel::Low),
            "medium" => Ok(MetricsDetailLevel::Medium),
            "high" => Ok(MetricsDetailLevel::High),
            s => Err(Error::new(format!(
                "{} is not a valid MetricsDetailLevel",
                s
            ))),
        }
    }
}

/// Used by the consumer to notify on measurable state changes
#[derive(Clone)]
pub struct Instrumentation {
    instr: InstrumentationSelection,
    detail: MetricsDetailLevel,
}

impl Instrumentation {
    /// Do not collect any data.
    ///
    /// This is the default.
    pub fn off() -> Self {
        {
            Instrumentation {
                instr: InstrumentationSelection::Off,
                detail: MetricsDetailLevel::default(),
            }
        }
    }

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

    #[cfg(feature = "metrix")]
    pub fn metrix(metrix: Metrix, detail: MetricsDetailLevel) -> Self {
        Instrumentation {
            instr: InstrumentationSelection::Metrix(metrix),
            detail,
        }
    }

    #[cfg(feature = "metrix")]
    pub fn metrix_mounted<A: AggregatesProcessors>(
        config: &MetrixConfig,
        detail: MetricsDetailLevel,
        processor: &mut A,
    ) -> Self {
        let metrix = Metrix::new(config, processor);
        Self::metrix(metrix, detail)
    }

    #[cfg(feature = "metrix")]
    pub fn metrix_mountable(
        config: &MetrixConfig,
        detail: MetricsDetailLevel,
        mountable_name: Option<&str>,
    ) -> (Self, ProcessorMount) {
        let (metrix, mount) = Metrix::new_mountable(config, mountable_name);
        (Self::metrix(metrix, detail), mount)
    }
}

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 {
    fn consumer_started(&self) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => instr.consumer_started(),
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => instr.consumer_started(),
            }
        }
    }

    fn consumer_stopped(&self, ran_for: Duration) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => instr.consumer_stopped(ran_for),
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => instr.consumer_stopped(ran_for),
            }
        }
    }

    fn streaming_ended(&self, streamed_for: Duration) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => instr.streaming_ended(streamed_for),
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => instr.streaming_ended(streamed_for),
            }
        }
    }

    fn stream_connect_attempt_success(&self, time: Duration) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => {
                    instr.stream_connect_attempt_success(time)
                }
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => {
                    instr.stream_connect_attempt_success(time)
                }
            }
        }
    }

    fn stream_connect_attempt_failed(&self, time: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.stream_connect_attempt_failed(time)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.stream_connect_attempt_failed(time)
            }
        }
    }
    fn stream_connected(&self, time: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.stream_connected(time),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.stream_connected(time),
        }
    }

    fn stream_not_connected(&self, time: Duration, err: &ConnectError) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.stream_not_connected(time, err),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.stream_not_connected(time, err),
        }
    }

    fn stream_chunk_received(&self, n_bytes: usize) {
        if self.detail == MetricsDetailLevel::High {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => instr.stream_chunk_received(n_bytes),
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => instr.stream_chunk_received(n_bytes),
            }
        }
    }
    fn stream_frame_completed(&self, n_bytes: usize, time: Duration) {
        if self.detail == MetricsDetailLevel::High {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => {
                    instr.stream_frame_completed(n_bytes, time)
                }
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => {
                    instr.stream_frame_completed(n_bytes, time)
                }
            }
        }
    }
    fn stream_tick_emitted(&self) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => instr.stream_tick_emitted(),
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => instr.stream_tick_emitted(),
            }
        }
    }

    fn info_frame_received(&self, frame_started_at: Instant, frame_completed_at: Instant) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.info_frame_received(frame_started_at, frame_completed_at)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.info_frame_received(frame_started_at, frame_completed_at)
            }
        }
    }
    fn keep_alive_frame_received(&self, frame_started_at: Instant, frame_completed_at: Instant) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.keep_alive_frame_received(frame_started_at, frame_completed_at)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.keep_alive_frame_received(frame_started_at, frame_completed_at)
            }
        }
    }

    fn batch_frame_received(
        &self,
        frame_started_at: Instant,
        frame_completed_at: Instant,
        events_bytes: usize,
    ) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.batch_frame_received(frame_started_at, frame_completed_at, events_bytes)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.batch_frame_received(frame_started_at, frame_completed_at, events_bytes)
            }
        }
    }

    fn batch_frame_gap(&self, gap: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batch_frame_gap(gap),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batch_frame_gap(gap),
        }
    }

    fn no_frames_warning(&self, no_frames_for: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.no_frames_warning(no_frames_for),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.no_frames_warning(no_frames_for),
        }
    }

    fn no_events_warning(&self, no_events_for: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.no_events_warning(no_events_for),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.no_events_warning(no_events_for),
        }
    }

    fn stream_dead(&self, after: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.stream_dead(after),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.stream_dead(after),
        }
    }

    fn stream_error(&self, err: &EventStreamError) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.stream_error(err),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.stream_error(err),
        }
    }

    fn stream_unconsumed_events(&self, n_unconsumed: usize) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.stream_unconsumed_events(n_unconsumed)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.stream_unconsumed_events(n_unconsumed)
            }
        }
    }

    fn batches_in_flight_inc(&self) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batches_in_flight_inc(),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batches_in_flight_inc(),
        }
    }
    fn batches_in_flight_dec(&self) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batches_in_flight_dec(),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batches_in_flight_dec(),
        }
    }
    fn batches_in_flight_dec_by(&self, by: usize) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batches_in_flight_dec_by(by),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batches_in_flight_dec_by(by),
        }
    }

    fn event_type_partition_activated(&self) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.event_type_partition_activated(),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.event_type_partition_activated(),
        }
    }

    fn event_type_partition_deactivated(&self, active_for: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.event_type_partition_deactivated(active_for)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.event_type_partition_deactivated(active_for)
            }
        }
    }

    fn batch_processing_started(&self, frame_started_at: Instant, frame_completed_at: Instant) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.batch_processing_started(frame_started_at, frame_completed_at)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.batch_processing_started(frame_started_at, frame_completed_at)
            }
        }
    }

    fn batch_processed(&self, n_bytes: usize, time: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batch_processed(n_bytes, time),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batch_processed(n_bytes, time),
        }
    }
    fn batch_processed_n_events(&self, n_events: usize) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.batch_processed_n_events(n_events),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.batch_processed_n_events(n_events),
        }
    }
    fn batch_deserialized(&self, n_bytes: usize, time: Duration) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => {
                    instr.batch_deserialized(n_bytes, time)
                }
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => {
                    instr.batch_deserialized(n_bytes, time)
                }
            }
        }
    }

    fn cursor_to_commit_received(&self, frame_started_at: Instant, frame_completed_at: Instant) {
        if self.detail >= MetricsDetailLevel::Medium {
            match self.instr {
                InstrumentationSelection::Off => {}
                InstrumentationSelection::Custom(ref instr) => {
                    instr.cursor_to_commit_received(frame_started_at, frame_completed_at)
                }
                #[cfg(feature = "metrix")]
                InstrumentationSelection::Metrix(ref instr) => {
                    instr.cursor_to_commit_received(frame_started_at, frame_completed_at)
                }
            }
        }
    }

    fn cursors_commit_triggered(&self, trigger: CommitTrigger) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.cursors_commit_triggered(trigger),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.cursors_commit_triggered(trigger),
        }
    }

    fn cursor_ages_on_commit_attempt(
        &self,
        first_cursor_age: Duration,
        last_cursor_age: Duration,
        first_cursor_age_warning: bool,
    ) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.cursor_ages_on_commit_attempt(
                first_cursor_age,
                last_cursor_age,
                first_cursor_age_warning,
            ),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.cursor_ages_on_commit_attempt(
                first_cursor_age,
                last_cursor_age,
                first_cursor_age_warning,
            ),
        }
    }

    fn cursors_committed(&self, n_cursors: usize, time: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => instr.cursors_committed(n_cursors, time),
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => instr.cursors_committed(n_cursors, time),
        }
    }
    fn cursors_not_committed(&self, n_cursors: usize, time: Duration, err: &CommitError) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.cursors_not_committed(n_cursors, time, err)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.cursors_not_committed(n_cursors, time, err)
            }
        }
    }

    fn commit_cursors_attempt_failed(&self, n_cursors: usize, time: Duration) {
        match self.instr {
            InstrumentationSelection::Off => {}
            InstrumentationSelection::Custom(ref instr) => {
                instr.commit_cursors_attempt_failed(n_cursors, time)
            }
            #[cfg(feature = "metrix")]
            InstrumentationSelection::Metrix(ref instr) => {
                instr.commit_cursors_attempt_failed(n_cursors, time)
            }
        }
    }
}

#[derive(Clone)]
enum InstrumentationSelection {
    Off,
    Custom(Arc<dyn Instruments + Send + Sync>),
    #[cfg(feature = "metrix")]
    Metrix(Metrix),
}