Skip to content

feat(replay): combined envelope items #2170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions relay-dynamic-config/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum Feature {
SessionReplay,
/// Enables data scrubbing of replay recording payloads.
SessionReplayRecordingScrubbing,
/// Enables combining session replay envelope item (Replay Recordings and Replay Events).
/// into one item.
SessionReplayCombinedEnvelopeItems,
/// Enables device.class synthesis
///
/// Enables device.class tag synthesis on mobile events.
Expand All @@ -27,6 +30,9 @@ impl<'de> Deserialize<'de> for Feature {
let feature_name = Cow::<str>::deserialize(deserializer)?;
Ok(match feature_name.as_ref() {
"organizations:session-replay" => Feature::SessionReplay,
"organizations:session-replay-combined-envelope-items" => {
Feature::SessionReplayCombinedEnvelopeItems
}
"organizations:session-replay-recording-scrubbing" => {
Feature::SessionReplayRecordingScrubbing
}
Expand All @@ -47,6 +53,9 @@ impl Serialize for Feature {
Feature::SessionReplayRecordingScrubbing => {
"organizations:session-replay-recording-scrubbing"
}
Feature::SessionReplayCombinedEnvelopeItems => {
"organizations:session-replay-combined-envelope-items"
}
Feature::DeviceClassSynthesis => "organizations:device-class-synthesis",
Feature::SpanMetricsExtraction => "projects:span-metrics-extraction",
Feature::Unknown(s) => s,
Expand Down
1 change: 0 additions & 1 deletion relay-general/src/protocol/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ impl Replay {
} else {
&user_agent_info
};

let contexts = self.contexts.get_or_insert_with(|| Contexts::new());
user_agent::normalize_user_agent_info_generic(contexts, &self.platform, user_agent_info);
}
Expand Down
48 changes: 32 additions & 16 deletions relay-replays/src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,34 +379,50 @@ impl<'a> RecordingScrubber<'a> {
/// - Headers and the body are separated by exactly one UNIX newline (`\n`).
/// - The payload size exceeds the configured `limit` of the scrubber after decompression.
/// - On errors during decompression or JSON parsing.
pub fn process_recording(&mut self, bytes: &[u8]) -> Result<Vec<u8>, ParseRecordingError> {
pub fn process_recording(
&mut self,
bytes: &[u8],
) -> Result<(Vec<u8>, Vec<u8>), ParseRecordingError> {
// Check for null byte condition.
if bytes.is_empty() {
return Err(ParseRecordingError::Message("no data found"));
}

let mut split = bytes.splitn(2, |b| b == &b'\n');
let header = split
.next()
.ok_or(ParseRecordingError::Message("no headers found"))?;

let body = match split.next() {
Some(b"") | None => return Err(ParseRecordingError::Message("no body found")),
Some(body) => body,
};
let (headers, body) = split_headers_from_body(bytes.to_owned())?;

let mut output = header.to_owned();
output.push(b'\n');
// Data scrubbing usually does not change the size of the output by much. We can preallocate
// enough space for the scrubbed output to avoid resizing the output buffer serveral times.
// Benchmarks have NOT shown a big difference, however.
output.reserve(body.len());
self.transcode_replay(body, &mut output)?;
let mut output = Vec::with_capacity(body.len());
self.transcode_replay(&body, &mut output)?;

Ok(output)
Ok((headers, output))
}
}

/// Splits the headers bytes from the body bytes.
///
/// # Errors
///
/// This function requires a full recording payload including headers and body. This function
/// will return errors if:
/// - Headers or the body are missing.
/// - Headers and the body are not separated by exactly one UNIX newline (`\n`).
pub fn split_headers_from_body(bytes: Vec<u8>) -> Result<(Vec<u8>, Vec<u8>), ParseRecordingError> {
let mut split = bytes.splitn(2, |b| b == &b'\n');

let headers = split
.next()
.ok_or(ParseRecordingError::Message("no headers found"))?;

let body = match split.next() {
Some(b"") | None => return Err(ParseRecordingError::Message("no body found")),
Some(body) => body,
};

Ok((headers.to_vec(), body.to_vec()))
}

#[cfg(test)]
mod tests {
// End to end test coverage.
Expand Down Expand Up @@ -451,7 +467,7 @@ mod tests {

let config = default_pii_config();
let result = scrubber(&config).process_recording(payload);
assert!(!result.unwrap().is_empty());
assert!(!result.unwrap().0.is_empty());
}

#[test]
Expand Down
Loading