Skip to content

Commit 6a9dc11

Browse files
committed
ref + handle arrays
1 parent 4d8a191 commit 6a9dc11

File tree

1 file changed

+128
-8
lines changed

1 file changed

+128
-8
lines changed

relay-event-normalization/src/eap/trimming.rs

Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct TrimmingProcessor {
3030
size_state: Vec<SizeState>,
3131
/// Whether we are currently trimming a collection of attributes.
3232
/// This case needs to be distinguished for the purpose of accounting
33-
/// for string lengths.
33+
/// for bool/number lengths, which we only want to count in attributes.
3434
in_attributes: bool,
3535
}
3636

@@ -141,6 +141,53 @@ impl Processor for TrimmingProcessor {
141141

142142
Ok(())
143143
}
144+
fn process_u64(
145+
&mut self,
146+
_value: &mut u64,
147+
_meta: &mut Meta,
148+
_state: &ProcessingState<'_>,
149+
) -> ProcessingResult {
150+
if self.in_attributes {
151+
self.consume_size(8);
152+
}
153+
Ok(())
154+
}
155+
156+
fn process_i64(
157+
&mut self,
158+
_value: &mut i64,
159+
_meta: &mut Meta,
160+
_state: &ProcessingState<'_>,
161+
) -> ProcessingResult {
162+
if self.in_attributes {
163+
self.consume_size(8);
164+
}
165+
Ok(())
166+
}
167+
168+
fn process_f64(
169+
&mut self,
170+
_value: &mut f64,
171+
_meta: &mut Meta,
172+
_state: &ProcessingState<'_>,
173+
) -> ProcessingResult {
174+
if self.in_attributes {
175+
self.consume_size(8);
176+
}
177+
Ok(())
178+
}
179+
180+
fn process_bool(
181+
&mut self,
182+
_value: &mut bool,
183+
_meta: &mut Meta,
184+
_state: &ProcessingState<'_>,
185+
) -> ProcessingResult {
186+
if self.in_attributes {
187+
self.consume_size(1);
188+
}
189+
Ok(())
190+
}
144191

145192
fn process_string(
146193
&mut self,
@@ -153,18 +200,15 @@ impl Processor for TrimmingProcessor {
153200
}
154201

155202
if !state.attrs().trim {
203+
self.consume_size(value.len());
156204
return Ok(());
157205
}
158206

159207
if let Some(size_remaining) = self.remaining_size() {
160208
crate::trimming::trim_string(value, meta, size_remaining, 0);
161209
}
162210

163-
// Only count string size here if we're _not_ currently trimming attributes.
164-
// In that case, the size accounting is already handled by `process_attributes`.
165-
if !self.in_attributes {
166-
self.consume_size(value.len());
167-
}
211+
self.consume_size(value.len());
168212

169213
Ok(())
170214
}
@@ -294,7 +338,6 @@ impl Processor for TrimmingProcessor {
294338
processor::process_value(value, self, &value_state).inspect_err(|_| {
295339
self.in_attributes = false;
296340
})?;
297-
self.consume_size(size::attribute_size(value));
298341
}
299342

300343
if let Some(split_idx) = split_idx {
@@ -316,7 +359,8 @@ impl Processor for TrimmingProcessor {
316359

317360
#[cfg(test)]
318361
mod tests {
319-
use relay_protocol::{Annotated, FromValue, IntoValue, SerializableAnnotated};
362+
use relay_event_schema::protocol::{AttributeType, AttributeValue};
363+
use relay_protocol::{Annotated, FromValue, IntoValue, SerializableAnnotated, Value};
320364

321365
use super::*;
322366

@@ -595,4 +639,80 @@ mod tests {
595639
}
596640
"###);
597641
}
642+
643+
#[test]
644+
fn test_array_attribute() {
645+
let mut attributes = Attributes::new();
646+
647+
let array = vec![
648+
Annotated::new("first string".into()),
649+
Annotated::new("second string".into()),
650+
Annotated::new("another string".into()),
651+
Annotated::new("last string".into()),
652+
];
653+
654+
attributes.insert(
655+
"array",
656+
AttributeValue {
657+
ty: Annotated::new(AttributeType::Array),
658+
value: Annotated::new(Value::Array(array)),
659+
},
660+
);
661+
662+
let mut value = Annotated::new(TestObject {
663+
attributes: Annotated::new(attributes),
664+
number: Annotated::new(0),
665+
body: Annotated::new("Short".to_owned()),
666+
});
667+
668+
let mut processor = TrimmingProcessor::new(None);
669+
let state = ProcessingState::new_root(Default::default(), []);
670+
processor::process_value(&mut value, &mut processor, &state).unwrap();
671+
672+
// The key `"array"` and the first and second array value take up 5 + 12 + 13 = 30B in total,
673+
// leaving 10B for the third array value and nothing for the last.
674+
insta::assert_json_snapshot!(SerializableAnnotated(&value), @r###"
675+
{
676+
"body": "Short",
677+
"number": 0,
678+
"attributes": {
679+
"array": {
680+
"type": "array",
681+
"value": [
682+
"first string",
683+
"second string",
684+
"another..."
685+
]
686+
}
687+
},
688+
"_meta": {
689+
"attributes": {
690+
"": {
691+
"len": 55
692+
},
693+
"array": {
694+
"value": {
695+
"": {
696+
"len": 4
697+
},
698+
"2": {
699+
"": {
700+
"rem": [
701+
[
702+
"!limit",
703+
"s",
704+
7,
705+
10
706+
]
707+
],
708+
"len": 14
709+
}
710+
}
711+
}
712+
}
713+
}
714+
}
715+
}
716+
"###);
717+
}
598718
}

0 commit comments

Comments
 (0)