From d645d4da78ae90c5378650ed8e83758d9de1e7d9 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 12:42:52 -0400 Subject: [PATCH 01/19] [crashtracker] Implement structured log format from RFC --- crashtracker/src/crash_info/mod.rs | 1 + crashtracker/src/crash_info/stacktrace.rs | 30 +++----- .../crash_info/structured_log_format/mod.rs | 74 +++++++++++++++++++ .../structured_log_format/stacktrace.rs | 51 +++++++++++++ 4 files changed, 136 insertions(+), 20 deletions(-) create mode 100644 crashtracker/src/crash_info/structured_log_format/mod.rs create mode 100644 crashtracker/src/crash_info/structured_log_format/stacktrace.rs diff --git a/crashtracker/src/crash_info/mod.rs b/crashtracker/src/crash_info/mod.rs index da9305f2cb..18d061dc68 100644 --- a/crashtracker/src/crash_info/mod.rs +++ b/crashtracker/src/crash_info/mod.rs @@ -6,6 +6,7 @@ use ddcommon::Endpoint; pub use metadata::*; mod stacktrace; pub use stacktrace::*; +mod structured; mod telemetry; use self::telemetry::TelemetryCrashUploader; diff --git a/crashtracker/src/crash_info/stacktrace.rs b/crashtracker/src/crash_info/stacktrace.rs index 1d1ca22ceb..de90321a45 100644 --- a/crashtracker/src/crash_info/stacktrace.rs +++ b/crashtracker/src/crash_info/stacktrace.rs @@ -6,40 +6,30 @@ use std::path::PathBuf; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct StackFrameNames { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub colno: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub filename: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub lineno: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub name: Option, } /// All fields are hex encoded integers. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StackFrame { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub ip: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub module_base_address: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub names: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub normalized_ip: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub sp: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub symbol_address: Option, } diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs new file mode 100644 index 0000000000..755969080c --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -0,0 +1,74 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +mod stacktrace; +pub use stacktrace::*; + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use uuid::Uuid; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum ErrorKind { + SigBus, + SigSegv, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum StackType { + CrashTrackerV1, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Metadata { + pub library_name: String, + pub library_version: String, + pub family: String, + // Should include "service", "environment", etc + pub tags: Vec, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct StackFrame {} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ErrorData { + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub additional_stacks: HashMap>, + pub is_crash: bool, + pub kind: ErrorKind, + pub message: String, + pub stack: Vec, + pub stack_type: StackType, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ProcessInfo { + pub pid: u32, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct StructuredCrashInfo { + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub additional_stacktraces: HashMap>, + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub counters: HashMap, + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub files: HashMap>, + pub incomplete: bool, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub metadata: Option, + pub os_info: os_info::Info, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub proc_info: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub span_ids: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub stacktrace: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub trace_ids: Vec, + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub timestamp: Option>, + pub uuid: Uuid, +} diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs new file mode 100644 index 0000000000..8c90e60d6c --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -0,0 +1,51 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct StackFrameNames { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub colno: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub filename: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub lineno: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +/// All fields are hex encoded integers. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct StackFrame { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ip: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub module_base_address: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub names: Option>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub normalized_ip: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sp: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub symbol_address: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum NormalizedAddressMeta { + Apk(PathBuf), + Elf { + path: PathBuf, + build_id: Option>, + }, + Unknown, + Unexpected(String), +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct NormalizedAddress { + pub file_offset: u64, + pub meta: NormalizedAddressMeta, +} From 66e6e8c66c949965fbaa27ddab3f5cfc70a0b999 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 14:04:30 -0400 Subject: [PATCH 02/19] split into internal and structured --- .../src/crash_info/{ => internal}/metadata.rs | 0 crashtracker/src/crash_info/internal/mod.rs | 271 ++++++++++++++++++ .../crash_info/{ => internal}/stacktrace.rs | 0 .../crash_info/{ => internal}/telemetry.rs | 0 crashtracker/src/crash_info/mod.rs | 271 +----------------- .../crash_info/structured_log_format/mod.rs | 4 +- 6 files changed, 276 insertions(+), 270 deletions(-) rename crashtracker/src/crash_info/{ => internal}/metadata.rs (100%) create mode 100644 crashtracker/src/crash_info/internal/mod.rs rename crashtracker/src/crash_info/{ => internal}/stacktrace.rs (100%) rename crashtracker/src/crash_info/{ => internal}/telemetry.rs (100%) diff --git a/crashtracker/src/crash_info/metadata.rs b/crashtracker/src/crash_info/internal/metadata.rs similarity index 100% rename from crashtracker/src/crash_info/metadata.rs rename to crashtracker/src/crash_info/internal/metadata.rs diff --git a/crashtracker/src/crash_info/internal/mod.rs b/crashtracker/src/crash_info/internal/mod.rs new file mode 100644 index 0000000000..da9305f2cb --- /dev/null +++ b/crashtracker/src/crash_info/internal/mod.rs @@ -0,0 +1,271 @@ +// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +mod metadata; +use ddcommon::Endpoint; +pub use metadata::*; +mod stacktrace; +pub use stacktrace::*; +mod telemetry; + +use self::telemetry::TelemetryCrashUploader; +use anyhow::Context; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::io::BufRead; +use std::path::Path; +use std::{collections::HashMap, fs::File, io::BufReader}; +use uuid::Uuid; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct SigInfo { + pub signum: u64, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub signame: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ProcessInfo { + pub pid: u32, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct CrashInfo { + #[serde(skip_serializing_if = "HashMap::is_empty")] + #[serde(default)] + pub additional_stacktraces: HashMap>, + #[serde(skip_serializing_if = "HashMap::is_empty")] + #[serde(default)] + pub counters: HashMap, + #[serde(skip_serializing_if = "HashMap::is_empty")] + #[serde(default)] + pub files: HashMap>, + pub incomplete: bool, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub metadata: Option, + pub os_info: os_info::Info, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub proc_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub siginfo: Option, + #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default)] + pub span_ids: Vec, + #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default)] + pub stacktrace: Vec, + #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default)] + pub trace_ids: Vec, + /// Any additional data goes here + #[serde(skip_serializing_if = "HashMap::is_empty")] + #[serde(default)] + pub tags: HashMap, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub timestamp: Option>, + pub uuid: Uuid, +} + +/// Getters and predicates +impl CrashInfo { + pub fn crash_seen(&self) -> bool { + self.siginfo.is_some() + } +} + +impl Default for CrashInfo { + fn default() -> Self { + Self::new() + } +} + +#[cfg(unix)] +impl CrashInfo { + pub fn normalize_ips(&mut self, pid: u32) -> anyhow::Result<()> { + let normalizer = blazesym::normalize::Normalizer::new(); + let pid = pid.into(); + self.stacktrace.iter_mut().for_each(|frame| { + frame + .normalize_ip(&normalizer, pid) + .unwrap_or_else(|err| eprintln!("Error resolving name {err}")) + }); + Ok(()) + } + + pub fn resolve_names(&mut self, src: &blazesym::symbolize::Source) -> anyhow::Result<()> { + let symbolizer = blazesym::symbolize::Symbolizer::new(); + for frame in &mut self.stacktrace { + // Resolving names is best effort, just print the error and continue + frame + .resolve_names(src, &symbolizer) + .unwrap_or_else(|err| eprintln!("Error resolving name {err}")); + } + Ok(()) + } + + pub fn resolve_names_from_process(&mut self, pid: u32) -> anyhow::Result<()> { + let mut process = blazesym::symbolize::Process::new(pid.into()); + // https://github.com/libbpf/blazesym/issues/518 + process.map_files = false; + let src = blazesym::symbolize::Source::Process(process); + self.resolve_names(&src) + } +} + +/// Constructor and setters +impl CrashInfo { + pub fn new() -> Self { + let os_info = os_info::get(); + let uuid = Uuid::new_v4(); + Self { + additional_stacktraces: HashMap::new(), + counters: HashMap::new(), + files: HashMap::new(), + incomplete: false, + metadata: None, + os_info, + proc_info: None, + siginfo: None, + span_ids: vec![], + stacktrace: vec![], + tags: HashMap::new(), + timestamp: None, + trace_ids: vec![], + uuid, + } + } + + pub fn add_counter(&mut self, name: &str, val: i64) -> anyhow::Result<()> { + let old = self.counters.insert(name.to_string(), val); + anyhow::ensure!(old.is_none(), "Double insert of counter {name}"); + Ok(()) + } + + pub fn add_file(&mut self, filename: &str) -> anyhow::Result<()> { + let file = File::open(filename).with_context(|| filename.to_string())?; + let lines: std::io::Result> = BufReader::new(file).lines().collect(); + self.add_file_with_contents(filename, lines?)?; + Ok(()) + } + + pub fn add_file_with_contents( + &mut self, + filename: &str, + lines: Vec, + ) -> anyhow::Result<()> { + let old = self.files.insert(filename.to_string(), lines); + anyhow::ensure!( + old.is_none(), + "Attempted to add file that was already there {filename}" + ); + Ok(()) + } + + pub fn add_tag(&mut self, key: String, value: String) -> anyhow::Result<()> { + anyhow::ensure!( + !self.tags.contains_key(&key), + "Already had tag with key: {key}" + ); + self.tags.insert(key, value); + Ok(()) + } + + pub fn set_incomplete(&mut self, incomplete: bool) -> anyhow::Result<()> { + self.incomplete = incomplete; + Ok(()) + } + + pub fn set_metadata(&mut self, metadata: CrashtrackerMetadata) -> anyhow::Result<()> { + anyhow::ensure!(self.metadata.is_none()); + self.metadata = Some(metadata); + Ok(()) + } + + pub fn set_procinfo(&mut self, proc_info: ProcessInfo) -> anyhow::Result<()> { + anyhow::ensure!(self.proc_info.is_none()); + self.proc_info = Some(proc_info); + Ok(()) + } + + pub fn set_siginfo(&mut self, siginfo: SigInfo) -> anyhow::Result<()> { + anyhow::ensure!(self.siginfo.is_none()); + self.siginfo = Some(siginfo); + Ok(()) + } + pub fn set_span_ids(&mut self, ids: Vec) -> anyhow::Result<()> { + anyhow::ensure!(self.span_ids.is_empty()); + self.span_ids = ids; + Ok(()) + } + + pub fn set_stacktrace( + &mut self, + thread_id: Option, + stacktrace: Vec, + ) -> anyhow::Result<()> { + if let Some(thread_id) = thread_id { + anyhow::ensure!(!self.additional_stacktraces.contains_key(&thread_id)); + self.additional_stacktraces.insert(thread_id, stacktrace); + } else { + anyhow::ensure!(self.stacktrace.is_empty()); + self.stacktrace = stacktrace; + } + + Ok(()) + } + + pub fn set_timestamp(&mut self, ts: DateTime) -> anyhow::Result<()> { + anyhow::ensure!(self.timestamp.is_none()); + self.timestamp = Some(ts); + Ok(()) + } + + pub fn set_timestamp_to_now(&mut self) -> anyhow::Result<()> { + self.set_timestamp(Utc::now()) + } + + pub fn set_trace_ids(&mut self, ids: Vec) -> anyhow::Result<()> { + anyhow::ensure!(self.trace_ids.is_empty()); + self.trace_ids = ids; + Ok(()) + } +} + +impl CrashInfo { + /// Emit the CrashInfo as structured json in file `path`. + /// SIGNAL SAFETY: + /// I believe but have not verified this is signal safe. + pub fn to_file(&self, path: &Path) -> anyhow::Result<()> { + let file = + File::create(path).with_context(|| format!("Failed to create {}", path.display()))?; + serde_json::to_writer_pretty(file, self) + .with_context(|| format!("Failed to write json to {}", path.display()))?; + Ok(()) + } + + pub fn upload_to_endpoint(&self, endpoint: &Option) -> anyhow::Result<()> { + // If we're debugging to a file, dump the actual crashinfo into a json + if let Some(endpoint) = endpoint { + if Some("file") == endpoint.url.scheme_str() { + let path = ddcommon::decode_uri_path_in_authority(&endpoint.url) + .context("crash output file was not correctly formatted")?; + self.to_file(&path)?; + } + } + self.upload_to_telemetry(endpoint) + } + + fn upload_to_telemetry(&self, endpoint: &Option) -> anyhow::Result<()> { + if let Some(metadata) = &self.metadata { + if let Ok(uploader) = TelemetryCrashUploader::new(metadata, endpoint) { + uploader.upload_to_telemetry(self)?; + } + } + Ok(()) + } +} diff --git a/crashtracker/src/crash_info/stacktrace.rs b/crashtracker/src/crash_info/internal/stacktrace.rs similarity index 100% rename from crashtracker/src/crash_info/stacktrace.rs rename to crashtracker/src/crash_info/internal/stacktrace.rs diff --git a/crashtracker/src/crash_info/telemetry.rs b/crashtracker/src/crash_info/internal/telemetry.rs similarity index 100% rename from crashtracker/src/crash_info/telemetry.rs rename to crashtracker/src/crash_info/internal/telemetry.rs diff --git a/crashtracker/src/crash_info/mod.rs b/crashtracker/src/crash_info/mod.rs index 18d061dc68..5f503617c8 100644 --- a/crashtracker/src/crash_info/mod.rs +++ b/crashtracker/src/crash_info/mod.rs @@ -1,272 +1,7 @@ // Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -mod metadata; -use ddcommon::Endpoint; -pub use metadata::*; -mod stacktrace; -pub use stacktrace::*; -mod structured; -mod telemetry; +mod internal; +pub mod structured_log_format; -use self::telemetry::TelemetryCrashUploader; -use anyhow::Context; -use chrono::{DateTime, Utc}; -use serde::{Deserialize, Serialize}; -use std::io::BufRead; -use std::path::Path; -use std::{collections::HashMap, fs::File, io::BufReader}; -use uuid::Uuid; - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct SigInfo { - pub signum: u64, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] - pub signame: Option, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct ProcessInfo { - pub pid: u32, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct CrashInfo { - #[serde(skip_serializing_if = "HashMap::is_empty")] - #[serde(default)] - pub additional_stacktraces: HashMap>, - #[serde(skip_serializing_if = "HashMap::is_empty")] - #[serde(default)] - pub counters: HashMap, - #[serde(skip_serializing_if = "HashMap::is_empty")] - #[serde(default)] - pub files: HashMap>, - pub incomplete: bool, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] - pub metadata: Option, - pub os_info: os_info::Info, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] - pub proc_info: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] - pub siginfo: Option, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] - pub span_ids: Vec, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] - pub stacktrace: Vec, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] - pub trace_ids: Vec, - /// Any additional data goes here - #[serde(skip_serializing_if = "HashMap::is_empty")] - #[serde(default)] - pub tags: HashMap, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] - pub timestamp: Option>, - pub uuid: Uuid, -} - -/// Getters and predicates -impl CrashInfo { - pub fn crash_seen(&self) -> bool { - self.siginfo.is_some() - } -} - -impl Default for CrashInfo { - fn default() -> Self { - Self::new() - } -} - -#[cfg(unix)] -impl CrashInfo { - pub fn normalize_ips(&mut self, pid: u32) -> anyhow::Result<()> { - let normalizer = blazesym::normalize::Normalizer::new(); - let pid = pid.into(); - self.stacktrace.iter_mut().for_each(|frame| { - frame - .normalize_ip(&normalizer, pid) - .unwrap_or_else(|err| eprintln!("Error resolving name {err}")) - }); - Ok(()) - } - - pub fn resolve_names(&mut self, src: &blazesym::symbolize::Source) -> anyhow::Result<()> { - let symbolizer = blazesym::symbolize::Symbolizer::new(); - for frame in &mut self.stacktrace { - // Resolving names is best effort, just print the error and continue - frame - .resolve_names(src, &symbolizer) - .unwrap_or_else(|err| eprintln!("Error resolving name {err}")); - } - Ok(()) - } - - pub fn resolve_names_from_process(&mut self, pid: u32) -> anyhow::Result<()> { - let mut process = blazesym::symbolize::Process::new(pid.into()); - // https://github.com/libbpf/blazesym/issues/518 - process.map_files = false; - let src = blazesym::symbolize::Source::Process(process); - self.resolve_names(&src) - } -} - -/// Constructor and setters -impl CrashInfo { - pub fn new() -> Self { - let os_info = os_info::get(); - let uuid = Uuid::new_v4(); - Self { - additional_stacktraces: HashMap::new(), - counters: HashMap::new(), - files: HashMap::new(), - incomplete: false, - metadata: None, - os_info, - proc_info: None, - siginfo: None, - span_ids: vec![], - stacktrace: vec![], - tags: HashMap::new(), - timestamp: None, - trace_ids: vec![], - uuid, - } - } - - pub fn add_counter(&mut self, name: &str, val: i64) -> anyhow::Result<()> { - let old = self.counters.insert(name.to_string(), val); - anyhow::ensure!(old.is_none(), "Double insert of counter {name}"); - Ok(()) - } - - pub fn add_file(&mut self, filename: &str) -> anyhow::Result<()> { - let file = File::open(filename).with_context(|| filename.to_string())?; - let lines: std::io::Result> = BufReader::new(file).lines().collect(); - self.add_file_with_contents(filename, lines?)?; - Ok(()) - } - - pub fn add_file_with_contents( - &mut self, - filename: &str, - lines: Vec, - ) -> anyhow::Result<()> { - let old = self.files.insert(filename.to_string(), lines); - anyhow::ensure!( - old.is_none(), - "Attempted to add file that was already there {filename}" - ); - Ok(()) - } - - pub fn add_tag(&mut self, key: String, value: String) -> anyhow::Result<()> { - anyhow::ensure!( - !self.tags.contains_key(&key), - "Already had tag with key: {key}" - ); - self.tags.insert(key, value); - Ok(()) - } - - pub fn set_incomplete(&mut self, incomplete: bool) -> anyhow::Result<()> { - self.incomplete = incomplete; - Ok(()) - } - - pub fn set_metadata(&mut self, metadata: CrashtrackerMetadata) -> anyhow::Result<()> { - anyhow::ensure!(self.metadata.is_none()); - self.metadata = Some(metadata); - Ok(()) - } - - pub fn set_procinfo(&mut self, proc_info: ProcessInfo) -> anyhow::Result<()> { - anyhow::ensure!(self.proc_info.is_none()); - self.proc_info = Some(proc_info); - Ok(()) - } - - pub fn set_siginfo(&mut self, siginfo: SigInfo) -> anyhow::Result<()> { - anyhow::ensure!(self.siginfo.is_none()); - self.siginfo = Some(siginfo); - Ok(()) - } - pub fn set_span_ids(&mut self, ids: Vec) -> anyhow::Result<()> { - anyhow::ensure!(self.span_ids.is_empty()); - self.span_ids = ids; - Ok(()) - } - - pub fn set_stacktrace( - &mut self, - thread_id: Option, - stacktrace: Vec, - ) -> anyhow::Result<()> { - if let Some(thread_id) = thread_id { - anyhow::ensure!(!self.additional_stacktraces.contains_key(&thread_id)); - self.additional_stacktraces.insert(thread_id, stacktrace); - } else { - anyhow::ensure!(self.stacktrace.is_empty()); - self.stacktrace = stacktrace; - } - - Ok(()) - } - - pub fn set_timestamp(&mut self, ts: DateTime) -> anyhow::Result<()> { - anyhow::ensure!(self.timestamp.is_none()); - self.timestamp = Some(ts); - Ok(()) - } - - pub fn set_timestamp_to_now(&mut self) -> anyhow::Result<()> { - self.set_timestamp(Utc::now()) - } - - pub fn set_trace_ids(&mut self, ids: Vec) -> anyhow::Result<()> { - anyhow::ensure!(self.trace_ids.is_empty()); - self.trace_ids = ids; - Ok(()) - } -} - -impl CrashInfo { - /// Emit the CrashInfo as structured json in file `path`. - /// SIGNAL SAFETY: - /// I believe but have not verified this is signal safe. - pub fn to_file(&self, path: &Path) -> anyhow::Result<()> { - let file = - File::create(path).with_context(|| format!("Failed to create {}", path.display()))?; - serde_json::to_writer_pretty(file, self) - .with_context(|| format!("Failed to write json to {}", path.display()))?; - Ok(()) - } - - pub fn upload_to_endpoint(&self, endpoint: &Option) -> anyhow::Result<()> { - // If we're debugging to a file, dump the actual crashinfo into a json - if let Some(endpoint) = endpoint { - if Some("file") == endpoint.url.scheme_str() { - let path = ddcommon::decode_uri_path_in_authority(&endpoint.url) - .context("crash output file was not correctly formatted")?; - self.to_file(&path)?; - } - } - self.upload_to_telemetry(endpoint) - } - - fn upload_to_telemetry(&self, endpoint: &Option) -> anyhow::Result<()> { - if let Some(metadata) = &self.metadata { - if let Ok(uploader) = TelemetryCrashUploader::new(metadata, endpoint) { - uploader.upload_to_telemetry(self)?; - } - } - Ok(()) - } -} +pub use internal::*; diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 755969080c..2262f96923 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -58,7 +58,7 @@ pub struct StructuredCrashInfo { pub files: HashMap>, pub incomplete: bool, #[serde(default, skip_serializing_if = "Option::is_none")] - pub metadata: Option, + pub metadata: Option, pub os_info: os_info::Info, #[serde(default, skip_serializing_if = "Option::is_none")] pub proc_info: Option, @@ -68,7 +68,7 @@ pub struct StructuredCrashInfo { pub stacktrace: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub trace_ids: Vec, - #[serde(default, skip_serializing_if = "HashMap::is_empty")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub timestamp: Option>, pub uuid: Uuid, } From 9aa596e8656bf4e75e4ceea4abfcbf10add405d0 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 14:57:51 -0400 Subject: [PATCH 03/19] split it up --- .../structured_log_format/error_data.rs | 23 ++++++++++ .../structured_log_format/metadata.rs | 16 +++++++ .../crash_info/structured_log_format/mod.rs | 45 +++---------------- .../structured_log_format/stacktrace.rs | 5 +++ 4 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 crashtracker/src/crash_info/structured_log_format/error_data.rs create mode 100644 crashtracker/src/crash_info/structured_log_format/metadata.rs diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs new file mode 100644 index 0000000000..6b8073119b --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -0,0 +1,23 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use super::stacktrace::{StackType, StackFrame}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum ErrorKind { + SigBus, + SigSegv, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ErrorData { + #[serde(default, skip_serializing_if = "HashMap::is_empty")] + pub additional_stacks: HashMap>, + pub is_crash: bool, + pub kind: ErrorKind, + pub message: String, + pub stack: Vec, + pub stack_type: StackType, +} \ No newline at end of file diff --git a/crashtracker/src/crash_info/structured_log_format/metadata.rs b/crashtracker/src/crash_info/structured_log_format/metadata.rs new file mode 100644 index 0000000000..175637abc7 --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/metadata.rs @@ -0,0 +1,16 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use uuid::Uuid; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Metadata { + pub library_name: String, + pub library_version: String, + pub family: String, + // Should include "service", "environment", etc + pub tags: Vec, +} diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 2262f96923..ac8d814075 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -3,45 +3,18 @@ mod stacktrace; pub use stacktrace::*; +mod error_data; +pub use error_data::*; +mod metadata; +pub use metadata::*; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use uuid::Uuid; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub enum ErrorKind { - SigBus, - SigSegv, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub enum StackType { - CrashTrackerV1, -} +use super::CrashInfo; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct Metadata { - pub library_name: String, - pub library_version: String, - pub family: String, - // Should include "service", "environment", etc - pub tags: Vec, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct StackFrame {} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct ErrorData { - #[serde(default, skip_serializing_if = "HashMap::is_empty")] - pub additional_stacks: HashMap>, - pub is_crash: bool, - pub kind: ErrorKind, - pub message: String, - pub stack: Vec, - pub stack_type: StackType, -} #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ProcessInfo { @@ -50,8 +23,6 @@ pub struct ProcessInfo { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StructuredCrashInfo { - #[serde(default, skip_serializing_if = "HashMap::is_empty")] - pub additional_stacktraces: HashMap>, #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub counters: HashMap, #[serde(default, skip_serializing_if = "HashMap::is_empty")] @@ -63,11 +34,9 @@ pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "Option::is_none")] pub proc_info: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub span_ids: Vec, - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub stacktrace: Vec, + pub span_ids: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub trace_ids: Vec, + pub trace_ids: Vec, #[serde(default, skip_serializing_if = "Option::is_none")] pub timestamp: Option>, pub uuid: Uuid, diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs index 8c90e60d6c..f3df2e7402 100644 --- a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -4,6 +4,11 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum StackType { + CrashTrackerV1, +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct StackFrameNames { #[serde(default, skip_serializing_if = "Option::is_none")] From 59622beab40cc6eeaa16328ccb84a2d85e48530e Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 15:56:57 -0400 Subject: [PATCH 04/19] clippy --- .../src/crash_info/structured_log_format/error_data.rs | 4 ++-- crashtracker/src/crash_info/structured_log_format/metadata.rs | 3 --- crashtracker/src/crash_info/structured_log_format/mod.rs | 3 --- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 6b8073119b..0042640d7c 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -1,9 +1,9 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 +use super::stacktrace::{StackFrame, StackType}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use super::stacktrace::{StackType, StackFrame}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum ErrorKind { @@ -20,4 +20,4 @@ pub struct ErrorData { pub message: String, pub stack: Vec, pub stack_type: StackType, -} \ No newline at end of file +} diff --git a/crashtracker/src/crash_info/structured_log_format/metadata.rs b/crashtracker/src/crash_info/structured_log_format/metadata.rs index 175637abc7..1010833d1e 100644 --- a/crashtracker/src/crash_info/structured_log_format/metadata.rs +++ b/crashtracker/src/crash_info/structured_log_format/metadata.rs @@ -1,10 +1,7 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use uuid::Uuid; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Metadata { diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index ac8d814075..bea770a6f6 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -13,9 +13,6 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use uuid::Uuid; -use super::CrashInfo; - - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ProcessInfo { pub pid: u32, From dcc0416f2d885da4fd1f112e4a2876ff852a2cb2 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 16:18:55 -0400 Subject: [PATCH 05/19] working on From function --- .../structured_log_format/error_data.rs | 1 + .../crash_info/structured_log_format/mod.rs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 0042640d7c..112c5b11c8 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -9,6 +9,7 @@ use std::collections::HashMap; pub enum ErrorKind { SigBus, SigSegv, + Unknown, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index bea770a6f6..3c465bf1ff 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -38,3 +38,27 @@ pub struct StructuredCrashInfo { pub timestamp: Option>, pub uuid: Uuid, } + +impl From for StructuredCrashInfo { + fn from(value: super::internal::CrashInfo) -> Self { + let kind = if let Some(siginfo) = value.siginfo { + match siginfo.signum as libc::c_int { + libc::SIGSEGV => ErrorKind::SigSegv, + libc::SIGBUS => ErrorKind::SigBus, + _ => ErrorKind::Unknown, + } + } else { + ErrorKind::Unknown + }; + let error_data = ErrorData { + additional_stacks: HashMap::new(), + is_crash : true, + kind, + message: "placeholder".to_string(), + stack: vec![], + stack_type: StackType::CrashTrackerV1 + }; + + todo!() + } +} From a1fd1e2f95ee31c6b8a00650f5d8d6345b4141c7 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Tue, 6 Aug 2024 17:01:15 -0400 Subject: [PATCH 06/19] working on converting the types --- .../src/crash_info/structured_log_format/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 3c465bf1ff..35d8b463a7 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -37,8 +37,11 @@ pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "Option::is_none")] pub timestamp: Option>, pub uuid: Uuid, + pub version_id: u64, } + + impl From for StructuredCrashInfo { fn from(value: super::internal::CrashInfo) -> Self { let kind = if let Some(siginfo) = value.siginfo { @@ -59,6 +62,18 @@ impl From for StructuredCrashInfo { stack_type: StackType::CrashTrackerV1 }; - todo!() + Self { + counters: value.counters, + files: value.files, + incomplete: value.incomplete, + metadata: None,//TODO + os_info: value.os_info, //TODO, make this defined + proc_info: value.proc_info, + span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), + trace_ids: value.trace_ids.into_iter().map(|v| v.to_string()).collect(), + timestamp : value.timestamp, + uuid: value.uuid, + version_id: 1 + } } } From f74fbc1296d09c42e56ea49f5b005b47d0ccee75 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 11:34:28 -0400 Subject: [PATCH 07/19] into for processinfo --- crashtracker/src/crash_info/structured_log_format/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 35d8b463a7..5104fe9e30 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -7,16 +7,14 @@ mod error_data; pub use error_data::*; mod metadata; pub use metadata::*; +mod process_info; +pub use process_info::*; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use uuid::Uuid; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct ProcessInfo { - pub pid: u32, -} #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StructuredCrashInfo { @@ -68,7 +66,7 @@ impl From for StructuredCrashInfo { incomplete: value.incomplete, metadata: None,//TODO os_info: value.os_info, //TODO, make this defined - proc_info: value.proc_info, + proc_info: value.proc_info.map(ProcessInfo::from), span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), trace_ids: value.trace_ids.into_iter().map(|v| v.to_string()).collect(), timestamp : value.timestamp, From ca3f434ef743eb580732dcaae2d5bdfdb2dc6ba4 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 13:54:15 -0400 Subject: [PATCH 08/19] modify stacktrace to make it easier to convert --- crashtracker-ffi/src/crash_info/datatypes.rs | 6 +-- .../src/crash_info/internal/stacktrace.rs | 6 +-- .../crash_info/structured_log_format/mod.rs | 2 + .../structured_log_format/stacktrace.rs | 38 ++++++++++++------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/crashtracker-ffi/src/crash_info/datatypes.rs b/crashtracker-ffi/src/crash_info/datatypes.rs index d52139aa46..b5b17f1c07 100644 --- a/crashtracker-ffi/src/crash_info/datatypes.rs +++ b/crashtracker-ffi/src/crash_info/datatypes.rs @@ -187,14 +187,12 @@ impl<'a> TryFrom<&StackFrame<'a>> for datadog_crashtracker::StackFrame { } let ip = to_hex(value.ip); let module_base_address = to_hex(value.module_base_address); - let names = if value.names.is_empty() { - None - } else { + let names = { let mut vec = Vec::with_capacity(value.names.len()); for x in value.names.iter() { vec.push(x.try_into()?); } - Some(vec) + vec }; let normalized_ip = (&value.normalized_ip).try_into()?; let sp = to_hex(value.sp); diff --git a/crashtracker/src/crash_info/internal/stacktrace.rs b/crashtracker/src/crash_info/internal/stacktrace.rs index de90321a45..89e09c8dd6 100644 --- a/crashtracker/src/crash_info/internal/stacktrace.rs +++ b/crashtracker/src/crash_info/internal/stacktrace.rs @@ -23,8 +23,8 @@ pub struct StackFrame { pub ip: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub module_base_address: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub names: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub names: Vec, #[serde(default, skip_serializing_if = "Option::is_none")] pub normalized_ip: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -112,7 +112,7 @@ mod unix { match symbolizer.symbolize_single(src, input)? { Symbolized::Sym(s) => { //TODO: handle - self.names = Some(vec![s.into()]); + self.names = vec![s.into()]; } Symbolized::Unknown(reason) => { anyhow::bail!("Couldn't symbolize {ip}: {reason}"); diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 5104fe9e30..bbd06babf3 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -20,6 +20,7 @@ use uuid::Uuid; pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub counters: HashMap, + pub error: ErrorData, #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub files: HashMap>, pub incomplete: bool, @@ -62,6 +63,7 @@ impl From for StructuredCrashInfo { Self { counters: value.counters, + error: error_data, files: value.files, incomplete: value.incomplete, metadata: None,//TODO diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs index f3df2e7402..88ac96e85a 100644 --- a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -9,27 +9,27 @@ pub enum StackType { CrashTrackerV1, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] -pub struct StackFrameNames { - #[serde(default, skip_serializing_if = "Option::is_none")] - pub colno: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub filename: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub lineno: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub name: Option, +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct StackTrace { + pub format: StackType, + pub trace: Vec } /// All fields are hex encoded integers. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] pub struct StackFrame { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub column: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub file: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub function: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub ip: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - pub module_base_address: Option, + pub line: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - pub names: Option>, + pub module_base_address: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub normalized_ip: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -54,3 +54,15 @@ pub struct NormalizedAddress { pub file_offset: u64, pub meta: NormalizedAddressMeta, } + +impl From> for StackTrace { + fn from(value: Vec) -> Self { + let mut trace = vec![]; + for frame in value { + } + Self { + trace, + format: StackType::CrashTrackerV1 + } + } +} \ No newline at end of file From 93b0271213dd4c5b373bf96e90df038759e0a256 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 14:01:20 -0400 Subject: [PATCH 09/19] use iter collect rather than mut vec --- crashtracker-ffi/src/crash_info/datatypes.rs | 12 +++++------- .../src/crash_info/structured_log_format/mod.rs | 13 +++++-------- .../crash_info/structured_log_format/stacktrace.rs | 13 ++++++------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/crashtracker-ffi/src/crash_info/datatypes.rs b/crashtracker-ffi/src/crash_info/datatypes.rs index b5b17f1c07..cdc0de33a8 100644 --- a/crashtracker-ffi/src/crash_info/datatypes.rs +++ b/crashtracker-ffi/src/crash_info/datatypes.rs @@ -187,13 +187,11 @@ impl<'a> TryFrom<&StackFrame<'a>> for datadog_crashtracker::StackFrame { } let ip = to_hex(value.ip); let module_base_address = to_hex(value.module_base_address); - let names = { - let mut vec = Vec::with_capacity(value.names.len()); - for x in value.names.iter() { - vec.push(x.try_into()?); - } - vec - }; + let names = value + .names + .iter() + .map(datadog_crashtracker::StackFrameNames::try_from) + .collect::>>()?; let normalized_ip = (&value.normalized_ip).try_into()?; let sp = to_hex(value.sp); let symbol_address = to_hex(value.symbol_address); diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index bbd06babf3..cfd784ac6b 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -15,7 +15,6 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use uuid::Uuid; - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "HashMap::is_empty")] @@ -39,8 +38,6 @@ pub struct StructuredCrashInfo { pub version_id: u64, } - - impl From for StructuredCrashInfo { fn from(value: super::internal::CrashInfo) -> Self { let kind = if let Some(siginfo) = value.siginfo { @@ -54,11 +51,11 @@ impl From for StructuredCrashInfo { }; let error_data = ErrorData { additional_stacks: HashMap::new(), - is_crash : true, + is_crash: true, kind, message: "placeholder".to_string(), stack: vec![], - stack_type: StackType::CrashTrackerV1 + stack_type: StackType::CrashTrackerV1, }; Self { @@ -66,14 +63,14 @@ impl From for StructuredCrashInfo { error: error_data, files: value.files, incomplete: value.incomplete, - metadata: None,//TODO + metadata: None, //TODO os_info: value.os_info, //TODO, make this defined proc_info: value.proc_info.map(ProcessInfo::from), span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), trace_ids: value.trace_ids.into_iter().map(|v| v.to_string()).collect(), - timestamp : value.timestamp, + timestamp: value.timestamp, uuid: value.uuid, - version_id: 1 + version_id: 1, } } } diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs index 88ac96e85a..d169c36b44 100644 --- a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -12,7 +12,7 @@ pub enum StackType { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StackTrace { pub format: StackType, - pub trace: Vec + pub trace: Vec, } /// All fields are hex encoded integers. @@ -29,7 +29,7 @@ pub struct StackFrame { #[serde(default, skip_serializing_if = "Option::is_none")] pub line: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - pub module_base_address: Option, + pub module_base_address: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub normalized_ip: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -57,12 +57,11 @@ pub struct NormalizedAddress { impl From> for StackTrace { fn from(value: Vec) -> Self { - let mut trace = vec![]; - for frame in value { - } + let trace = vec![]; + for _frame in value {} Self { trace, - format: StackType::CrashTrackerV1 + format: StackType::CrashTrackerV1, } } -} \ No newline at end of file +} From 10b80901c12cfd19d33e183a1bb54bd93d06e296 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 16:10:05 -0400 Subject: [PATCH 10/19] test output --- crashtracker/src/collector/api.rs | 3 +- crashtracker/src/crash_info/internal/mod.rs | 3 ++ .../structured_log_format/error_data.rs | 6 +-- .../crash_info/structured_log_format/mod.rs | 18 ++++++- .../structured_log_format/stacktrace.rs | 54 ++++++++++++++++++- 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/crashtracker/src/collector/api.rs b/crashtracker/src/collector/api.rs index afee513557..97e98acbe7 100644 --- a/crashtracker/src/collector/api.rs +++ b/crashtracker/src/collector/api.rs @@ -146,7 +146,8 @@ fn test_crash() -> anyhow::Result<()> { let endpoint = Some(Endpoint::from_slice(&output_url)); let path_to_receiver_binary = - "/tmp/libdatadog/bin/libdatadog-crashtracking-receiver".to_string(); + "/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/build-ffi/bin/libdatadog-crashtracking-receiver".to_string(); + // "/tmp/libdatadog/bin/libdatadog-crashtracking-receiver".to_string(); let create_alt_stack = true; let resolve_frames = StacktraceCollection::EnabledWithInprocessSymbols; let stderr_filename = Some(format!("{dir}/stderr_{time}.txt")); diff --git a/crashtracker/src/crash_info/internal/mod.rs b/crashtracker/src/crash_info/internal/mod.rs index da9305f2cb..e42e17c14a 100644 --- a/crashtracker/src/crash_info/internal/mod.rs +++ b/crashtracker/src/crash_info/internal/mod.rs @@ -255,6 +255,9 @@ impl CrashInfo { let path = ddcommon::decode_uri_path_in_authority(&endpoint.url) .context("crash output file was not correctly formatted")?; self.to_file(&path)?; + let structured: crate::crash_info::structured_log_format::StructuredCrashInfo = + self.clone().into(); + structured.to_file(&path.with_extension(".structured.json"))?; } } self.upload_to_telemetry(endpoint) diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 112c5b11c8..565a40f21a 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -1,7 +1,7 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -use super::stacktrace::{StackFrame, StackType}; +use super::{stacktrace::{StackFrame, StackType}, StackTrace}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -15,10 +15,10 @@ pub enum ErrorKind { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ErrorData { #[serde(default, skip_serializing_if = "HashMap::is_empty")] - pub additional_stacks: HashMap>, + pub additional_stacks: HashMap>, pub is_crash: bool, pub kind: ErrorKind, pub message: String, - pub stack: Vec, + pub stack: StackTrace, pub stack_type: StackType, } diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index cfd784ac6b..ddafb0cba4 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -10,9 +10,10 @@ pub use metadata::*; mod process_info; pub use process_info::*; +use anyhow::Context; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::{collections::HashMap, fs::File, path::Path}; use uuid::Uuid; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -54,7 +55,7 @@ impl From for StructuredCrashInfo { is_crash: true, kind, message: "placeholder".to_string(), - stack: vec![], + stack: value.stacktrace.into(), stack_type: StackType::CrashTrackerV1, }; @@ -74,3 +75,16 @@ impl From for StructuredCrashInfo { } } } + +impl StructuredCrashInfo { + /// Emit the CrashInfo as structured json in file `path`. + /// SIGNAL SAFETY: + /// I believe but have not verified this is signal safe. + pub fn to_file(&self, path: &Path) -> anyhow::Result<()> { + let file = + File::create(path).with_context(|| format!("Failed to create {}", path.display()))?; + serde_json::to_writer_pretty(file, self) + .with_context(|| format!("Failed to write json to {}", path.display()))?; + Ok(()) + } +} diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs index d169c36b44..4bab2cfcf5 100644 --- a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -49,16 +49,66 @@ pub enum NormalizedAddressMeta { Unexpected(String), } +impl From for NormalizedAddressMeta { + fn from(value: crate::crash_info::internal::NormalizedAddressMeta) -> Self { + match value { + crate::NormalizedAddressMeta::Apk(path) => NormalizedAddressMeta::Apk(path), + crate::NormalizedAddressMeta::Elf { path, build_id } => { + NormalizedAddressMeta::Elf { path, build_id } + } + crate::NormalizedAddressMeta::Unknown => NormalizedAddressMeta::Unknown, + crate::NormalizedAddressMeta::Unexpected(msg) => NormalizedAddressMeta::Unexpected(msg), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct NormalizedAddress { pub file_offset: u64, pub meta: NormalizedAddressMeta, } +impl From for NormalizedAddress { + fn from(value: crate::crash_info::internal::NormalizedAddress) -> Self { + Self { + file_offset: value.file_offset, + meta: value.meta.into(), + } + } +} + impl From> for StackTrace { fn from(value: Vec) -> Self { - let trace = vec![]; - for _frame in value {} + let mut trace = vec![]; + for frame in value { + if frame.names.is_empty() { + trace.push(StackFrame { + column: None, + file: None, + function: None, + ip: frame.ip, + line: None, + module_base_address: frame.module_base_address, + normalized_ip: frame.normalized_ip.map(NormalizedAddress::from), + sp: frame.sp, + symbol_address: frame.symbol_address, + }) + } else { + for name in frame.names { + trace.push(StackFrame { + column: name.colno, + file: name.filename, + function: name.name, + ip: frame.ip.clone(), + line: name.lineno, + module_base_address: frame.module_base_address.clone(), + normalized_ip: frame.normalized_ip.clone().map(NormalizedAddress::from), + sp: frame.sp.clone(), + symbol_address: frame.symbol_address.clone(), + }) + } + } + } Self { trace, format: StackType::CrashTrackerV1, From f68a9cd23852719bdea7ec4d1b968c7563673be3 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 16:32:00 -0400 Subject: [PATCH 11/19] continued transforming into the structured format --- .../structured_log_format/error_data.rs | 4 +-- .../crash_info/structured_log_format/mod.rs | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 565a40f21a..1faafbc93c 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -1,7 +1,7 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -use super::{stacktrace::{StackFrame, StackType}, StackTrace}; +use super::{stacktrace::StackType, StackTrace}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -15,7 +15,7 @@ pub enum ErrorKind { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ErrorData { #[serde(default, skip_serializing_if = "HashMap::is_empty")] - pub additional_stacks: HashMap>, + pub additional_stacks: HashMap, pub is_crash: bool, pub kind: ErrorKind, pub message: String, diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index ddafb0cba4..99eff193fe 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -41,20 +41,30 @@ pub struct StructuredCrashInfo { impl From for StructuredCrashInfo { fn from(value: super::internal::CrashInfo) -> Self { - let kind = if let Some(siginfo) = value.siginfo { - match siginfo.signum as libc::c_int { - libc::SIGSEGV => ErrorKind::SigSegv, - libc::SIGBUS => ErrorKind::SigBus, - _ => ErrorKind::Unknown, - } + let (message, kind) = if let Some(siginfo) = value.siginfo { + ( + siginfo.signame.unwrap_or_default(), + match siginfo.signum as libc::c_int { + libc::SIGSEGV => ErrorKind::SigSegv, + libc::SIGBUS => ErrorKind::SigBus, + _ => ErrorKind::Unknown, + }, + ) } else { - ErrorKind::Unknown + ("Unknown".to_string(), ErrorKind::Unknown) }; + + let additional_stacks = value + .additional_stacktraces + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(); + let error_data = ErrorData { - additional_stacks: HashMap::new(), + additional_stacks, is_crash: true, kind, - message: "placeholder".to_string(), + message, stack: value.stacktrace.into(), stack_type: StackType::CrashTrackerV1, }; From 449448a6a719abe6800e36d2333c2aae69b70d07 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 16:51:57 -0400 Subject: [PATCH 12/19] output metadata --- .../crash_info/structured_log_format/metadata.rs | 15 +++++++++++++++ .../src/crash_info/structured_log_format/mod.rs | 2 +- .../structured_log_format/process_info.rs | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 crashtracker/src/crash_info/structured_log_format/process_info.rs diff --git a/crashtracker/src/crash_info/structured_log_format/metadata.rs b/crashtracker/src/crash_info/structured_log_format/metadata.rs index 1010833d1e..f00d9502bf 100644 --- a/crashtracker/src/crash_info/structured_log_format/metadata.rs +++ b/crashtracker/src/crash_info/structured_log_format/metadata.rs @@ -11,3 +11,18 @@ pub struct Metadata { // Should include "service", "environment", etc pub tags: Vec, } + +impl From for Metadata { + fn from(value: crate::crash_info::internal::CrashtrackerMetadata) -> Self { + Self { + library_name: value.library_name, + library_version: value.library_version, + family: value.family, + tags: value + .tags + .iter() + .map(ddcommon::tag::Tag::to_string) + .collect(), + } + } +} diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 99eff193fe..ca38867651 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -74,7 +74,7 @@ impl From for StructuredCrashInfo { error: error_data, files: value.files, incomplete: value.incomplete, - metadata: None, //TODO + metadata: value.metadata.map(Metadata::from), os_info: value.os_info, //TODO, make this defined proc_info: value.proc_info.map(ProcessInfo::from), span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), diff --git a/crashtracker/src/crash_info/structured_log_format/process_info.rs b/crashtracker/src/crash_info/structured_log_format/process_info.rs new file mode 100644 index 0000000000..41bf9b4bd7 --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/process_info.rs @@ -0,0 +1,16 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +use super::super::internal; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ProcessInfo { + pub pid: u32, +} + +impl From for ProcessInfo { + fn from(value: internal::ProcessInfo) -> Self { + Self { pid: value.pid } + } +} From 8f25cff86fb683fa413dddd54eb341c1ec4653ac Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 17:13:10 -0400 Subject: [PATCH 13/19] we now fully translate the structures --- Cargo.lock | 42 +++++++++++++++++++ crashtracker/Cargo.toml | 1 + .../crash_info/structured_log_format/mod.rs | 6 ++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93f5cd9303..401d270bb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1345,6 +1345,7 @@ dependencies = [ "page_size", "portable-atomic", "rand", + "schemars", "serde", "serde_json", "tempfile", @@ -1851,6 +1852,12 @@ dependencies = [ "proc-macro-error", ] +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + [[package]] name = "educe" version = "0.4.23" @@ -4196,6 +4203,30 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "schemars" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" +dependencies = [ + "dyn-clone", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 2.0.68", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -4273,6 +4304,17 @@ dependencies = [ "syn 2.0.68", ] +[[package]] +name = "serde_derive_internals" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + [[package]] name = "serde_json" version = "1.0.117" diff --git a/crashtracker/Cargo.toml b/crashtracker/Cargo.toml index 248c9a8d39..89f7de734d 100644 --- a/crashtracker/Cargo.toml +++ b/crashtracker/Cargo.toml @@ -40,6 +40,7 @@ tokio = { version = "1.23", features = ["rt", "macros"] } http = "0.2" portable-atomic = { version = "1.6.0", features = ["serde"] } rand = "0.8.5" +schemars = "0.8.21" [dev-dependencies] tempfile = { version = "3.3" } diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index ca38867651..54957aeeaa 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -7,6 +7,8 @@ mod error_data; pub use error_data::*; mod metadata; pub use metadata::*; +mod os_info; +pub use os_info::*; mod process_info; pub use process_info::*; @@ -26,7 +28,7 @@ pub struct StructuredCrashInfo { pub incomplete: bool, #[serde(default, skip_serializing_if = "Option::is_none")] pub metadata: Option, - pub os_info: os_info::Info, + pub os_info: OsInfo, #[serde(default, skip_serializing_if = "Option::is_none")] pub proc_info: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -75,7 +77,7 @@ impl From for StructuredCrashInfo { files: value.files, incomplete: value.incomplete, metadata: value.metadata.map(Metadata::from), - os_info: value.os_info, //TODO, make this defined + os_info: value.os_info.into(), //TODO, make this defined proc_info: value.proc_info.map(ProcessInfo::from), span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), trace_ids: value.trace_ids.into_iter().map(|v| v.to_string()).collect(), From a8291524a6b3d1bc4d49c5e23219f1bf3923c4f8 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 17:34:43 -0400 Subject: [PATCH 14/19] the final structures --- .../structured_log_format/error_data.rs | 6 +- .../structured_log_format/metadata.rs | 3 +- .../crash_info/structured_log_format/mod.rs | 15 +++-- .../structured_log_format/os_info.rs | 59 +++++++++++++++++++ .../structured_log_format/process_info.rs | 3 +- .../structured_log_format/stacktrace.rs | 11 ++-- 6 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 crashtracker/src/crash_info/structured_log_format/os_info.rs diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 1faafbc93c..aa95fb9f9b 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -1,18 +1,18 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 - use super::{stacktrace::StackType, StackTrace}; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub enum ErrorKind { SigBus, SigSegv, Unknown, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct ErrorData { #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub additional_stacks: HashMap, diff --git a/crashtracker/src/crash_info/structured_log_format/metadata.rs b/crashtracker/src/crash_info/structured_log_format/metadata.rs index f00d9502bf..56aa910530 100644 --- a/crashtracker/src/crash_info/structured_log_format/metadata.rs +++ b/crashtracker/src/crash_info/structured_log_format/metadata.rs @@ -1,9 +1,10 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct Metadata { pub library_name: String, pub library_version: String, diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 54957aeeaa..dea4174169 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -13,12 +13,11 @@ mod process_info; pub use process_info::*; use anyhow::Context; -use chrono::{DateTime, Utc}; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fs::File, path::Path}; -use uuid::Uuid; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub counters: HashMap, @@ -36,8 +35,8 @@ pub struct StructuredCrashInfo { #[serde(default, skip_serializing_if = "Vec::is_empty")] pub trace_ids: Vec, #[serde(default, skip_serializing_if = "Option::is_none")] - pub timestamp: Option>, - pub uuid: Uuid, + pub timestamp: Option, + pub uuid: String, pub version_id: u64, } @@ -77,12 +76,12 @@ impl From for StructuredCrashInfo { files: value.files, incomplete: value.incomplete, metadata: value.metadata.map(Metadata::from), - os_info: value.os_info.into(), //TODO, make this defined + os_info: value.os_info.into(), proc_info: value.proc_info.map(ProcessInfo::from), span_ids: value.span_ids.into_iter().map(|v| v.to_string()).collect(), trace_ids: value.trace_ids.into_iter().map(|v| v.to_string()).collect(), - timestamp: value.timestamp, - uuid: value.uuid, + timestamp: value.timestamp.map(|v| v.to_string()), + uuid: value.uuid.to_string(), version_id: 1, } } diff --git a/crashtracker/src/crash_info/structured_log_format/os_info.rs b/crashtracker/src/crash_info/structured_log_format/os_info.rs new file mode 100644 index 0000000000..490ef667f9 --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/os_info.rs @@ -0,0 +1,59 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] +pub enum Version { + Unknown, + Semantic(u64, u64, u64), + Rolling(Option), + Custom(String), +} + +impl Default for Version { + fn default() -> Self { + Self::Unknown + } +} + +impl From<&os_info::Version> for Version { + fn from(value: &os_info::Version) -> Self { + use Version::*; + match value { + os_info::Version::Unknown => Unknown, + os_info::Version::Semantic(a, b, c) => Semantic(*a, *b, *c), + os_info::Version::Rolling(a) => Rolling(a.clone()), + os_info::Version::Custom(a) => Custom(a.to_string()), + } + } +} + +impl Version { + pub fn is_unknown(&self) -> bool { + matches!(self, Self::Unknown) + } +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] +pub struct OsInfo { + #[serde(default, skip_serializing_if = "String::is_empty")] + architecture: String, + #[serde(default, skip_serializing_if = "String::is_empty")] + bitness: String, + #[serde(default, skip_serializing_if = "String::is_empty")] + os_type: String, + #[serde(default, skip_serializing_if = "Version::is_unknown")] + version: Version, +} + +impl From for OsInfo { + fn from(value: os_info::Info) -> Self { + Self { + architecture: value.architecture().unwrap_or_default().to_string(), + bitness: value.bitness().to_string(), + os_type: value.os_type().to_string(), + version: value.version().into(), + } + } +} diff --git a/crashtracker/src/crash_info/structured_log_format/process_info.rs b/crashtracker/src/crash_info/structured_log_format/process_info.rs index 41bf9b4bd7..505d6b1d80 100644 --- a/crashtracker/src/crash_info/structured_log_format/process_info.rs +++ b/crashtracker/src/crash_info/structured_log_format/process_info.rs @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use super::super::internal; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct ProcessInfo { pub pid: u32, } diff --git a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs index 4bab2cfcf5..b8283e16b4 100644 --- a/crashtracker/src/crash_info/structured_log_format/stacktrace.rs +++ b/crashtracker/src/crash_info/structured_log_format/stacktrace.rs @@ -1,22 +1,23 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::path::PathBuf; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub enum StackType { CrashTrackerV1, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct StackTrace { pub format: StackType, pub trace: Vec, } /// All fields are hex encoded integers. -#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct StackFrame { #[serde(default, skip_serializing_if = "Option::is_none")] pub column: Option, @@ -38,7 +39,7 @@ pub struct StackFrame { pub symbol_address: Option, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub enum NormalizedAddressMeta { Apk(PathBuf), Elf { @@ -62,7 +63,7 @@ impl From for NormalizedAddr } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct NormalizedAddress { pub file_offset: u64, pub meta: NormalizedAddressMeta, From 4dafc187960b63b1f646136de004e22deda2e2c1 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Wed, 7 Aug 2024 17:36:53 -0400 Subject: [PATCH 15/19] add schema emission --- crashtracker/src/crash_info/structured_log_format/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index dea4174169..382bd77acd 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -99,3 +99,9 @@ impl StructuredCrashInfo { Ok(()) } } + +#[test] +fn schema() { + let schema = schemars::schema_for!(StructuredCrashInfo); + println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +} From 3d3ac331964e5f8ad8018603a670c5dd47c52cc8 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Thu, 8 Aug 2024 14:33:04 -0400 Subject: [PATCH 16/19] Threads instead of additional stacktraces --- .../structured_log_format/error_data.rs | 7 +++---- .../src/crash_info/structured_log_format/mod.rs | 13 ++++++++++--- .../crash_info/structured_log_format/thread.rs | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 crashtracker/src/crash_info/structured_log_format/thread.rs diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index aa95fb9f9b..2b056143cd 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -1,9 +1,8 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -use super::{stacktrace::StackType, StackTrace}; +use super::{stacktrace::StackType, StackTrace, Thread}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub enum ErrorKind { @@ -14,11 +13,11 @@ pub enum ErrorKind { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct ErrorData { - #[serde(default, skip_serializing_if = "HashMap::is_empty")] - pub additional_stacks: HashMap, pub is_crash: bool, pub kind: ErrorKind, pub message: String, pub stack: StackTrace, pub stack_type: StackType, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub threads: Vec, } diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index 382bd77acd..c64491280a 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -11,6 +11,8 @@ mod os_info; pub use os_info::*; mod process_info; pub use process_info::*; +mod thread; +pub use thread::*; use anyhow::Context; use schemars::JsonSchema; @@ -55,19 +57,24 @@ impl From for StructuredCrashInfo { ("Unknown".to_string(), ErrorKind::Unknown) }; - let additional_stacks = value + let threads = value .additional_stacktraces .into_iter() - .map(|(k, v)| (k, v.into())) + .map(|(name, stack)| Thread { + crashed: false, + name, + stack: stack.into(), + state: String::new(), + }) .collect(); let error_data = ErrorData { - additional_stacks, is_crash: true, kind, message, stack: value.stacktrace.into(), stack_type: StackType::CrashTrackerV1, + threads, }; Self { diff --git a/crashtracker/src/crash_info/structured_log_format/thread.rs b/crashtracker/src/crash_info/structured_log_format/thread.rs new file mode 100644 index 0000000000..c7e444dfb9 --- /dev/null +++ b/crashtracker/src/crash_info/structured_log_format/thread.rs @@ -0,0 +1,17 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +use super::StackTrace; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] +pub struct Thread { + pub crashed: bool, + pub name: String, + pub stack: StackTrace, + #[serde(default, skip_serializing_if = "String::is_empty")] + /// "Platform-specific state of the thread when its state was captured (CPU registers dump for + /// iOS, thread state enum for Android, etc.)." + pub state: String, +} From cc23c57b77823e89d50685a3cd23ae8a1ec8aef4 Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Thu, 8 Aug 2024 14:39:46 -0400 Subject: [PATCH 17/19] multiple tags in test --- crashtracker/src/collector/api.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crashtracker/src/collector/api.rs b/crashtracker/src/collector/api.rs index 97e98acbe7..6cfe9d579e 100644 --- a/crashtracker/src/collector/api.rs +++ b/crashtracker/src/collector/api.rs @@ -180,12 +180,11 @@ fn test_crash() -> anyhow::Result<()> { super::insert_span(12)?; super::insert_trace(99399939399939393993)?; - let tag = tag!("apple", "banana"); let metadata2 = CrashtrackerMetadata::new( "libname".to_string(), "version".to_string(), "family".to_string(), - vec![tag], + vec![tag!("strawberry", "jam"), tag!("orange", "marmalade")], ); update_metadata(metadata2).expect("metadata"); From b15d28bc10279e6d852b50eba568e8d47eac87cc Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Thu, 8 Aug 2024 15:46:53 -0400 Subject: [PATCH 18/19] RFC comment --- crashtracker/src/crash_info/internal/mod.rs | 2 +- crashtracker/src/crash_info/structured_log_format/error_data.rs | 1 + crashtracker/src/crash_info/structured_log_format/mod.rs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crashtracker/src/crash_info/internal/mod.rs b/crashtracker/src/crash_info/internal/mod.rs index e42e17c14a..8b70052f6a 100644 --- a/crashtracker/src/crash_info/internal/mod.rs +++ b/crashtracker/src/crash_info/internal/mod.rs @@ -257,7 +257,7 @@ impl CrashInfo { self.to_file(&path)?; let structured: crate::crash_info::structured_log_format::StructuredCrashInfo = self.clone().into(); - structured.to_file(&path.with_extension(".structured.json"))?; + structured.to_file(&path.with_extension("structured.json"))?; } } self.upload_to_telemetry(endpoint) diff --git a/crashtracker/src/crash_info/structured_log_format/error_data.rs b/crashtracker/src/crash_info/structured_log_format/error_data.rs index 2b056143cd..bc453fc962 100644 --- a/crashtracker/src/crash_info/structured_log_format/error_data.rs +++ b/crashtracker/src/crash_info/structured_log_format/error_data.rs @@ -16,6 +16,7 @@ pub struct ErrorData { pub is_crash: bool, pub kind: ErrorKind, pub message: String, + pub source_type: String, pub stack: StackTrace, pub stack_type: StackType, #[serde(default, skip_serializing_if = "Vec::is_empty")] diff --git a/crashtracker/src/crash_info/structured_log_format/mod.rs b/crashtracker/src/crash_info/structured_log_format/mod.rs index c64491280a..4e892b1353 100644 --- a/crashtracker/src/crash_info/structured_log_format/mod.rs +++ b/crashtracker/src/crash_info/structured_log_format/mod.rs @@ -72,6 +72,7 @@ impl From for StructuredCrashInfo { is_crash: true, kind, message, + source_type: String::from("crashtracking"), stack: value.stacktrace.into(), stack_type: StackType::CrashTrackerV1, threads, From 357075de692fd89a2ba68476602bbeef613b8a4b Mon Sep 17 00:00:00 2001 From: Daniel Schwartz-Narbonne Date: Mon, 21 Oct 2024 13:15:33 -0400 Subject: [PATCH 19/19] format --- crashtracker/src/crash_info/structured_log_format/thread.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crashtracker/src/crash_info/structured_log_format/thread.rs b/crashtracker/src/crash_info/structured_log_format/thread.rs index c7e444dfb9..05f3a5d606 100644 --- a/crashtracker/src/crash_info/structured_log_format/thread.rs +++ b/crashtracker/src/crash_info/structured_log_format/thread.rs @@ -11,7 +11,7 @@ pub struct Thread { pub name: String, pub stack: StackTrace, #[serde(default, skip_serializing_if = "String::is_empty")] - /// "Platform-specific state of the thread when its state was captured (CPU registers dump for - /// iOS, thread state enum for Android, etc.)." + /// Platform-specific state of the thread when its state was captured (CPU registers dump for + /// iOS, thread state enum for Android, etc.). pub state: String, }