Skip to content

Commit d46d548

Browse files
authored
feat(eap): Infer the client ip if set to auto (#5304)
1 parent 5670ba0 commit d46d548

File tree

7 files changed

+36
-5
lines changed

7 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Features**:
66

77
- Only apply non-destructive PII rules to log bodies by default. ([#5272](https://github.com/getsentry/relay/pull/5272))
8+
- Infer the client ip when set to `{{auto}}` for EAP items. ([#5304](https://github.com/getsentry/relay/pull/5304))
89
- Add `sentry.origin` attribute to OTLP spans. ([#5294](https://github.com/getsentry/relay/pull/5294))
910

1011
**Breaking Changes**:

relay-conventions/src/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ macro_rules! convention_attributes {
1515
convention_attributes!(
1616
BROWSER_NAME => "sentry.browser.name",
1717
BROWSER_VERSION => "sentry.browser.version",
18+
CLIENT_ADDRESS => "client.address",
1819
DB_QUERY_TEXT => "db.query.text",
1920
DB_STATEMENT => "db.statement",
2021
DB_SYSTEM_NAME => "db.system.name",

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
//!
33
//! A central place for all modifications/normalizations for attributes.
44
5+
use std::net::IpAddr;
6+
57
use chrono::{DateTime, Utc};
68
use relay_common::time::UnixTimestamp;
79
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,
1012
};
1113
use relay_event_schema::protocol::{AttributeType, Attributes, BrowserContext, Geo};
1214
use relay_protocol::{Annotated, ErrorKind, Value};
@@ -106,6 +108,30 @@ pub fn normalize_user_agent(
106108
attributes.insert_if_missing(BROWSER_VERSION, || context.version);
107109
}
108110

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+
109135
/// Normalizes the user's geographical information into [`Attributes`].
110136
///
111137
/// Does not modify the attributes if there is already user geo information present,

relay-server/src/processing/logs/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn scrub_log(log: &mut Annotated<OurLog>, ctx: Context<'_>) -> Result<()> {
110110
fn normalize_log(log: &mut Annotated<OurLog>, meta: &RequestMeta) -> Result<()> {
111111
if let Some(log) = log.value_mut() {
112112
eap::normalize_received(&mut log.attributes, meta.received_at());
113+
eap::normalize_client_address(&mut log.attributes, meta.client_addr());
113114
eap::normalize_user_agent(&mut log.attributes, meta.user_agent(), meta.client_hints());
114115
eap::normalize_attribute_types(&mut log.attributes);
115116
}

relay-server/src/processing/spans/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ fn normalize_span(
8989

9090
if let Some(span) = span.value_mut() {
9191
eap::normalize_received(&mut span.attributes, meta.received_at());
92+
eap::normalize_client_address(&mut span.attributes, meta.client_addr());
9293
eap::normalize_user_agent(&mut span.attributes, meta.user_agent(), meta.client_hints());
9394
eap::normalize_attribute_types(&mut span.attributes);
9495
eap::normalize_user_geo(&mut span.attributes, || {

relay-server/src/processing/trace_metrics/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ fn scrub_trace_metric(metric: &mut Annotated<TraceMetric>, ctx: Context<'_>) ->
102102
fn normalize_trace_metric(metric: &mut Annotated<TraceMetric>, meta: &RequestMeta) -> Result<()> {
103103
if let Some(metric_value) = metric.value_mut() {
104104
eap::normalize_received(&mut metric_value.attributes, meta.received_at());
105+
eap::normalize_client_address(&mut metric_value.attributes, meta.client_addr());
105106
eap::normalize_user_agent(
106107
&mut metric_value.attributes,
107108
meta.user_agent(),

tests/integration/test_spans_standalone.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ def lcp_cls_inp_differences(mode):
2929
if mode == "legacy":
3030
attributes = {
3131
"browser.name": {"type": "string", "value": "Chrome"},
32-
"client.address": {"type": "string", "value": "127.0.0.1"},
3332
# Legacy behaviour, new field is `sentry.segment.name`
3433
# Maybe this shouldn't exist since parent and segment information is also removed
3534
"sentry.transaction": {"type": "string", "value": "/insights/projects/"},
3635
}
3736
fields = {}
3837
else:
3938
attributes = {
40-
# Not implemented
41-
"client.address": {"type": "string", "value": "{{auto}}"},
4239
# We additionally extract the browser version for EAP items
4340
"sentry.browser.version": {"type": "string", "value": "141.0.0"},
4441
# New for EAP items
@@ -127,6 +124,7 @@ def test_lcp_span(
127124

128125
assert spans_consumer.get_span() == {
129126
"attributes": {
127+
"client.address": {"type": "string", "value": "127.0.0.1"},
130128
"lcp": {"type": "double", "value": 548.0},
131129
"lcp.loadTime": {"type": "double", "value": 527.5},
132130
"lcp.renderTime": {"type": "integer", "value": 548},
@@ -297,6 +295,7 @@ def test_cls_span(
297295

298296
assert spans_consumer.get_span() == {
299297
"attributes": {
298+
"client.address": {"type": "string", "value": "127.0.0.1"},
300299
"cls": {"type": "double", "value": 0.1},
301300
"cls.source.1": {
302301
"type": "string",
@@ -467,6 +466,7 @@ def test_inp_span(
467466

468467
assert spans_consumer.get_span() == {
469468
"attributes": {
469+
"client.address": {"type": "string", "value": "127.0.0.1"},
470470
"inp": {"type": "double", "value": 104.0},
471471
"sentry.browser.name": {"type": "string", "value": "Chrome"},
472472
"sentry.description": {

0 commit comments

Comments
 (0)