Skip to content

Commit

Permalink
ref(replay): Run geo info normalization on the replay user
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Sep 30, 2024
1 parent fabbfe9 commit 7293a50
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 150 deletions.
39 changes: 28 additions & 11 deletions relay-event-normalization/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use relay_event_schema::processor::{self, ProcessingState, Processor};
use relay_event_schema::protocol::{Contexts, IpAddr, Replay};
use relay_protocol::Annotated;

use crate::event::normalize_user_geoinfo;
use crate::normalize::user_agent;
use crate::trimming;
use crate::user_agent::RawUserAgentInfo;
use crate::{trimming, GeoIpLookup};

/// Replay validation or normalization error.
///
Expand Down Expand Up @@ -82,11 +83,17 @@ pub fn validate(replay: &Replay) -> Result<(), ReplayError> {
pub fn normalize(
replay: &mut Annotated<Replay>,
client_ip: Option<StdIpAddr>,
user_agent: &RawUserAgentInfo<&str>,
user_agent: RawUserAgentInfo<&str>,
geoip_lookup: Option<&GeoIpLookup>,
) {
let _ = processor::apply(replay, |replay_value, meta| {
normalize_platform(replay_value);
normalize_ip_address(replay_value, client_ip);
if let Some(geoip_lookup) = geoip_lookup {
if let Some(user) = replay_value.user.value_mut() {
normalize_user_geoinfo(geoip_lookup, user);
}
}
normalize_user_agent(replay_value, user_agent);
normalize_type(replay_value);
normalize_array_fields(replay_value);
Expand Down Expand Up @@ -124,7 +131,7 @@ fn normalize_ip_address(replay: &mut Replay, ip_address: Option<StdIpAddr>) {
);
}

fn normalize_user_agent(replay: &mut Replay, default_user_agent: &RawUserAgentInfo<&str>) {
fn normalize_user_agent(replay: &mut Replay, default_user_agent: RawUserAgentInfo<&str>) {
let headers = match replay
.request
.value()
Expand All @@ -139,11 +146,11 @@ fn normalize_user_agent(replay: &mut Replay, default_user_agent: &RawUserAgentIn
let user_agent_info = if user_agent_info.is_empty() {
default_user_agent
} else {
&user_agent_info
user_agent_info
};

let contexts = replay.contexts.get_or_insert_with(Contexts::new);
user_agent::normalize_user_agent_info_generic(contexts, &replay.platform, user_agent_info);
user_agent::normalize_user_agent_info_generic(contexts, &replay.platform, &user_agent_info);
}

fn normalize_platform(replay: &mut Replay) {
Expand Down Expand Up @@ -253,7 +260,7 @@ mod tests {
let payload = include_str!("../../tests/fixtures/replay.json");

let mut replay: Annotated<Replay> = Annotated::from_json(payload).unwrap();
normalize(&mut replay, None, &RawUserAgentInfo::default());
normalize(&mut replay, None, RawUserAgentInfo::default(), None);

let contexts = get_value!(replay.contexts!);
assert_eq!(
Expand Down Expand Up @@ -290,12 +297,17 @@ mod tests {
let mut replay: Annotated<Replay> = Annotated::from_json(payload).unwrap();

// No user object and no ip-address was provided.
normalize(&mut replay, None, &RawUserAgentInfo::default());
normalize(&mut replay, None, RawUserAgentInfo::default(), None);
assert_eq!(get_value!(replay.user), None);

// No user object but an ip-address was provided.
let ip_address = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
normalize(&mut replay, Some(ip_address), &RawUserAgentInfo::default());
normalize(
&mut replay,
Some(ip_address),
RawUserAgentInfo::default(),
None,
);

let ipaddr = get_value!(replay.user!).ip_address.as_str();
assert_eq!(Some("127.0.0.1"), ipaddr);
Expand All @@ -309,7 +321,12 @@ mod tests {
let payload = include_str!("../../tests/fixtures/replay_missing_user_ip_address.json");

let mut replay: Annotated<Replay> = Annotated::from_json(payload).unwrap();
normalize(&mut replay, Some(ip_address), &RawUserAgentInfo::default());
normalize(
&mut replay,
Some(ip_address),
RawUserAgentInfo::default(),
None,
);

let ipaddr = get_value!(replay.user!).ip_address.as_str();
assert_eq!(Some("127.0.0.1"), ipaddr);
Expand All @@ -320,7 +337,7 @@ mod tests {
let payload = include_str!("../../tests/fixtures/replay_failure_22_08_31.json");

let mut replay: Annotated<Replay> = Annotated::from_json(payload).unwrap();
normalize(&mut replay, None, &RawUserAgentInfo::default());
normalize(&mut replay, None, RawUserAgentInfo::default(), None);

let user = get_value!(replay.user!);
assert_eq!(user.ip_address.as_str(), Some("127.1.1.1"));
Expand Down Expand Up @@ -444,7 +461,7 @@ mod tests {
let json = format!(r#"{{"dist": "{}"}}"#, "0".repeat(100));
let mut replay = Annotated::<Replay>::from_json(json.as_str()).unwrap();

normalize(&mut replay, None, &RawUserAgentInfo::default());
normalize(&mut replay, None, RawUserAgentInfo::default(), None);
assert_annotated_snapshot!(replay, @r#"{
"platform": "other",
"dist": "0000000000000000000000000000000000000000000000000000000000000...",
Expand Down
6 changes: 5 additions & 1 deletion relay-server/src/services/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,11 @@ impl EnvelopeProcessorService {
&self,
state: &mut ProcessEnvelopeState<ReplayGroup>,
) -> Result<(), ProcessingError> {
replay::process(state, &self.inner.global_config.current())?;
replay::process(
state,
&self.inner.global_config.current(),
self.inner.geoip_lookup.as_ref(),
)?;
if_processing!(self.inner.config, {
self.enforce_quotas(state)?;
});
Expand Down
Loading

0 comments on commit 7293a50

Please sign in to comment.