|
2 | 2 | //! |
3 | 3 | //! A central place for all modifications/normalizations for attributes. |
4 | 4 |
|
| 5 | +use std::net::IpAddr; |
| 6 | + |
5 | 7 | use chrono::{DateTime, Utc}; |
6 | 8 | use relay_common::time::UnixTimestamp; |
7 | 9 | use relay_conventions::{ |
8 | | - BROWSER_NAME, BROWSER_VERSION, OBSERVED_TIMESTAMP_NANOS, USER_AGENT_ORIGINAL, USER_GEO_CITY, |
9 | | - USER_GEO_COUNTRY_CODE, USER_GEO_REGION, USER_GEO_SUBDIVISION, |
| 10 | + BROWSER_NAME, BROWSER_VERSION, CLIENT_ADDRESS, OBSERVED_TIMESTAMP_NANOS, USER_AGENT_ORIGINAL, |
| 11 | + USER_GEO_CITY, USER_GEO_COUNTRY_CODE, USER_GEO_REGION, USER_GEO_SUBDIVISION, |
10 | 12 | }; |
11 | 13 | use relay_event_schema::protocol::{AttributeType, Attributes, BrowserContext, Geo}; |
12 | 14 | use relay_protocol::{Annotated, ErrorKind, Value}; |
@@ -106,6 +108,30 @@ pub fn normalize_user_agent( |
106 | 108 | attributes.insert_if_missing(BROWSER_VERSION, || context.version); |
107 | 109 | } |
108 | 110 |
|
| 111 | +/// Normalizes the client address into [`Attributes`]. |
| 112 | +/// |
| 113 | +/// Infers the client ip from the client information which was provided to Relay, if the SDK |
| 114 | +/// indicates the client ip should be inferred by setting it to `{{auto}}`. |
| 115 | +/// |
| 116 | +/// This requires cooperation from SDKs as inferring a client ip only works in non-server |
| 117 | +/// environments, where the user/client device is also the device sending the item. |
| 118 | +pub fn normalize_client_address(attributes: &mut Annotated<Attributes>, client_ip: Option<IpAddr>) { |
| 119 | + let Some(attributes) = attributes.value_mut() else { |
| 120 | + return; |
| 121 | + }; |
| 122 | + let Some(client_ip) = client_ip else { |
| 123 | + return; |
| 124 | + }; |
| 125 | + |
| 126 | + let client_address = attributes |
| 127 | + .get_value(CLIENT_ADDRESS) |
| 128 | + .and_then(|v| v.as_str()); |
| 129 | + |
| 130 | + if client_address == Some("{{auto}}") { |
| 131 | + attributes.insert(CLIENT_ADDRESS, client_ip.to_string()); |
| 132 | + } |
| 133 | +} |
| 134 | + |
109 | 135 | /// Normalizes the user's geographical information into [`Attributes`]. |
110 | 136 | /// |
111 | 137 | /// Does not modify the attributes if there is already user geo information present, |
|
0 commit comments