libdd-crashtracker 1.0.0

Detects program crashes and reports them to datadog backend.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use crate::runtime_callback::RuntimeStack;

use chrono::{DateTime, Utc};
use error_data::ThreadData;
use stacktrace::StackTrace;
use std::io::{BufRead, BufReader};
use unknown_value::UnknownValue;
use uuid::Uuid;

use super::*;

#[derive(Debug, Default, PartialEq)]
pub struct ErrorDataBuilder {
    pub kind: Option<ErrorKind>,
    pub message: Option<String>,
    pub stack: Option<StackTrace>,
    pub threads: Option<Vec<ThreadData>>,
}

impl ErrorDataBuilder {
    pub fn build(self) -> anyhow::Result<(ErrorData, bool /* incomplete */)> {
        let incomplete = self.stack.is_none();
        let is_crash = true;
        let kind = self.kind.context("required field 'kind' missing")?;
        let message = self.message;
        let source_type = SourceType::Crashtracking;
        let stack = self.stack.unwrap_or_else(StackTrace::missing);
        let threads = self.threads.unwrap_or_default();
        Ok((
            ErrorData {
                is_crash,
                kind,
                message,
                source_type,
                stack,
                threads,
            },
            incomplete,
        ))
    }

    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_kind(&mut self, kind: ErrorKind) -> anyhow::Result<&mut Self> {
        self.kind = Some(kind);
        Ok(self)
    }

    pub fn with_message(&mut self, message: String) -> anyhow::Result<&mut Self> {
        self.message = Some(message);
        Ok(self)
    }

    pub fn with_stack(&mut self, stack: StackTrace) -> anyhow::Result<&mut Self> {
        self.stack = Some(stack);
        Ok(self)
    }

    pub fn with_stack_frame(
        &mut self,
        frame: StackFrame,
        incomplete: bool,
    ) -> anyhow::Result<&mut Self> {
        if let Some(stack) = &mut self.stack {
            stack.push_frame(frame, incomplete)?;
        } else {
            self.stack = Some(StackTrace::from_frames(vec![frame], incomplete));
        }
        Ok(self)
    }

    pub fn with_stack_set_complete(&mut self) -> anyhow::Result<&mut Self> {
        if let Some(stack) = &mut self.stack {
            stack.set_complete()?;
        } else {
            // With https://github.com/DataDog/libdatadog/pull/1076 it happens that stack trace are
            // empty on musl based Linux (Alpine) because stack unwinding may not be able to unwind
            // passed the signal handler. This by-passing for musl is temporary and needs a fix.
            #[cfg(target_env = "musl")]
            return Ok(self);
            #[cfg(not(target_env = "musl"))]
            anyhow::bail!("Can't set non-existant stack complete");
        }
        Ok(self)
    }

    pub fn with_threads(&mut self, threads: Vec<ThreadData>) -> anyhow::Result<&mut Self> {
        self.threads = Some(threads);
        Ok(self)
    }
}

#[derive(Debug, PartialEq)]
pub struct CrashInfoBuilder {
    pub counters: Option<HashMap<String, i64>>,
    pub error: ErrorDataBuilder,
    pub experimental: Option<Experimental>,
    pub files: Option<HashMap<String, Vec<String>>>,
    pub fingerprint: Option<String>,
    pub incomplete: Option<bool>,
    pub log_messages: Option<Vec<String>>,
    pub metadata: Option<Metadata>,
    pub os_info: Option<OsInfo>,
    pub proc_info: Option<ProcInfo>,
    pub sig_info: Option<SigInfo>,
    pub span_ids: Option<Vec<Span>>,
    pub timestamp: Option<DateTime<Utc>>,
    pub trace_ids: Option<Vec<Span>>,
    pub uuid: Uuid,
}

impl Default for CrashInfoBuilder {
    fn default() -> Self {
        Self {
            counters: None,
            error: ErrorDataBuilder::default(),
            experimental: None,
            files: None,
            fingerprint: None,
            incomplete: None,
            log_messages: None,
            metadata: None,
            os_info: None,
            proc_info: None,
            sig_info: None,
            span_ids: None,
            timestamp: None,
            trace_ids: None,
            uuid: Uuid::new_v4(),
        }
    }
}

impl CrashInfoBuilder {
    pub fn build(self) -> anyhow::Result<CrashInfo> {
        let counters = self.counters.unwrap_or_default();
        let data_schema_version = CrashInfo::current_schema_version().to_string();
        let (error, incomplete_error) = self.error.build()?;
        let experimental = self.experimental;
        let files = self.files.unwrap_or_default();
        let fingerprint = self.fingerprint;
        let incomplete = incomplete_error || self.incomplete.unwrap_or(false);
        let log_messages = self.log_messages.unwrap_or_default();
        let metadata = self.metadata.unwrap_or_else(Metadata::unknown_value);
        let os_info = self.os_info.unwrap_or_else(OsInfo::unknown_value);
        let proc_info = self.proc_info;
        let sig_info = self.sig_info;
        let span_ids = self.span_ids.unwrap_or_default();
        let timestamp = self.timestamp.unwrap_or_else(Utc::now).to_string();
        let trace_ids = self.trace_ids.unwrap_or_default();
        let uuid = self.uuid;
        Ok(CrashInfo {
            counters,
            data_schema_version,
            error,
            experimental,
            files,
            fingerprint,
            incomplete,
            log_messages,
            metadata,
            os_info,
            proc_info,
            sig_info,
            span_ids,
            timestamp,
            trace_ids,
            uuid: uuid.to_string(),
        })
    }

    pub fn has_data(&self) -> bool {
        *self != Self::default()
    }

    pub fn new() -> Self {
        Self::default()
    }

    /// Inserts the given counter to the current set of counters in the builder.
    pub fn with_counter(&mut self, name: String, value: i64) -> anyhow::Result<&mut Self> {
        anyhow::ensure!(!name.is_empty(), "Empty counter name not allowed");
        if let Some(ref mut counters) = &mut self.counters {
            counters.insert(name, value);
        } else {
            self.counters = Some(HashMap::from([(name, value)]));
        }
        Ok(self)
    }

    pub fn with_counters(&mut self, counters: HashMap<String, i64>) -> anyhow::Result<&mut Self> {
        self.counters = Some(counters);
        Ok(self)
    }

    pub fn with_experimental_additional_tags(
        &mut self,
        additional_tags: Vec<String>,
    ) -> anyhow::Result<&mut Self> {
        if let Some(experimental) = &mut self.experimental {
            experimental.additional_tags = additional_tags;
        } else {
            self.experimental = Some(Experimental::new().with_additional_tags(additional_tags));
        }
        Ok(self)
    }

    pub fn with_experimental_ucontext(&mut self, ucontext: String) -> anyhow::Result<&mut Self> {
        if let Some(experimental) = &mut self.experimental {
            experimental.ucontext = Some(ucontext);
        } else {
            self.experimental = Some(Experimental::new().with_ucontext(ucontext));
        }
        Ok(self)
    }

    pub fn with_experimental_runtime_stack(
        &mut self,
        runtime_stack: RuntimeStack,
    ) -> anyhow::Result<&mut Self> {
        if let Some(experimental) = &mut self.experimental {
            experimental.runtime_stack = Some(runtime_stack);
        } else {
            self.experimental = Some(Experimental::new().with_runtime_stack(runtime_stack));
        }
        Ok(self)
    }

    pub fn with_kind(&mut self, kind: ErrorKind) -> anyhow::Result<&mut Self> {
        self.error.with_kind(kind)?;
        Ok(self)
    }

    pub fn with_file(&mut self, filename: String) -> anyhow::Result<&mut Self> {
        let file = File::open(&filename).with_context(|| format!("filename: {filename}"))?;
        let lines: std::io::Result<Vec<_>> = BufReader::new(file).lines().collect();
        self.with_file_and_contents(filename, lines?)
    }

    /// Appends the given file to the current set of files in the builder.
    pub fn with_file_and_contents(
        &mut self,
        filename: String,
        contents: Vec<String>,
    ) -> anyhow::Result<&mut Self> {
        if let Some(ref mut files) = &mut self.files {
            files.insert(filename, contents);
        } else {
            self.files = Some(HashMap::from([(filename, contents)]));
        }
        Ok(self)
    }

    /// Sets the current set of files in the builder.
    pub fn with_files(&mut self, files: HashMap<String, Vec<String>>) -> anyhow::Result<&mut Self> {
        self.files = Some(files);
        Ok(self)
    }

    pub fn with_fingerprint(&mut self, fingerprint: String) -> anyhow::Result<&mut Self> {
        anyhow::ensure!(!fingerprint.is_empty(), "Expect non-empty fingerprint");
        self.fingerprint = Some(fingerprint);
        Ok(self)
    }

    pub fn with_incomplete(&mut self, incomplete: bool) -> anyhow::Result<&mut Self> {
        self.incomplete = Some(incomplete);
        Ok(self)
    }

    /// Appends the given message to the current set of messages in the builder.
    pub fn with_log_message(
        &mut self,
        message: String,
        also_print: bool,
    ) -> anyhow::Result<&mut Self> {
        if also_print {
            eprintln!("{message}");
        }

        if let Some(ref mut messages) = &mut self.log_messages {
            messages.push(message);
        } else {
            self.log_messages = Some(vec![message]);
        }
        Ok(self)
    }

    pub fn with_log_messages(&mut self, log_messages: Vec<String>) -> anyhow::Result<&mut Self> {
        self.log_messages = Some(log_messages);
        Ok(self)
    }

    pub fn with_message(&mut self, message: String) -> anyhow::Result<&mut Self> {
        self.error.with_message(message)?;
        Ok(self)
    }

    pub fn with_metadata(&mut self, metadata: Metadata) -> anyhow::Result<&mut Self> {
        self.metadata = Some(metadata);
        Ok(self)
    }

    pub fn with_os_info(&mut self, os_info: OsInfo) -> anyhow::Result<&mut Self> {
        self.os_info = Some(os_info);
        Ok(self)
    }

    pub fn with_os_info_this_machine(&mut self) -> anyhow::Result<&mut Self> {
        self.with_os_info(::os_info::get().into())
    }

    pub fn with_proc_info(&mut self, proc_info: ProcInfo) -> anyhow::Result<&mut Self> {
        self.proc_info = Some(proc_info);
        Ok(self)
    }

    pub fn with_sig_info(&mut self, sig_info: SigInfo) -> anyhow::Result<&mut Self> {
        self.sig_info = Some(sig_info);
        Ok(self)
    }

    pub fn with_span_id(&mut self, span_id: Span) -> anyhow::Result<&mut Self> {
        if let Some(ref mut span_ids) = &mut self.span_ids {
            span_ids.push(span_id);
        } else {
            self.span_ids = Some(vec![span_id]);
        }
        Ok(self)
    }

    pub fn with_span_ids(&mut self, span_ids: Vec<Span>) -> anyhow::Result<&mut Self> {
        self.span_ids = Some(span_ids);
        Ok(self)
    }

    pub fn with_stack(&mut self, stack: StackTrace) -> anyhow::Result<&mut Self> {
        self.error.with_stack(stack)?;
        Ok(self)
    }

    pub fn with_stack_frame(
        &mut self,
        frame: StackFrame,
        incomplete: bool,
    ) -> anyhow::Result<&mut Self> {
        self.error.with_stack_frame(frame, incomplete)?;
        Ok(self)
    }

    pub fn with_stack_set_complete(&mut self) -> anyhow::Result<&mut Self> {
        self.error.with_stack_set_complete()?;
        Ok(self)
    }

    pub fn with_thread(&mut self, thread: ThreadData) -> anyhow::Result<&mut Self> {
        if let Some(ref mut threads) = &mut self.error.threads {
            threads.push(thread);
        } else {
            self.error.threads = Some(vec![thread]);
        }
        Ok(self)
    }

    pub fn with_threads(&mut self, threads: Vec<ThreadData>) -> anyhow::Result<&mut Self> {
        self.error.with_threads(threads)?;
        Ok(self)
    }

    pub fn with_timestamp(&mut self, timestamp: DateTime<Utc>) -> anyhow::Result<&mut Self> {
        self.timestamp = Some(timestamp);
        Ok(self)
    }

    pub fn with_timestamp_now(&mut self) -> anyhow::Result<&mut Self> {
        self.with_timestamp(Utc::now())
    }

    pub fn with_trace_id(&mut self, trace_id: Span) -> anyhow::Result<&mut Self> {
        if let Some(ref mut trace_ids) = &mut self.trace_ids {
            trace_ids.push(trace_id);
        } else {
            self.trace_ids = Some(vec![trace_id]);
        }
        Ok(self)
    }

    pub fn with_trace_ids(&mut self, trace_ids: Vec<Span>) -> anyhow::Result<&mut Self> {
        self.trace_ids = Some(trace_ids);
        Ok(self)
    }

    /// This method requires that the builder has a UUID and metadata set.
    /// Siginfo is optional for platforms that don't support it (like Windows)
    pub fn build_crash_ping(&self) -> anyhow::Result<CrashPing> {
        let sig_info = self.sig_info.clone();
        let metadata = self.metadata.clone().context("metadata is required")?;

        let mut builder = CrashPingBuilder::new(self.uuid).with_metadata(metadata);
        if let Some(sig_info) = sig_info {
            builder = builder.with_sig_info(sig_info);
        }
        builder.build()
    }

    pub fn is_ping_ready(&self) -> bool {
        // On Unix platforms, wait for both metadata and siginfo
        // On Windows, siginfo is not available, so only wait for metadata
        #[cfg(unix)]
        {
            self.metadata.is_some() && self.sig_info.is_some()
        }
        #[cfg(windows)]
        {
            self.metadata.is_some()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crash_info::test_utils::TestInstance;

    #[test]
    fn test_crash_info_builder_to_crash_ping() {
        let sig_info = SigInfo::test_instance(42);
        let metadata = Metadata::test_instance(1);

        let mut crash_info_builder = CrashInfoBuilder::new();
        crash_info_builder.with_sig_info(sig_info.clone()).unwrap();
        crash_info_builder.with_metadata(metadata.clone()).unwrap();
        crash_info_builder.with_kind(ErrorKind::Panic).unwrap();

        let crash_ping = crash_info_builder.build_crash_ping().unwrap();

        assert!(!crash_ping.crash_uuid().is_empty());
        assert!(Uuid::parse_str(crash_ping.crash_uuid()).is_ok());
        assert_eq!(crash_ping.siginfo(), Some(&sig_info));
        assert_eq!(crash_ping.metadata(), &metadata);
        assert!(crash_ping.message().contains("crash processing started"));
    }
}