Skip to content
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

feat(profiling): Extract client sdk for profiles #3915

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 25 additions & 9 deletions relay-profiling/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use data_encoding::BASE64_NOPAD;
use relay_event_schema::protocol::{EventId, SpanId};
use serde::{Deserialize, Serialize};

use crate::client_sdk::ClientSdk;
use crate::measurements::Measurement;
use crate::native_debug_image::NativeDebugImage;
use crate::sample::v1::SampleProfile;
Expand Down Expand Up @@ -91,6 +92,9 @@ pub struct ProfileMetadata {

#[serde(skip_serializing_if = "Option::is_none")]
debug_meta: Option<DebugMeta>,

#[serde(skip_serializing_if = "Option::is_none")]
client_sdk: Option<ClientSdk>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -203,11 +207,14 @@ fn parse_profile(payload: &[u8]) -> Result<AndroidProfilingEvent, ProfileError>

pub fn parse_android_profile(
payload: &[u8],
client_sdk: Option<ClientSdk>,
transaction_metadata: BTreeMap<String, String>,
transaction_tags: BTreeMap<String, String>,
) -> Result<Vec<u8>, ProfileError> {
let mut profile = parse_profile(payload)?;

profile.metadata.client_sdk = client_sdk;
Zylphrex marked this conversation as resolved.
Show resolved Hide resolved

if let Some(transaction_name) = transaction_metadata.get("transaction") {
transaction_name.clone_into(&mut profile.metadata.transaction_name);

Expand Down Expand Up @@ -253,9 +260,13 @@ mod tests {
let profile = parse_profile(payload);
assert!(profile.is_ok());
let data = serde_json::to_vec(&profile.unwrap());
assert!(
parse_android_profile(&(data.unwrap())[..], BTreeMap::new(), BTreeMap::new()).is_ok()
);
assert!(parse_android_profile(
&(data.unwrap())[..],
None,
BTreeMap::new(),
BTreeMap::new()
)
.is_ok());
}

#[test]
Expand All @@ -264,22 +275,26 @@ mod tests {
let profile = parse_profile(payload);
assert!(profile.is_ok());
let data = serde_json::to_vec(&profile.unwrap());
assert!(
parse_android_profile(&(data.unwrap())[..], BTreeMap::new(), BTreeMap::new()).is_ok()
);
assert!(parse_android_profile(
&(data.unwrap())[..],
None,
BTreeMap::new(),
BTreeMap::new()
)
.is_ok());
}

#[test]
fn test_no_transaction() {
let payload = include_bytes!("../tests/fixtures/android/no_transaction.json");
let data = parse_android_profile(payload, BTreeMap::new(), BTreeMap::new());
let data = parse_android_profile(payload, None, BTreeMap::new(), BTreeMap::new());
assert!(data.is_err());
}

#[test]
fn test_remove_invalid_events() {
let payload = include_bytes!("../tests/fixtures/android/remove_invalid_events.json");
let data = parse_android_profile(payload, BTreeMap::new(), BTreeMap::new());
let data = parse_android_profile(payload, None, BTreeMap::new(), BTreeMap::new());
assert!(data.is_err());
}

Expand Down Expand Up @@ -315,7 +330,8 @@ mod tests {
]);

let payload = include_bytes!("../tests/fixtures/android/valid.json");
let profile_json = parse_android_profile(payload, transaction_metadata, BTreeMap::new());
let profile_json =
parse_android_profile(payload, None, transaction_metadata, BTreeMap::new());
assert!(profile_json.is_ok());

let payload = profile_json.unwrap();
Expand Down
7 changes: 7 additions & 0 deletions relay-profiling/src/client_sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ClientSdk {
Zylphrex marked this conversation as resolved.
Show resolved Hide resolved
Zylphrex marked this conversation as resolved.
Show resolved Hide resolved
pub name: String,
pub version: String,
}
54 changes: 54 additions & 0 deletions relay-profiling/src/extract_from_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use chrono::SecondsFormat;

use relay_event_schema::protocol::{AppContext, AsPair, Event, SpanStatus, TraceContext};

use crate::client_sdk::ClientSdk;

pub fn extract_transaction_metadata(event: &Event) -> BTreeMap<String, String> {
let mut tags = BTreeMap::new();

Expand Down Expand Up @@ -98,6 +100,19 @@ fn extract_http_method(transaction: &Event) -> Option<String> {
Some(method.clone())
}

pub fn extract_sdk_metadata(transaction: &Event) -> Option<ClientSdk> {
match transaction.client_sdk.value() {
Some(client_sdk) => match (client_sdk.name.value(), client_sdk.version.value()) {
(Some(name), Some(version)) => Some(ClientSdk {
name: name.to_owned(),
version: version.to_owned(),
}),
_ => None,
},
None => None,
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -145,4 +160,43 @@ mod tests {
}
"#);
}

#[test]
fn test_extract_sdk_metadata() {
let event = Event::from_value(
serde_json::json!({
"release": "myrelease",
"dist": "mydist",
"environment": "myenvironment",
"transaction": "mytransaction",
"contexts": {
"app": {
"app_identifier": "io.sentry.myexample",
},
"trace": {
"status": "ok",
"op": "myop",
},
},
"request": {
"method": "GET",
},
"timestamp": "2011-05-02T17:41:36Z",
"start_timestamp": "2011-05-02T17:40:36Z",
"sdk": {
"name": "sentry.python",
"version": "2.10.7",
},
})
.into(),
);
let sdk = extract_sdk_metadata(&event.0.unwrap());
assert_eq!(
sdk,
Some(ClientSdk {
name: "sentry.python".to_string(),
version: "2.10.7".to_string(),
})
);
}
}
24 changes: 17 additions & 7 deletions relay-profiling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ use relay_event_schema::protocol::{Event, EventId};
use serde::Deserialize;
use serde_json::Deserializer;

use crate::extract_from_transaction::{extract_transaction_metadata, extract_transaction_tags};
use crate::extract_from_transaction::{
extract_sdk_metadata, extract_transaction_metadata, extract_transaction_tags,
};

pub use crate::error::ProfileError;
pub use crate::outcomes::discard_reason;

mod android;
mod client_sdk;
mod error;
mod extract_from_transaction;
mod measurements;
Expand Down Expand Up @@ -154,16 +157,23 @@ pub fn expand_profile(payload: &[u8], event: &Event) -> Result<(ProfileId, Vec<u
return Err(ProfileError::InvalidJson(err));
}
};
let client_sdk = extract_sdk_metadata(event);
let transaction_metadata = extract_transaction_metadata(event);
let transaction_tags = extract_transaction_tags(event);
let processed_payload = match profile.version {
sample::Version::V1 => {
sample::v1::parse_sample_profile(payload, transaction_metadata, transaction_tags)
}
sample::Version::V1 => sample::v1::parse_sample_profile(
payload,
client_sdk,
transaction_metadata,
transaction_tags,
),
_ => match profile.platform.as_str() {
"android" => {
android::parse_android_profile(payload, transaction_metadata, transaction_tags)
}
"android" => android::parse_android_profile(
payload,
client_sdk,
transaction_metadata,
transaction_tags,
),
_ => return Err(ProfileError::PlatformNotSupported),
},
};
Expand Down
15 changes: 12 additions & 3 deletions relay-profiling/src/sample/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use itertools::Itertools;
use relay_event_schema::protocol::{EventId, SpanId};
use serde::{Deserialize, Serialize};

use crate::client_sdk::ClientSdk;
use crate::error::ProfileError;
use crate::measurements::Measurement;
use crate::sample::{DebugMeta, Frame, ThreadMetadata, Version};
Expand Down Expand Up @@ -255,6 +256,9 @@ pub struct ProfileMetadata {

#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
transaction_tags: BTreeMap<String, String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub client_sdk: Option<ClientSdk>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -322,11 +326,14 @@ fn parse_profile(payload: &[u8]) -> Result<ProfilingEvent, ProfileError> {

pub fn parse_sample_profile(
payload: &[u8],
client_sdk: Option<ClientSdk>,
transaction_metadata: BTreeMap<String, String>,
transaction_tags: BTreeMap<String, String>,
) -> Result<Vec<u8>, ProfileError> {
let mut profile = parse_profile(payload)?;

profile.metadata.client_sdk = client_sdk;

if let Some(transaction_name) = transaction_metadata.get("transaction") {
if let Some(ref mut transaction) = profile.metadata.transaction {
transaction_name.clone_into(&mut transaction.name)
Expand Down Expand Up @@ -377,7 +384,7 @@ mod tests {
#[test]
fn test_expand() {
let payload = include_bytes!("../../tests/fixtures/sample/v1/valid.json");
let profile = parse_sample_profile(payload, BTreeMap::new(), BTreeMap::new());
let profile = parse_sample_profile(payload, None, BTreeMap::new(), BTreeMap::new());
assert!(profile.is_ok());
}

Expand Down Expand Up @@ -410,6 +417,7 @@ mod tests {
dist: "9999".to_string(),
transaction_metadata: BTreeMap::new(),
transaction_tags: BTreeMap::new(),
client_sdk: None,
},
profile: SampleProfile {
queue_metadata: Some(HashMap::new()),
Expand Down Expand Up @@ -593,7 +601,7 @@ mod tests {
]);

let payload = serde_json::to_vec(&profile).unwrap();
let data = parse_sample_profile(&payload[..], BTreeMap::new(), BTreeMap::new());
let data = parse_sample_profile(&payload[..], None, BTreeMap::new(), BTreeMap::new());

assert!(data.is_err());
}
Expand Down Expand Up @@ -873,7 +881,8 @@ mod tests {
)]);

let payload = include_bytes!("../../tests/fixtures/sample/v1/valid.json");
let profile_json = parse_sample_profile(payload, transaction_metadata, BTreeMap::new());
let profile_json =
parse_sample_profile(payload, None, transaction_metadata, BTreeMap::new());
assert!(profile_json.is_ok());

let payload = profile_json.unwrap();
Expand Down
7 changes: 1 addition & 6 deletions relay-profiling/src/sample/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize};
use relay_event_schema::protocol::EventId;
use relay_metrics::FiniteF64;

use crate::client_sdk::ClientSdk;
use crate::error::ProfileError;
use crate::measurements::Measurement;
use crate::sample::{DebugMeta, Frame, ThreadMetadata, Version};
Expand All @@ -42,12 +43,6 @@ pub struct ProfileMetadata {
pub version: Version,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ClientSdk {
name: String,
version: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Sample {
/// Unix timestamp in seconds with millisecond precision when the sample
Expand Down
Loading