Skip to content

Commit 66c2f82

Browse files
authored
chore(trace): Add extra "data" field to the trace context (#2899)
Adds extra `data` field to the trace context with nested types for better schema definition.
1 parent 8a6b8da commit 66c2f82

File tree

4 files changed

+218
-1
lines changed

4 files changed

+218
-1
lines changed

relay-event-schema/src/protocol/contexts/trace.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,58 @@ pub struct TraceContext {
118118
/// This flag only applies to events with [`Error`] type that have an associated dynamic sampling context.
119119
pub sampled: Annotated<bool>,
120120

121+
/// Arbitrary additional data on a trace.
122+
#[metastructure(pii = "maybe", skip_serialization = "empty")]
123+
pub data: Annotated<Data>,
124+
121125
/// Additional arbitrary fields for forwards compatibility.
122126
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
123127
pub other: Object<Value>,
124128
}
125129

130+
/// The arbitrary data on the trace.
131+
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
132+
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
133+
pub struct Data {
134+
/// The current route in the application.
135+
///
136+
/// Set by React Native SDK.
137+
#[metastructure(pii = "maybe", skip_serialization = "empty")]
138+
pub route: Annotated<Route>,
139+
/// The previous route in the application
140+
///
141+
/// Set by React Native SDK.
142+
#[metastructure(field = "previousRoute", pii = "maybe", skip_serialization = "empty")]
143+
pub previous_route: Annotated<Route>,
144+
145+
/// Additional arbitrary fields for forwards compatibility.
146+
#[metastructure(
147+
additional_properties,
148+
retain = "true",
149+
pii = "maybe",
150+
skip_serialization = "empty"
151+
)]
152+
pub other: Object<Value>,
153+
}
154+
155+
/// The route in the application, set by React Native SDK.
156+
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
157+
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
158+
pub struct Route {
159+
/// Parameters assigned to this route.
160+
#[metastructure(pii = "true", skip_serialization = "empty", bag_size = "medium")]
161+
params: Annotated<Object<Value>>,
162+
163+
/// Additional arbitrary fields for forwards compatibility.
164+
#[metastructure(
165+
additional_properties,
166+
retain = "true",
167+
pii = "maybe",
168+
skip_serialization = "empty"
169+
)]
170+
pub other: Object<Value>,
171+
}
172+
126173
impl super::DefaultContext for TraceContext {
127174
fn default_key() -> &'static str {
128175
"trace"
@@ -170,6 +217,14 @@ mod tests {
170217
"exclusive_time": 0.0,
171218
"client_sample_rate": 0.5,
172219
"origin": "auto.http",
220+
"data": {
221+
"route": {
222+
"params": {
223+
"tok": "test"
224+
},
225+
"path": "/path"
226+
}
227+
},
173228
"other": "value",
174229
"type": "trace"
175230
}"#;
@@ -182,6 +237,27 @@ mod tests {
182237
exclusive_time: Annotated::new(0.0),
183238
client_sample_rate: Annotated::new(0.5),
184239
origin: Annotated::new("auto.http".to_owned()),
240+
data: Annotated::new(Data {
241+
route: Annotated::new(Route {
242+
params: Annotated::new({
243+
let mut map = Object::new();
244+
map.insert(
245+
"tok".to_string(),
246+
Annotated::new(Value::String("test".into())),
247+
);
248+
map
249+
}),
250+
other: {
251+
let mut map = Object::new();
252+
map.insert(
253+
"path".to_string(),
254+
Annotated::new(Value::String("/path".into())),
255+
);
256+
map
257+
},
258+
}),
259+
..Default::default()
260+
}),
185261
other: {
186262
let mut map = Object::new();
187263
map.insert(

relay-pii/src/processor.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ mod tests {
470470
use relay_event_schema::processor::process_value;
471471
use relay_event_schema::protocol::{
472472
Addr, Breadcrumb, DebugImage, DebugMeta, Event, ExtraValue, Headers, LogEntry,
473-
NativeDebugImage, Request, Span, TagEntry, Tags,
473+
NativeDebugImage, Request, Span, TagEntry, Tags, TraceContext,
474474
};
475475
use relay_protocol::{assert_annotated_snapshot, Annotated, FromValue, Object, Value};
476476

@@ -1163,6 +1163,43 @@ mod tests {
11631163
assert_eq!(chunks, res);
11641164
}
11651165

1166+
#[test]
1167+
fn test_trace_route_params_scrubbed() {
1168+
let mut trace_context: Annotated<TraceContext> = Annotated::from_json(
1169+
r#"
1170+
{
1171+
"type": "trace",
1172+
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
1173+
"span_id": "fa90fdead5f74052",
1174+
"data": {
1175+
"previousRoute": {
1176+
"params": {
1177+
"password": "test"
1178+
}
1179+
}
1180+
}
1181+
}
1182+
"#,
1183+
)
1184+
.unwrap();
1185+
1186+
let ds_config = DataScrubbingConfig {
1187+
scrub_data: true,
1188+
scrub_defaults: true,
1189+
..Default::default()
1190+
};
1191+
let pii_config = ds_config.pii_config().unwrap().as_ref().unwrap();
1192+
let mut pii_processor = PiiProcessor::new(pii_config.compiled());
1193+
1194+
process_value(
1195+
&mut trace_context,
1196+
&mut pii_processor,
1197+
ProcessingState::root(),
1198+
)
1199+
.unwrap();
1200+
assert_annotated_snapshot!(trace_context);
1201+
}
1202+
11661203
#[test]
11671204
fn test_scrub_span_data_http_not_scrubbed() {
11681205
let mut span: Annotated<Span> = Annotated::from_json(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
source: relay-pii/src/processor.rs
3+
expression: trace_context
4+
---
5+
{
6+
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
7+
"span_id": "fa90fdead5f74052",
8+
"data": {
9+
"previousRoute": {
10+
"params": {
11+
"password": "[Filtered]"
12+
}
13+
}
14+
},
15+
"type": "trace",
16+
"_meta": {
17+
"data": {
18+
"previousRoute": {
19+
"params": {
20+
"password": {
21+
"": {
22+
"rem": [
23+
[
24+
"@password:filter",
25+
"s",
26+
0,
27+
10
28+
]
29+
],
30+
"len": 4
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}

relay-server/tests/snapshots/test_fixtures__event_schema.snap

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,41 @@ expression: "relay_event_schema::protocol::event_json_schema()"
10051005
}
10061006
]
10071007
},
1008+
"Data": {
1009+
"description": " The arbitrary data on the trace.",
1010+
"anyOf": [
1011+
{
1012+
"type": "object",
1013+
"properties": {
1014+
"previousRoute": {
1015+
"description": " The previous route in the application\n\n Set by React Native SDK.",
1016+
"default": null,
1017+
"anyOf": [
1018+
{
1019+
"$ref": "#/definitions/Route"
1020+
},
1021+
{
1022+
"type": "null"
1023+
}
1024+
]
1025+
},
1026+
"route": {
1027+
"description": " The current route in the application.\n\n Set by React Native SDK.",
1028+
"default": null,
1029+
"anyOf": [
1030+
{
1031+
"$ref": "#/definitions/Route"
1032+
},
1033+
{
1034+
"type": "null"
1035+
}
1036+
]
1037+
}
1038+
},
1039+
"additionalProperties": false
1040+
}
1041+
]
1042+
},
10081043
"DebugId": {
10091044
"type": "string"
10101045
},
@@ -3214,6 +3249,26 @@ expression: "relay_event_schema::protocol::event_json_schema()"
32143249
}
32153250
]
32163251
},
3252+
"Route": {
3253+
"description": " The route in the application, set by React Native SDK.",
3254+
"anyOf": [
3255+
{
3256+
"type": "object",
3257+
"properties": {
3258+
"params": {
3259+
"description": " Parameters assigned to this route.",
3260+
"default": null,
3261+
"type": [
3262+
"object",
3263+
"null"
3264+
],
3265+
"additionalProperties": true
3266+
}
3267+
},
3268+
"additionalProperties": false
3269+
}
3270+
]
3271+
},
32173272
"RuntimeContext": {
32183273
"description": " Runtime information.\n\n Runtime context describes a runtime in more detail. Typically, this context is present in\n `contexts` multiple times if multiple runtimes are involved (for instance, if you have a\n JavaScript application running on top of JVM).",
32193274
"anyOf": [
@@ -3579,6 +3634,18 @@ expression: "relay_event_schema::protocol::event_json_schema()"
35793634
],
35803635
"format": "double"
35813636
},
3637+
"data": {
3638+
"description": " Arbitrary additional data on a trace.",
3639+
"default": null,
3640+
"anyOf": [
3641+
{
3642+
"$ref": "#/definitions/Data"
3643+
},
3644+
{
3645+
"type": "null"
3646+
}
3647+
]
3648+
},
35823649
"exclusive_time": {
35833650
"description": " The amount of time in milliseconds spent in this transaction span,\n excluding its immediate child spans.",
35843651
"default": null,

0 commit comments

Comments
 (0)