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
//! Commonly used span and event grouping mechanisms.

use super::EventGroup;
use super::SpanGroup;
use std::borrow::Cow;
use std::fmt::Write;
use tracing_core::*;

impl<F, R> SpanGroup for F
where
    F: Fn(&span::Attributes) -> R,
{
    type Id = R;
    fn group(&self, a: &span::Attributes) -> Self::Id {
        (self)(a)
    }
}

impl<F, R> EventGroup for F
where
    F: Fn(&Event) -> R,
{
    type Id = R;
    fn group(&self, e: &Event) -> Self::Id {
        (self)(e)
    }
}

/// Group spans/events by their configured `target`.
///
/// This is usually the module path where the span or event was created.
/// See `Metadata::target` in `tracing` for details.
#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
pub struct ByTarget;

impl SpanGroup for ByTarget {
    type Id = &'static str;
    fn group(&self, a: &span::Attributes) -> Self::Id {
        a.metadata().target()
    }
}

impl EventGroup for ByTarget {
    type Id = &'static str;
    fn group(&self, e: &Event) -> Self::Id {
        e.metadata().target()
    }
}

/// Group spans/events by their "name".
///
/// For spans, this is the string passed as the first argument to the various span tracing macros.
/// For events, this is usually the source file and line number where the event was created.
#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
pub struct ByName;

impl SpanGroup for ByName {
    type Id = &'static str;
    fn group(&self, a: &span::Attributes) -> Self::Id {
        a.metadata().name()
    }
}

impl EventGroup for ByName {
    type Id = &'static str;
    fn group(&self, e: &Event) -> Self::Id {
        e.metadata().name()
    }
}

/// Group events by their "message".
///
/// This is the string passed as the first argument to the various event tracing macros.
#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
pub struct ByMessage;

impl EventGroup for ByMessage {
    type Id = String;
    fn group(&self, e: &Event) -> Self::Id {
        EventGroup::group(&ByField(Cow::Borrowed("message")), e)
    }
}

/// Group spans/events by the value of a particular field.
///
/// If a field by the contained name is found, its recorded value (usually its `Debug` value) is
/// used as the grouping identifier. If the field is not found, an empty `String` is used.
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct ByField(pub Cow<'static, str>);

impl<T> From<T> for ByField
where
    T: Into<Cow<'static, str>>,
{
    fn from(t: T) -> Self {
        Self(t.into())
    }
}

struct ByFieldVisitor<'a> {
    field: &'a ByField,
    value: String,
}

impl<'a> field::Visit for ByFieldVisitor<'a> {
    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        if field.name() == &*self.field.0 {
            write!(&mut self.value, "{:?}", value).unwrap();
        }
    }

    fn record_i64(&mut self, field: &Field, value: i64) {
        if field.name() == &*self.field.0 {
            write!(&mut self.value, "{}", value).unwrap();
        }
    }

    fn record_u64(&mut self, field: &Field, value: u64) {
        if field.name() == &*self.field.0 {
            write!(&mut self.value, "{}", value).unwrap();
        }
    }

    fn record_str(&mut self, field: &Field, value: &str) {
        if field.name() == &*self.field.0 {
            write!(&mut self.value, "{}", value).unwrap();
        }
    }
}

impl SpanGroup for ByField {
    type Id = String;
    fn group(&self, a: &span::Attributes) -> Self::Id {
        let mut visitor = ByFieldVisitor {
            field: self,
            value: String::new(),
        };
        a.record(&mut visitor);
        visitor.value
    }
}

impl EventGroup for ByField {
    type Id = String;
    fn group(&self, e: &Event) -> Self::Id {
        let mut visitor = ByFieldVisitor {
            field: self,
            value: String::new(),
        };
        e.record(&mut visitor);
        visitor.value
    }
}