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
//! Dispatch metrics to multiple sinks.

use core::*;
use std::sync::Arc;

/// Hold each sink's metric key.
pub type DoubleKey<M1, M2> = (M1, M2);

/// Hold the two target sinks.
/// Multiple `DoubleSink`s can be combined if more than two sinks are needed.
pub type DoubleSink<S1, S2> = (S1, S2);

impl<M1, S1, M2, S2> Sink<DoubleKey<M1, M2>> for DoubleSink<S1, S2>
where
    S1: Sink<M1>,
    S2: Sink<M2>,
    M1: 'static + Clone + Send + Sync,
    M2: 'static + Clone + Send + Sync,
{
    #[allow(unused_variables)]
    fn new_metric(&self, kind: Kind, name: &str, sampling: Rate) -> DoubleKey<M1, M2> {
        (
            self.0.new_metric(kind, name, sampling),
            self.1.new_metric(kind, &name, sampling),
        )
    }

    fn new_scope(&self, auto_flush: bool) -> ScopeFn<DoubleKey<M1, M2>> {
        let scope0 = self.0.new_scope(auto_flush);
        let scope1 = self.1.new_scope(auto_flush);
        Arc::new(move |cmd| match cmd {
            Scope::Write(metric, value) => {
                scope0(Scope::Write(&metric.0, value));
                scope1(Scope::Write(&metric.1, value));
            }
            Scope::Flush => {
                scope0(Scope::Flush);
                scope1(Scope::Flush);
            }
        })
    }
}