Skip to content

Commit c36749c

Browse files
authored
ref: Move attributes out of ourlogs (#4715)
No functional changes. This is in preparation for introducing Spans V2, which will use these types as well. #skip-changelog ref: RELAY-62
1 parent 69b7388 commit c36749c

File tree

5 files changed

+205
-220
lines changed

5 files changed

+205
-220
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, SkipSerialization, Value};
2+
use std::fmt;
3+
4+
use crate::processor::ProcessValue;
5+
6+
#[derive(Clone, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
7+
pub struct Attribute {
8+
#[metastructure(flatten)]
9+
pub value: AttributeValue,
10+
11+
/// Additional arbitrary fields for forwards compatibility.
12+
#[metastructure(additional_properties)]
13+
pub other: Object<Value>,
14+
}
15+
16+
impl Attribute {
17+
pub fn new(attribute_type: AttributeType, value: Value) -> Self {
18+
Self {
19+
value: AttributeValue {
20+
ty: Annotated::new(attribute_type),
21+
value: Annotated::new(value),
22+
},
23+
other: Object::new(),
24+
}
25+
}
26+
}
27+
28+
impl fmt::Debug for Attribute {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
f.debug_struct("Attribute")
31+
.field("value", &self.value.value)
32+
.field("type", &self.value.ty)
33+
.field("other", &self.other)
34+
.finish()
35+
}
36+
}
37+
38+
#[derive(Debug, Clone, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
39+
pub struct AttributeValue {
40+
#[metastructure(field = "type", required = true, trim = false)]
41+
pub ty: Annotated<AttributeType>,
42+
#[metastructure(required = true, pii = "true")]
43+
pub value: Annotated<Value>,
44+
}
45+
46+
#[derive(Debug, Clone, PartialEq, Eq)]
47+
pub enum AttributeType {
48+
Boolean,
49+
Integer,
50+
Double,
51+
String,
52+
Unknown(String),
53+
}
54+
55+
impl ProcessValue for AttributeType {}
56+
57+
impl AttributeType {
58+
pub fn as_str(&self) -> &str {
59+
match self {
60+
Self::Boolean => "boolean",
61+
Self::Integer => "integer",
62+
Self::Double => "double",
63+
Self::String => "string",
64+
Self::Unknown(value) => value,
65+
}
66+
}
67+
68+
pub fn unknown_string() -> String {
69+
"unknown".to_string()
70+
}
71+
}
72+
73+
impl fmt::Display for AttributeType {
74+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75+
write!(f, "{}", self.as_str())
76+
}
77+
}
78+
79+
impl From<String> for AttributeType {
80+
fn from(value: String) -> Self {
81+
match value.as_str() {
82+
"boolean" => Self::Boolean,
83+
"integer" => Self::Integer,
84+
"double" => Self::Double,
85+
"string" => Self::String,
86+
_ => Self::Unknown(value),
87+
}
88+
}
89+
}
90+
91+
impl Empty for AttributeType {
92+
#[inline]
93+
fn is_empty(&self) -> bool {
94+
false
95+
}
96+
}
97+
98+
impl FromValue for AttributeType {
99+
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
100+
match String::from_value(value) {
101+
Annotated(Some(value), meta) => Annotated(Some(value.into()), meta),
102+
Annotated(None, meta) => Annotated(None, meta),
103+
}
104+
}
105+
}
106+
107+
impl IntoValue for AttributeType {
108+
fn into_value(self) -> Value
109+
where
110+
Self: Sized,
111+
{
112+
Value::String(match self {
113+
Self::Unknown(s) => s,
114+
s => s.to_string(),
115+
})
116+
}
117+
118+
fn serialize_payload<S>(&self, s: S, _behavior: SkipSerialization) -> Result<S::Ok, S::Error>
119+
where
120+
Self: Sized,
121+
S: serde::Serializer,
122+
{
123+
serde::ser::Serialize::serialize(self.as_str(), s)
124+
}
125+
}

relay-event-schema/src/protocol/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Implements the sentry event protocol.
22
3+
mod attributes;
34
mod base;
45
mod breadcrumb;
56
mod breakdowns;
@@ -38,6 +39,7 @@ mod utils;
3839
#[doc(inline)]
3940
pub use relay_base_schema::{events::*, spans::*};
4041

42+
pub use self::attributes::*;
4143
pub use self::breadcrumb::*;
4244
pub use self::breakdowns::*;
4345
pub use self::client_report::*;

relay-event-schema/src/protocol/ourlog.rs

Lines changed: 11 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::{self, Display};
44
use serde::{Serialize, Serializer};
55

66
use crate::processor::ProcessValue;
7-
use crate::protocol::{SpanId, Timestamp, TraceId};
7+
use crate::protocol::{Attribute, SpanId, Timestamp, TraceId};
88

99
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
1010
#[metastructure(process_func = "process_ourlog", value_type = "OurLog")]
@@ -31,7 +31,7 @@ pub struct OurLog {
3131

3232
/// Arbitrary attributes on a log.
3333
#[metastructure(pii = "true", trim = false)]
34-
pub attributes: Annotated<Object<OurLogAttribute>>,
34+
pub attributes: Annotated<Object<Attribute>>,
3535

3636
/// Additional arbitrary fields for forwards compatibility.
3737
#[metastructure(additional_properties, retain = true, pii = "maybe")]
@@ -44,127 +44,6 @@ impl OurLog {
4444
}
4545
}
4646

47-
#[derive(Clone, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
48-
pub struct OurLogAttribute {
49-
#[metastructure(flatten)]
50-
pub value: OurLogAttributeValue,
51-
52-
/// Additional arbitrary fields for forwards compatibility.
53-
#[metastructure(additional_properties)]
54-
pub other: Object<Value>,
55-
}
56-
57-
impl OurLogAttribute {
58-
pub fn new(attribute_type: OurLogAttributeType, value: Value) -> Self {
59-
Self {
60-
value: OurLogAttributeValue {
61-
ty: Annotated::new(attribute_type),
62-
value: Annotated::new(value),
63-
},
64-
other: Object::new(),
65-
}
66-
}
67-
}
68-
69-
impl fmt::Debug for OurLogAttribute {
70-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
f.debug_struct("OurLogAttribute")
72-
.field("value", &self.value.value)
73-
.field("type", &self.value.ty)
74-
.field("other", &self.other)
75-
.finish()
76-
}
77-
}
78-
79-
#[derive(Debug, Clone, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
80-
pub struct OurLogAttributeValue {
81-
#[metastructure(field = "type", required = true, trim = false)]
82-
pub ty: Annotated<OurLogAttributeType>,
83-
#[metastructure(required = true, pii = "true")]
84-
pub value: Annotated<Value>,
85-
}
86-
87-
#[derive(Debug, Clone, PartialEq, Eq)]
88-
pub enum OurLogAttributeType {
89-
Boolean,
90-
Integer,
91-
Double,
92-
String,
93-
Unknown(String),
94-
}
95-
96-
impl ProcessValue for OurLogAttributeType {}
97-
98-
impl OurLogAttributeType {
99-
pub fn as_str(&self) -> &str {
100-
match self {
101-
Self::Boolean => "boolean",
102-
Self::Integer => "integer",
103-
Self::Double => "double",
104-
Self::String => "string",
105-
Self::Unknown(value) => value,
106-
}
107-
}
108-
109-
pub fn unknown_string() -> String {
110-
"unknown".to_string()
111-
}
112-
}
113-
114-
impl fmt::Display for OurLogAttributeType {
115-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116-
write!(f, "{}", self.as_str())
117-
}
118-
}
119-
120-
impl From<String> for OurLogAttributeType {
121-
fn from(value: String) -> Self {
122-
match value.as_str() {
123-
"boolean" => Self::Boolean,
124-
"integer" => Self::Integer,
125-
"double" => Self::Double,
126-
"string" => Self::String,
127-
_ => Self::Unknown(value),
128-
}
129-
}
130-
}
131-
132-
impl Empty for OurLogAttributeType {
133-
#[inline]
134-
fn is_empty(&self) -> bool {
135-
false
136-
}
137-
}
138-
139-
impl FromValue for OurLogAttributeType {
140-
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
141-
match String::from_value(value) {
142-
Annotated(Some(value), meta) => Annotated(Some(value.into()), meta),
143-
Annotated(None, meta) => Annotated(None, meta),
144-
}
145-
}
146-
}
147-
148-
impl IntoValue for OurLogAttributeType {
149-
fn into_value(self) -> Value
150-
where
151-
Self: Sized,
152-
{
153-
Value::String(match self {
154-
Self::Unknown(s) => s,
155-
s => s.to_string(),
156-
})
157-
}
158-
159-
fn serialize_payload<S>(&self, s: S, _behavior: SkipSerialization) -> Result<S::Ok, S::Error>
160-
where
161-
Self: Sized,
162-
S: serde::Serializer,
163-
{
164-
serde::ser::Serialize::serialize(self.as_str(), s)
165-
}
166-
}
167-
16847
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16948
pub enum OurLogLevel {
17049
Trace,
@@ -289,7 +168,7 @@ mod tests {
289168
}"#;
290169

291170
let data = Annotated::<OurLog>::from_json(json).unwrap();
292-
insta::assert_debug_snapshot!(data, @r#"
171+
insta::assert_debug_snapshot!(data, @r###"
293172
OurLog {
294173
timestamp: Timestamp(
295174
2018-12-13T16:51:00Z,
@@ -301,49 +180,49 @@ mod tests {
301180
level: Info,
302181
body: "Example log record",
303182
attributes: {
304-
"boolean.attribute": OurLogAttribute {
183+
"boolean.attribute": Attribute {
305184
value: Bool(
306185
true,
307186
),
308187
type: Boolean,
309188
other: {},
310189
},
311-
"double.attribute": OurLogAttribute {
190+
"double.attribute": Attribute {
312191
value: F64(
313192
1.23,
314193
),
315194
type: Double,
316195
other: {},
317196
},
318-
"sentry.observed_timestamp_nanos": OurLogAttribute {
197+
"sentry.observed_timestamp_nanos": Attribute {
319198
value: String(
320199
"1544712660300000000",
321200
),
322201
type: Integer,
323202
other: {},
324203
},
325-
"sentry.severity_number": OurLogAttribute {
204+
"sentry.severity_number": Attribute {
326205
value: String(
327206
"10",
328207
),
329208
type: Integer,
330209
other: {},
331210
},
332-
"sentry.severity_text": OurLogAttribute {
211+
"sentry.severity_text": Attribute {
333212
value: String(
334213
"info",
335214
),
336215
type: String,
337216
other: {},
338217
},
339-
"sentry.trace_flags": OurLogAttribute {
218+
"sentry.trace_flags": Attribute {
340219
value: String(
341220
"10",
342221
),
343222
type: Integer,
344223
other: {},
345224
},
346-
"string.attribute": OurLogAttribute {
225+
"string.attribute": Attribute {
347226
value: String(
348227
"some string",
349228
),
@@ -353,7 +232,7 @@ mod tests {
353232
},
354233
other: {},
355234
}
356-
"#);
235+
"###);
357236

358237
insta::assert_json_snapshot!(SerializableAnnotated(&data), @r###"
359238
{

0 commit comments

Comments
 (0)