relay_event_schema/protocol/
ourlog.rs

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
use relay_protocol::{
    Annotated, Empty, Error, FromValue, IntoValue, Object, SkipSerialization, Value,
};

use serde::ser::SerializeMap;

use crate::processor::ProcessValue;
use crate::protocol::{SpanId, TraceId};

#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[metastructure(process_func = "process_ourlog", value_type = "OurLog")]
pub struct OurLog {
    /// Time when the event occurred.
    #[metastructure(required = true, trim = false)]
    pub timestamp_nanos: Annotated<u64>,

    /// Time when the event was observed.
    #[metastructure(required = true, trim = false)]
    pub observed_timestamp_nanos: Annotated<u64>,

    /// The ID of the trace the log belongs to.
    #[metastructure(required = false, trim = false)]
    pub trace_id: Annotated<TraceId>,
    /// The Span id.
    ///
    #[metastructure(required = false, trim = false)]
    pub span_id: Annotated<SpanId>,

    /// Trace flag bitfield.
    #[metastructure(required = false)]
    pub trace_flags: Annotated<f64>,

    /// This is the original string representation of the severity as it is known at the source
    #[metastructure(required = false, max_chars = 32, pii = "true", trim = false)]
    pub severity_text: Annotated<String>,

    /// Numerical representation of the severity level
    #[metastructure(required = false)]
    pub severity_number: Annotated<i64>,

    /// Log body.
    #[metastructure(required = true, pii = "true", trim = false)]
    pub body: Annotated<String>,

    /// Arbitrary attributes on a log.
    #[metastructure(pii = "true", trim = false)]
    pub attributes: Annotated<Object<AttributeValue>>,

    /// Additional arbitrary fields for forwards compatibility.
    #[metastructure(additional_properties, retain = true, pii = "maybe", trim = false)]
    pub other: Object<Value>,
}

#[derive(Debug, Clone, PartialEq, ProcessValue)]
pub enum AttributeValue {
    #[metastructure(field = "string_value", pii = "true")]
    StringValue(String),
    #[metastructure(field = "int_value", pii = "true")]
    IntValue(i64),
    #[metastructure(field = "double_value", pii = "true")]
    DoubleValue(f64),
    #[metastructure(field = "bool_value", pii = "true")]
    BoolValue(bool),
    /// Any other unknown attribute value.
    ///
    /// This exists to ensure other attribute values such as array and object can be added in the future.
    Unknown(String),
}

impl IntoValue for AttributeValue {
    fn into_value(self) -> Value {
        let mut map = Object::new();
        match self {
            AttributeValue::StringValue(v) => {
                map.insert("string_value".to_string(), Annotated::new(Value::String(v)));
            }
            AttributeValue::IntValue(v) => {
                map.insert("int_value".to_string(), Annotated::new(Value::I64(v)));
            }
            AttributeValue::DoubleValue(v) => {
                map.insert("double_value".to_string(), Annotated::new(Value::F64(v)));
            }
            AttributeValue::BoolValue(v) => {
                map.insert("bool_value".to_string(), Annotated::new(Value::Bool(v)));
            }
            AttributeValue::Unknown(v) => {
                map.insert("unknown".to_string(), Annotated::new(Value::String(v)));
            }
        }
        Value::Object(map)
    }

    fn serialize_payload<S>(&self, s: S, _behavior: SkipSerialization) -> Result<S::Ok, S::Error>
    where
        Self: Sized,
        S: serde::Serializer,
    {
        let mut map = s.serialize_map(None)?;
        match self {
            AttributeValue::StringValue(v) => {
                map.serialize_entry("string_value", v)?;
            }
            AttributeValue::IntValue(v) => {
                map.serialize_entry("int_value", v)?;
            }
            AttributeValue::DoubleValue(v) => {
                map.serialize_entry("double_value", v)?;
            }
            AttributeValue::BoolValue(v) => {
                map.serialize_entry("bool_value", v)?;
            }
            AttributeValue::Unknown(v) => {
                map.serialize_entry("unknown", v)?;
            }
        }
        map.end()
    }
}

impl AttributeValue {
    pub fn string_value(&self) -> Option<&String> {
        match self {
            AttributeValue::StringValue(s) => Some(s),
            _ => None,
        }
    }
    pub fn int_value(&self) -> Option<i64> {
        match self {
            AttributeValue::IntValue(i) => Some(*i),
            _ => None,
        }
    }
    pub fn double_value(&self) -> Option<f64> {
        match self {
            AttributeValue::DoubleValue(d) => Some(*d),
            _ => None,
        }
    }
    pub fn bool_value(&self) -> Option<bool> {
        match self {
            AttributeValue::BoolValue(b) => Some(*b),
            _ => None,
        }
    }
}

impl Empty for AttributeValue {
    #[inline]
    fn is_empty(&self) -> bool {
        matches!(self, Self::Unknown(_))
    }
}

impl FromValue for AttributeValue {
    fn from_value(value: Annotated<Value>) -> Annotated<Self> {
        match value {
            Annotated(Some(Value::String(value)), meta) => {
                Annotated(Some(AttributeValue::StringValue(value)), meta)
            }
            Annotated(Some(Value::I64(value)), meta) => {
                Annotated(Some(AttributeValue::IntValue(value)), meta)
            }
            Annotated(Some(Value::F64(value)), meta) => {
                Annotated(Some(AttributeValue::DoubleValue(value)), meta)
            }
            Annotated(Some(Value::Bool(value)), meta) => {
                Annotated(Some(AttributeValue::BoolValue(value)), meta)
            }
            Annotated(Some(value), mut meta) => {
                meta.add_error(Error::expected(
                    "a valid attribute value (string, int, double, bool)",
                ));
                meta.set_original_value(Some(value));
                Annotated(None, meta)
            }
            Annotated(None, meta) => Annotated(None, meta),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ourlog_serialization() {
        let json = r#"{
  "timestamp_nanos": 1544712660300000000,
  "observed_timestamp_nanos": 1544712660300000000,
  "trace_id": "5b8efff798038103d269b633813fc60c",
  "span_id": "eee19b7ec3c1b174",
  "severity_text": "Information",
  "severity_number": 10,
  "body": "Example log record",
  "attributes": {
    "boolean.attribute": {
      "bool_value": true
    },
    "double.attribute": {
      "double_value": 637.704
    },
    "int.attribute": {
      "int_value": 10
    },
    "string.attribute": {
      "string_value": "some string"
    }
  }
}"#;

        let mut attributes = Object::new();
        attributes.insert(
            "string.attribute".into(),
            Annotated::new(AttributeValue::StringValue("some string".into())),
        );
        attributes.insert(
            "boolean.attribute".into(),
            Annotated::new(AttributeValue::BoolValue(true)),
        );
        attributes.insert(
            "int.attribute".into(),
            Annotated::new(AttributeValue::IntValue(10)),
        );
        attributes.insert(
            "double.attribute".into(),
            Annotated::new(AttributeValue::DoubleValue(637.704)),
        );

        let log = Annotated::new(OurLog {
            timestamp_nanos: Annotated::new(1544712660300000000),
            observed_timestamp_nanos: Annotated::new(1544712660300000000),
            severity_number: Annotated::new(10),
            severity_text: Annotated::new("Information".to_string()),
            trace_id: Annotated::new(TraceId("5b8efff798038103d269b633813fc60c".into())),
            span_id: Annotated::new(SpanId("eee19b7ec3c1b174".into())),
            body: Annotated::new("Example log record".to_string()),
            attributes: Annotated::new(attributes),
            ..Default::default()
        });

        assert_eq!(json, log.to_json_pretty().unwrap());
    }
}