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): Update to new standard Profile format #504

Merged
merged 9 commits into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions sentry-core/Cargo.toml
Expand Up @@ -26,7 +26,7 @@ client = ["rand"]
# and macros actually expand features (and extern crate) where they are used!
debug-logs = ["log_"]
test = ["client"]
profiling = ["pprof", "build_id", "uuid", "sys-info", "findshlibs", "rustc_version_runtime", "libc"]
profiling = ["pprof", "build_id", "uuid", "sys-info", "findshlibs", "rustc_version_runtime", "libc", "indexmap"]
frame-pointer = ["pprof?/frame-pointer"]

[dependencies]
Expand All @@ -41,7 +41,7 @@ sys-info = { version = "0.9.1", optional = true }
build_id = { version = "0.2.1", optional = true }
findshlibs = { version = "=0.10.2", optional = true }
rustc_version_runtime = { version = "0.2.1", optional = true }
chrono = { version = "0.4.22", features = ["serde"], optional = true}
indexmap = { version = "1.9.1", optional = true}

[target.'cfg(target_family = "unix")'.dependencies]
pprof = { version = "0.10.1", optional = true, default-features = false}
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/performance.rs
Expand Up @@ -307,7 +307,7 @@ impl Transaction {
Some(protocol::Transaction {
name: Some(ctx.name),
#[cfg(all(feature = "profiling", target_family = "unix"))]
active_thread_id: Some(unsafe { libc::pthread_self().try_into().unwrap_or(0) }),
active_thread_id: Some(unsafe { libc::pthread_self() as u64 }),
..Default::default()
}),
),
Expand Down
35 changes: 18 additions & 17 deletions sentry-core/src/profiling.rs
@@ -1,8 +1,8 @@
use indexmap::set::IndexSet;
use std::collections::HashMap;
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};

use chrono::DateTime;
use findshlibs::{SharedLibrary, SharedLibraryId, TargetSharedLibrary, TARGET_SUPPORTED};

use sentry_types::protocol::v7::Profile;
Expand Down Expand Up @@ -175,8 +175,7 @@ fn get_profile_from_report(

let mut samples: Vec<Sample> = Vec::with_capacity(rep.data.len());
let mut stacks: Vec<Vec<u32>> = Vec::with_capacity(rep.data.len());
let mut frames: Vec<RustFrame> = Vec::new();
let mut address_to_frame_id: HashMap<String, u32> = HashMap::new();
let mut address_to_frame_idx: IndexSet<RustFrame> = IndexSet::new();
let mut thread_metadata: HashMap<String, ThreadMetadata> = HashMap::new();

for sample in rep.data.keys() {
Expand All @@ -188,12 +187,15 @@ fn get_profile_from_report(
let instruction_addr = format!("{:p}", frame.ip as *mut core::ffi::c_void);
#[cfg(not(feature = "frame-pointer"))]
let instruction_addr = format!("{:p}", frame.ip());
*address_to_frame_id
.entry(instruction_addr.clone())
.or_insert_with(|| {
frames.push(RustFrame { instruction_addr });
(frames.len() - 1) as u32
})
let rust_frame = RustFrame { instruction_addr };

address_to_frame_idx
.get_index_of(&rust_frame)
.unwrap_or_else(|| -> usize {
address_to_frame_idx.insert(rust_frame);

address_to_frame_idx.len() - 1
}) as u32
viglia marked this conversation as resolved.
Show resolved Hide resolved
})
.collect();

Expand Down Expand Up @@ -241,13 +243,12 @@ fn get_profile_from_report(
_ => "".to_string(),
},

event_id: uuid::Uuid::new_v4(), // double check this
release: format!(
"{} ({})",
env!("CARGO_PKG_VERSION"),
build_id::get().to_simple(),
),
timestamp: DateTime::from(rep.timing.start_time),
event_id: uuid::Uuid::new_v4(),
release: transaction
.release
.as_ref()
.map_or("".to_string(), |r| -> String { r.to_string() }),
viglia marked this conversation as resolved.
Show resolved Hide resolved
timestamp: rep.timing.start_time,
transactions: vec![TransactionMetadata {
id: transaction.event_id,
name: transaction.name.clone().unwrap_or_else(|| "".to_string()),
Expand All @@ -265,7 +266,7 @@ fn get_profile_from_report(
profile: Profile {
samples,
stacks,
frames,
frames: address_to_frame_idx.into_iter().collect(),
thread_metadata,
},
}
Expand Down
2 changes: 1 addition & 1 deletion sentry-types/src/lib.rs
Expand Up @@ -43,7 +43,7 @@ mod auth;
mod dsn;
mod project_id;
pub mod protocol;
mod utils;
pub(crate) mod utils;

pub use crate::auth::*;
pub use crate::dsn::*;
Expand Down
15 changes: 8 additions & 7 deletions sentry-types/src/protocol/profile.rs
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, time::SystemTime};

use super::v7::{DebugMeta, TraceId};
use crate::utils::ts_rfc3339;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize, Serializer};
use uuid::Uuid;

Expand All @@ -19,16 +19,16 @@ pub struct TransactionMetadata {
pub name: String,
/// Trace ID
pub trace_id: TraceId,
/// Transaction start timestamp in nanoseconds
/// Transaction start timestamp in nanoseconds relative to the start of the profiler
pub relative_start_ns: u64,
/// Transaction end timestamp in nanoseconds
/// Transaction end timestamp in nanoseconds relative to the start of the profiler
pub relative_end_ns: u64,
/// ID of the thread in which the transaction started
#[serde(default)]
pub active_thread_id: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
/// Single frame of a Sample
pub struct RustFrame {
/// Instruction address
Expand All @@ -42,7 +42,7 @@ pub struct Sample {
pub stack_id: u32,
/// Thread ID
pub thread_id: u64,
/// Timestamp at which this sample was collected
/// Timestamp at which this sample was collected relative to the start of the profiler
pub relative_timestamp_ns: u64,
}

Expand Down Expand Up @@ -134,8 +134,9 @@ pub struct SampleProfile {
pub profile: Profile,
/// Release
pub release: String,
#[serde(with = "ts_rfc3339")]
/// Timestamp at which the profiler started
pub timestamp: DateTime<Utc>,
pub timestamp: SystemTime,

#[serde(default, skip_serializing_if = "Vec::is_empty")]
/// List of transactions associated with this profile
Expand Down
5 changes: 0 additions & 5 deletions sentry-types/src/protocol/v7.rs
Expand Up @@ -1894,11 +1894,6 @@ impl fmt::Display for SpanStatus {
}
}

#[cfg(all(feature = "profiling", target_family = "unix"))]
fn is_zero(number: u64) -> bool {
number == 0
}

/// Represents a tracing transaction.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Transaction<'a> {
Expand Down