@@ -6,9 +6,10 @@ use relay_event_schema::processor::{self, ProcessingState, Processor};
6
6
use relay_event_schema:: protocol:: { Contexts , IpAddr , Replay } ;
7
7
use relay_protocol:: Annotated ;
8
8
9
+ use crate :: event:: normalize_user_geoinfo;
9
10
use crate :: normalize:: user_agent;
10
- use crate :: trimming;
11
11
use crate :: user_agent:: RawUserAgentInfo ;
12
+ use crate :: { trimming, GeoIpLookup } ;
12
13
13
14
/// Replay validation or normalization error.
14
15
///
@@ -82,11 +83,17 @@ pub fn validate(replay: &Replay) -> Result<(), ReplayError> {
82
83
pub fn normalize (
83
84
replay : & mut Annotated < Replay > ,
84
85
client_ip : Option < StdIpAddr > ,
85
- user_agent : & RawUserAgentInfo < & str > ,
86
+ user_agent : RawUserAgentInfo < & str > ,
87
+ geoip_lookup : Option < & GeoIpLookup > ,
86
88
) {
87
89
let _ = processor:: apply ( replay, |replay_value, meta| {
88
90
normalize_platform ( replay_value) ;
89
91
normalize_ip_address ( replay_value, client_ip) ;
92
+ if let Some ( geoip_lookup) = geoip_lookup {
93
+ if let Some ( user) = replay_value. user . value_mut ( ) {
94
+ normalize_user_geoinfo ( geoip_lookup, user) ;
95
+ }
96
+ }
90
97
normalize_user_agent ( replay_value, user_agent) ;
91
98
normalize_type ( replay_value) ;
92
99
normalize_array_fields ( replay_value) ;
@@ -124,7 +131,7 @@ fn normalize_ip_address(replay: &mut Replay, ip_address: Option<StdIpAddr>) {
124
131
) ;
125
132
}
126
133
127
- fn normalize_user_agent ( replay : & mut Replay , default_user_agent : & RawUserAgentInfo < & str > ) {
134
+ fn normalize_user_agent ( replay : & mut Replay , default_user_agent : RawUserAgentInfo < & str > ) {
128
135
let headers = match replay
129
136
. request
130
137
. value ( )
@@ -139,11 +146,11 @@ fn normalize_user_agent(replay: &mut Replay, default_user_agent: &RawUserAgentIn
139
146
let user_agent_info = if user_agent_info. is_empty ( ) {
140
147
default_user_agent
141
148
} else {
142
- & user_agent_info
149
+ user_agent_info
143
150
} ;
144
151
145
152
let contexts = replay. contexts . get_or_insert_with ( Contexts :: new) ;
146
- user_agent:: normalize_user_agent_info_generic ( contexts, & replay. platform , user_agent_info) ;
153
+ user_agent:: normalize_user_agent_info_generic ( contexts, & replay. platform , & user_agent_info) ;
147
154
}
148
155
149
156
fn normalize_platform ( replay : & mut Replay ) {
@@ -165,7 +172,8 @@ mod tests {
165
172
use std:: net:: { IpAddr , Ipv4Addr } ;
166
173
167
174
use chrono:: { TimeZone , Utc } ;
168
- use relay_protocol:: { assert_annotated_snapshot, get_value} ;
175
+ use insta:: { assert_debug_snapshot, assert_json_snapshot} ;
176
+ use relay_protocol:: { assert_annotated_snapshot, get_value, SerializableAnnotated } ;
169
177
use uuid:: Uuid ;
170
178
171
179
use relay_event_schema:: protocol:: {
@@ -253,7 +261,7 @@ mod tests {
253
261
let payload = include_str ! ( "../../tests/fixtures/replay.json" ) ;
254
262
255
263
let mut replay: Annotated < Replay > = Annotated :: from_json ( payload) . unwrap ( ) ;
256
- normalize ( & mut replay, None , & RawUserAgentInfo :: default ( ) ) ;
264
+ normalize ( & mut replay, None , RawUserAgentInfo :: default ( ) , None ) ;
257
265
258
266
let contexts = get_value ! ( replay. contexts!) ;
259
267
assert_eq ! (
@@ -290,37 +298,61 @@ mod tests {
290
298
let mut replay: Annotated < Replay > = Annotated :: from_json ( payload) . unwrap ( ) ;
291
299
292
300
// No user object and no ip-address was provided.
293
- normalize ( & mut replay, None , & RawUserAgentInfo :: default ( ) ) ;
301
+ normalize ( & mut replay, None , RawUserAgentInfo :: default ( ) , None ) ;
294
302
assert_eq ! ( get_value!( replay. user) , None ) ;
295
303
296
304
// No user object but an ip-address was provided.
297
305
let ip_address = IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) ;
298
- normalize ( & mut replay, Some ( ip_address) , & RawUserAgentInfo :: default ( ) ) ;
306
+ normalize (
307
+ & mut replay,
308
+ Some ( ip_address) ,
309
+ RawUserAgentInfo :: default ( ) ,
310
+ None ,
311
+ ) ;
299
312
300
313
let ipaddr = get_value ! ( replay. user!) . ip_address . as_str ( ) ;
301
314
assert_eq ! ( Some ( "127.0.0.1" ) , ipaddr) ;
302
315
}
303
316
304
317
#[ test]
305
- fn test_set_ip_address_missing_user_ip_address ( ) {
306
- let ip_address = IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) ;
318
+ fn test_set_ip_address_missing_user_ip_address_and_geo ( ) {
319
+ let lookup = GeoIpLookup :: open ( "tests/fixtures/GeoIP2-Enterprise-Test.mmdb" ) . unwrap ( ) ;
320
+ let ip_address = IpAddr :: V4 ( Ipv4Addr :: new ( 2 , 125 , 160 , 216 ) ) ;
307
321
308
322
// IP-Address set.
309
323
let payload = include_str ! ( "../../tests/fixtures/replay_missing_user_ip_address.json" ) ;
310
324
311
325
let mut replay: Annotated < Replay > = Annotated :: from_json ( payload) . unwrap ( ) ;
312
- normalize ( & mut replay, Some ( ip_address) , & RawUserAgentInfo :: default ( ) ) ;
326
+ normalize (
327
+ & mut replay,
328
+ Some ( ip_address) ,
329
+ RawUserAgentInfo :: default ( ) ,
330
+ Some ( & lookup) ,
331
+ ) ;
313
332
314
- let ipaddr = get_value ! ( replay. user!) . ip_address . as_str ( ) ;
315
- assert_eq ! ( Some ( "127.0.0.1" ) , ipaddr) ;
333
+ let user = & replay. value ( ) . unwrap ( ) . user ;
334
+ assert_json_snapshot ! ( SerializableAnnotated ( user) , @r###"
335
+ {
336
+ "id": "123",
337
+
338
+ "ip_address": "2.125.160.216",
339
+ "username": "user",
340
+ "geo": {
341
+ "country_code": "GB",
342
+ "city": "Boxford",
343
+ "subdivision": "England",
344
+ "region": "United Kingdom"
345
+ }
346
+ }
347
+ "### ) ;
316
348
}
317
349
318
350
#[ test]
319
351
fn test_loose_type_requirements ( ) {
320
352
let payload = include_str ! ( "../../tests/fixtures/replay_failure_22_08_31.json" ) ;
321
353
322
354
let mut replay: Annotated < Replay > = Annotated :: from_json ( payload) . unwrap ( ) ;
323
- normalize ( & mut replay, None , & RawUserAgentInfo :: default ( ) ) ;
355
+ normalize ( & mut replay, None , RawUserAgentInfo :: default ( ) , None ) ;
324
356
325
357
let user = get_value ! ( replay. user!) ;
326
358
assert_eq ! ( user. ip_address. as_str( ) , Some ( "127.1.1.1" ) ) ;
@@ -444,7 +476,7 @@ mod tests {
444
476
let json = format ! ( r#"{{"dist": "{}"}}"# , "0" . repeat( 100 ) ) ;
445
477
let mut replay = Annotated :: < Replay > :: from_json ( json. as_str ( ) ) . unwrap ( ) ;
446
478
447
- normalize ( & mut replay, None , & RawUserAgentInfo :: default ( ) ) ;
479
+ normalize ( & mut replay, None , RawUserAgentInfo :: default ( ) , None ) ;
448
480
assert_annotated_snapshot ! ( replay, @r#"{
449
481
"platform": "other",
450
482
"dist": "0000000000000000000000000000000000000000000000000000000000000...",
0 commit comments