Skip to content

Commit

Permalink
chore(trace): Add extra "data" field to the trace context (#2899)
Browse files Browse the repository at this point in the history
Adds extra `data` field to the trace context with nested types for
better schema definition.
  • Loading branch information
olksdr committed Jan 2, 2024
1 parent 8a6b8da commit 66c2f82
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 1 deletion.
76 changes: 76 additions & 0 deletions relay-event-schema/src/protocol/contexts/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,58 @@ pub struct TraceContext {
/// This flag only applies to events with [`Error`] type that have an associated dynamic sampling context.
pub sampled: Annotated<bool>,

/// Arbitrary additional data on a trace.
#[metastructure(pii = "maybe", skip_serialization = "empty")]
pub data: Annotated<Data>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
}

/// The arbitrary data on the trace.
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Data {
/// The current route in the application.
///
/// Set by React Native SDK.
#[metastructure(pii = "maybe", skip_serialization = "empty")]
pub route: Annotated<Route>,
/// The previous route in the application
///
/// Set by React Native SDK.
#[metastructure(field = "previousRoute", pii = "maybe", skip_serialization = "empty")]
pub previous_route: Annotated<Route>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(
additional_properties,
retain = "true",
pii = "maybe",
skip_serialization = "empty"
)]
pub other: Object<Value>,
}

/// The route in the application, set by React Native SDK.
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Route {
/// Parameters assigned to this route.
#[metastructure(pii = "true", skip_serialization = "empty", bag_size = "medium")]
params: Annotated<Object<Value>>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(
additional_properties,
retain = "true",
pii = "maybe",
skip_serialization = "empty"
)]
pub other: Object<Value>,
}

impl super::DefaultContext for TraceContext {
fn default_key() -> &'static str {
"trace"
Expand Down Expand Up @@ -170,6 +217,14 @@ mod tests {
"exclusive_time": 0.0,
"client_sample_rate": 0.5,
"origin": "auto.http",
"data": {
"route": {
"params": {
"tok": "test"
},
"path": "/path"
}
},
"other": "value",
"type": "trace"
}"#;
Expand All @@ -182,6 +237,27 @@ mod tests {
exclusive_time: Annotated::new(0.0),
client_sample_rate: Annotated::new(0.5),
origin: Annotated::new("auto.http".to_owned()),
data: Annotated::new(Data {
route: Annotated::new(Route {
params: Annotated::new({
let mut map = Object::new();
map.insert(
"tok".to_string(),
Annotated::new(Value::String("test".into())),
);
map
}),
other: {
let mut map = Object::new();
map.insert(
"path".to_string(),
Annotated::new(Value::String("/path".into())),
);
map
},
}),
..Default::default()
}),
other: {
let mut map = Object::new();
map.insert(
Expand Down
39 changes: 38 additions & 1 deletion relay-pii/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ mod tests {
use relay_event_schema::processor::process_value;
use relay_event_schema::protocol::{
Addr, Breadcrumb, DebugImage, DebugMeta, Event, ExtraValue, Headers, LogEntry,
NativeDebugImage, Request, Span, TagEntry, Tags,
NativeDebugImage, Request, Span, TagEntry, Tags, TraceContext,
};
use relay_protocol::{assert_annotated_snapshot, Annotated, FromValue, Object, Value};

Expand Down Expand Up @@ -1163,6 +1163,43 @@ mod tests {
assert_eq!(chunks, res);
}

#[test]
fn test_trace_route_params_scrubbed() {
let mut trace_context: Annotated<TraceContext> = Annotated::from_json(
r#"
{
"type": "trace",
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
"span_id": "fa90fdead5f74052",
"data": {
"previousRoute": {
"params": {
"password": "test"
}
}
}
}
"#,
)
.unwrap();

let ds_config = DataScrubbingConfig {
scrub_data: true,
scrub_defaults: true,
..Default::default()
};
let pii_config = ds_config.pii_config().unwrap().as_ref().unwrap();
let mut pii_processor = PiiProcessor::new(pii_config.compiled());

process_value(
&mut trace_context,
&mut pii_processor,
ProcessingState::root(),
)
.unwrap();
assert_annotated_snapshot!(trace_context);
}

#[test]
fn test_scrub_span_data_http_not_scrubbed() {
let mut span: Annotated<Span> = Annotated::from_json(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: relay-pii/src/processor.rs
expression: trace_context
---
{
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
"span_id": "fa90fdead5f74052",
"data": {
"previousRoute": {
"params": {
"password": "[Filtered]"
}
}
},
"type": "trace",
"_meta": {
"data": {
"previousRoute": {
"params": {
"password": {
"": {
"rem": [
[
"@password:filter",
"s",
0,
10
]
],
"len": 4
}
}
}
}
}
}
}
67 changes: 67 additions & 0 deletions relay-server/tests/snapshots/test_fixtures__event_schema.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,41 @@ expression: "relay_event_schema::protocol::event_json_schema()"
}
]
},
"Data": {
"description": " The arbitrary data on the trace.",
"anyOf": [
{
"type": "object",
"properties": {
"previousRoute": {
"description": " The previous route in the application\n\n Set by React Native SDK.",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/Route"
},
{
"type": "null"
}
]
},
"route": {
"description": " The current route in the application.\n\n Set by React Native SDK.",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/Route"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
}
]
},
"DebugId": {
"type": "string"
},
Expand Down Expand Up @@ -3214,6 +3249,26 @@ expression: "relay_event_schema::protocol::event_json_schema()"
}
]
},
"Route": {
"description": " The route in the application, set by React Native SDK.",
"anyOf": [
{
"type": "object",
"properties": {
"params": {
"description": " Parameters assigned to this route.",
"default": null,
"type": [
"object",
"null"
],
"additionalProperties": true
}
},
"additionalProperties": false
}
]
},
"RuntimeContext": {
"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).",
"anyOf": [
Expand Down Expand Up @@ -3579,6 +3634,18 @@ expression: "relay_event_schema::protocol::event_json_schema()"
],
"format": "double"
},
"data": {
"description": " Arbitrary additional data on a trace.",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/Data"
},
{
"type": "null"
}
]
},
"exclusive_time": {
"description": " The amount of time in milliseconds spent in this transaction span,\n excluding its immediate child spans.",
"default": null,
Expand Down

0 comments on commit 66c2f82

Please sign in to comment.