Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(metrics-extraction): Add defaulting of required attributes in light normalization #2961

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Internal**:

- Proactively move on-disk spool to memory. ([#2949](https://github.com/getsentry/relay/pull/2949))
- Default missing `Event.platform` and `Event.level` fields during light normalization. ([#2961](https://github.com/getsentry/relay/pull/2961))

**Bug Fixes**:

Expand Down
59 changes: 57 additions & 2 deletions relay-event-normalization/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use relay_event_schema::processor::{
};
use relay_event_schema::protocol::{
AsPair, Context, ContextInner, Contexts, DeviceClass, Event, EventType, Exception, Headers,
IpAddr, LogEntry, Measurement, Measurements, NelContext, Request, SpanAttribute, SpanStatus,
Tags, User,
IpAddr, Level, LogEntry, Measurement, Measurements, NelContext, Request, SpanAttribute,
SpanStatus, Tags, User,
};
use relay_protocol::{Annotated, Empty, Error, ErrorKind, Meta, Object, Value};
use smallvec::SmallVec;
Expand Down Expand Up @@ -253,6 +253,7 @@ fn normalize(event: &mut Event, meta: &mut Meta, config: &NormalizationConfig) -
normalize_logentry(&mut event.logentry, meta);
normalize_release_dist(event); // dist is a tag extracted along with other metrics from transactions
normalize_event_tags(event); // Tags are added to every metric
normalize_platform_and_level(event);

// TODO: Consider moving to store normalization
if config.device_class_synthesis_config {
Expand Down Expand Up @@ -455,6 +456,18 @@ fn normalize_dist(distribution: &mut Annotated<String>) {
});
}

/// Defaults the `platform` and `level` required attributes.
fn normalize_platform_and_level(event: &mut Event) {
// The defaulting behavior, was inherited from `StoreNormalizeProcessor` and it's put here since only light
// normalization happens before metrics extraction and we want the metrics extraction pipeline to already work
// on some normalized data.
event.platform.get_or_insert_with(|| "other".to_string());
event.level.get_or_insert_with(|| match event.ty.value() {
Comment on lines +464 to +465
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved both platform and level normalization, since both defaulted to values which might be useful for metrics extraction. I could technically move all the default normalizations, @iker-barriocanal what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good enough for now, thanks!

Some(EventType::Transaction) => Level::Info,
_ => Level::Error,
});
}

/// Validates the timestamp range and sets a default value.
fn normalize_timestamps(
event: &mut Event,
Expand Down Expand Up @@ -1111,6 +1124,48 @@ mod tests {
assert_eq!(dist.value(), None);
}

#[test]
fn test_normalize_platform_and_level_with_transaction_event() {
let json = r#"
{
"type": "transaction"
}
"#;

let mut event = Annotated::<Event>::from_json(json).unwrap().0.unwrap();

normalize_platform_and_level(&mut event);

insta::assert_ron_snapshot!(SerializableAnnotated(&Annotated::new(event)), {}, @r###"
{
"level": "info",
"type": "transaction",
"platform": "other",
}
"###);
}

#[test]
fn test_normalize_platform_and_level_with_error_event() {
let json = r#"
{
"type": "error"
}
"#;

let mut event = Annotated::<Event>::from_json(json).unwrap().0.unwrap();

normalize_platform_and_level(&mut event);

insta::assert_ron_snapshot!(SerializableAnnotated(&Annotated::new(event)), {}, @r###"
{
"level": "error",
"type": "error",
"platform": "other",
}
"###);
}

#[test]
fn test_computed_measurements() {
let json = r#"
Expand Down
3 changes: 3 additions & 0 deletions relay-event-schema/src/protocol/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ mod tests {
#[test]
fn test_field_value_provider_event_filled() {
let event = Event {
level: Annotated::new(Level::Info),
release: Annotated::new(LenientString("1.1.1".to_owned())),
environment: Annotated::new("prod".to_owned()),
user: Annotated::new(User {
Expand Down Expand Up @@ -1146,6 +1147,8 @@ mod tests {
..Default::default()
};

assert_eq!(Some(Val::String("info")), event.get_value("event.level"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test just to make sure the getter works as expected.


assert_eq!(Some(Val::String("1.1.1")), event.get_value("event.release"));
assert_eq!(
Some(Val::String("prod")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
},
"culprit": "default-src self",
"environment": "production",
"level": "error",
"logentry": {
"formatted": "Blocked 'default-src' from 'evilhackerscripts.com'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"request": {
"headers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
},
"culprit": "style-src cdn.example.com",
"environment": "production",
"level": "error",
"logentry": {
"formatted": "Blocked 'style' from 'localhost:8000'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"request": {
"headers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
},
"culprit": "style-src cdn.example.com",
"environment": "production",
"level": "error",
"logentry": {
"formatted": "Blocked 'style' from 'notlocalhost:8000'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"request": {
"headers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
},
"culprit": "style-src http://cdn.example.com",
"environment": "production",
"level": "error",
"logentry": {
"formatted": "Blocked 'style' from 'notlocalhost:8000'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"request": {
"headers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
}
},
"culprit": "www.example.com",
"level": "error",
"logentry": {
"formatted": "Expect-CT failed for 'www.example.com'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"environment": "production",
"request": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
}
},
"culprit": "www.example.com",
"level": "error",
"logentry": {
"formatted": "Expect-Staple failed for 'www.example.com'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"environment": "production",
"request": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"version": ">=10"
}
},
"level": "error",
"logentry": {
"formatted": "Public key pinning validation failed for 'www.example.com'"
},
"logger": "csp",
"platform": "other",
"release": "01d5c3165d9fbc5c8bdcf9550a1d6793a80fc02b",
"environment": "production",
"request": {
Expand Down