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
// Copyright 2024 New Vector Ltd.
// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use opentelemetry::{KeyValue, Value};

use crate::{utils::KV, FnWrapper};

/// Make metrics attributes from a type.
pub trait MetricsAttributes<T> {
    type Iter<'a>: Iterator<Item = KeyValue>
    where
        Self: 'a,
        T: 'a;

    fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'a>;
}

pub fn metrics_attributes_fn<T, F>(f: F) -> FnWrapper<F>
where
    F: Fn(&T) -> Vec<KeyValue> + 'static,
    T: 'static,
{
    FnWrapper(f)
}

impl<T, F> MetricsAttributes<T> for FnWrapper<F>
where
    F: Fn(&T) -> Vec<KeyValue> + 'static,
    T: 'static,
{
    type Iter<'a> = std::vec::IntoIter<KeyValue>;

    fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'a> {
        let values: Vec<KeyValue> = self.0(t);
        values.into_iter()
    }
}

impl<T> MetricsAttributes<T> for ()
where
    T: 'static,
{
    type Iter<'a> = std::iter::Empty<KeyValue>;

    fn attributes(&self, _t: &T) -> Self::Iter<'_> {
        std::iter::empty()
    }
}

impl<V, T> MetricsAttributes<T> for Vec<V>
where
    V: MetricsAttributes<T> + 'static,
    T: 'static,
{
    type Iter<'a> = Box<dyn Iterator<Item = KeyValue> + 'a>;
    fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'_> {
        Box::new(self.iter().flat_map(|v| v.attributes(t)))
    }
}

impl<V, T, const N: usize> MetricsAttributes<T> for [V; N]
where
    V: MetricsAttributes<T> + 'static,
    T: 'static,
{
    type Iter<'a> = Box<dyn Iterator<Item = KeyValue> + 'a>;
    fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'_> {
        Box::new(self.iter().flat_map(|v| v.attributes(t)))
    }
}

impl<V, T> MetricsAttributes<T> for KV<V>
where
    V: Into<Value> + Clone + 'static,
    T: 'static,
{
    type Iter<'a> = std::iter::Once<KeyValue>;
    fn attributes(&self, _t: &T) -> Self::Iter<'_> {
        std::iter::once(KeyValue::new(self.0, self.1.clone().into()))
    }
}

impl<T> MetricsAttributes<T> for KeyValue
where
    T: 'static,
{
    type Iter<'a> = std::iter::Once<KeyValue>;
    fn attributes(&self, _t: &T) -> Self::Iter<'_> {
        std::iter::once(self.clone())
    }
}

impl<V, T> MetricsAttributes<T> for Option<V>
where
    V: MetricsAttributes<T> + 'static,
    T: 'static,
{
    type Iter<'a> = std::iter::Flatten<std::option::IntoIter<V::Iter<'a>>>;

    fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'_> {
        self.as_ref().map(|v| v.attributes(t)).into_iter().flatten()
    }
}

macro_rules! chain_for {
    // Sub-macro for reversing the list of types.
    (@reverse ($( $reversed:ident ,)*)) => {
        chain_for!(@build_chain $($reversed),*)
    };
    (@reverse ($($reversed:ident,)*) $head:ident $(, $tail:ident)*) => {
        chain_for!(@reverse ($head, $($reversed,)*) $($tail),*)
    };

    // Sub-macro for building the chain of iterators.
    (@build_chain $last:ident) => {
        $last::Iter<'a>
    };
    (@build_chain $head:ident, $($tail:ident),*) => {
        std::iter::Chain<chain_for!(@build_chain $($tail),*), $head::Iter<'a>>
    };

    ($($idents:ident),+) => {
        chain_for!(@reverse () $($idents),+)
    };
}

macro_rules! impl_for_tuple {
    ($first:ident $(,$rest:ident)*) => {
        impl<T, $first, $($rest,)*> MetricsAttributes<T> for ($first, $($rest,)*)
        where
            T: 'static,
            $first: MetricsAttributes<T> + 'static,
            $($rest: MetricsAttributes<T> + 'static,)*
        {
            type Iter<'a> = chain_for!($first $(, $rest)*);
            fn attributes<'a>(&'a self, t: &'a T) -> Self::Iter<'a> {
                #[allow(non_snake_case)]
                let (head, $($rest,)*) = self;
                head.attributes(t)
                    $(.chain($rest.attributes(t)))*
            }
        }
    };
}

impl_for_tuple!(V1);
impl_for_tuple!(V1, V2);
impl_for_tuple!(V1, V2, V3);
impl_for_tuple!(V1, V2, V3, V4);
impl_for_tuple!(V1, V2, V3, V4, V5);
impl_for_tuple!(V1, V2, V3, V4, V5, V6);
impl_for_tuple!(V1, V2, V3, V4, V5, V6, V7);
impl_for_tuple!(V1, V2, V3, V4, V5, V6, V7, V8);
impl_for_tuple!(V1, V2, V3, V4, V5, V6, V7, V8, V9);