Skip to content

Commit

Permalink
Parse all fields in the profile to keep them for later
Browse files Browse the repository at this point in the history
  • Loading branch information
phacops committed Mar 8, 2022
1 parent ad82913 commit 9b92ae0
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions relay-server/src/utils/profile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bytes::Bytes;
use failure::Fail;

use android_trace_log::AndroidTraceLog;
Expand All @@ -10,36 +9,66 @@ pub const ANDROID_SDK_NAME: &str = "sentry.java.android";

#[derive(Serialize, Deserialize)]
struct AndroidProfile {
stacktrace: Bytes,
android_trace: AndroidTraceLog,
android_api_level: u16,
build_id: String,
device_locale: String,
device_manufacturer: String,
device_model: String,
device_os_name: String,
device_os_version: String,
environment: String,
error_code: String,
error_description: String,
platform: String,
stacktrace: String,
stacktrace_id: String,
trace_id: String,
transaction_id: String,
transaction_name: String,
version_code: String,
version_name: String,

android_trace: Option<AndroidTraceLog>,
}

impl AndroidProfile {
fn parse_stack_trace(&mut self) -> Result<(), ProfileError> {
let stacktrace_bytes = match base64::decode(&self.stacktrace) {
Ok(stacktrace) => stacktrace,
Err(_) => return Err(ProfileError::InvalidBase64Value),
};
self.android_trace = match android_trace_log::parse(&stacktrace_bytes) {
Ok(trace) => Some(trace),
Err(_) => return Err(ProfileError::InvalidStackTrace),
};
Ok(())
}
}

#[derive(Debug, Fail)]
pub enum ProfileError {
#[fail(display = "invalid json in profile")]
InvalidJson(#[cause] serde_json::Error),
#[fail(display = "invalid base64 value")]
InvalidBase64Value,
#[fail(display = "invalid stack trace in profile")]
InvalidStackTrace,
#[fail(display = "cannot serialize payload")]
CannotSerializePayload,
}

pub fn expand_profile_envelope(item: &mut Item) -> Result<(), ProfileError> {
let payload = item.payload();
let profile: AndroidProfile = match serde_json::from_slice(&payload) {
let mut profile: AndroidProfile = match serde_json::from_slice(&item.payload()) {
Ok(profile) => profile,
Err(err) => return Err(ProfileError::InvalidJson(err)),
};

let trace = match android_trace_log::parse(&profile.stacktrace) {
Ok(trace) => trace,
Err(_) => return Err(ProfileError::InvalidStackTrace),
match profile.parse_stack_trace() {
Ok(_) => (),
Err(err) => return Err(err),
};

match serde_json::to_vec(&AndroidProfile {
stacktrace: profile.stacktrace,
android_trace: trace,
}) {
match serde_json::to_vec(&profile) {
Ok(payload) => item.set_payload(ContentType::Json, &payload[..]),
Err(_) => return Err(ProfileError::CannotSerializePayload),
};
Expand Down

0 comments on commit 9b92ae0

Please sign in to comment.