-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathmod.rs
More file actions
480 lines (428 loc) · 16.6 KB
/
mod.rs
File metadata and controls
480 lines (428 loc) · 16.6 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::collections::HashMap;
use common_types::error_tracking::{FrameData, FrameId, RawFrameId};
use releases::ReleaseRecord;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{
error::{FrameError, UnhandledError},
fingerprinting::{FingerprintBuilder, FingerprintComponent, FingerprintRecordPart},
langs::{
apple::{AppleDebugImage, RawAppleFrame},
custom::CustomFrame,
dart::RawDartFrame,
go::RawGoFrame,
hermes::RawHermesFrame,
java::RawJavaFrame,
js::RawJSFrame,
node::RawNodeFrame,
python::RawPythonFrame,
ruby::RawRubyFrame,
},
metric_consts::{FRAME_NOT_RESOLVED, FRAME_RESOLVED, LEGACY_JS_FRAME_RESOLVED, PER_FRAME_TIME},
sanitize_string,
symbol_store::Catalog,
};
pub mod records;
pub mod releases;
// We consume a huge variety of differently shaped stack frames, which we have special-case
// transformation for, to produce a single, unified representation of a frame.
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(tag = "platform")]
pub enum RawFrame {
#[serde(rename = "python")]
Python(RawPythonFrame),
#[serde(rename = "ruby")]
Ruby(RawRubyFrame),
#[serde(rename = "web:javascript")]
JavaScriptWeb(RawJSFrame),
#[serde(rename = "node:javascript")]
JavaScriptNode(RawNodeFrame),
#[serde(rename = "go")]
Go(RawGoFrame),
#[serde(rename = "hermes")]
Hermes(RawHermesFrame),
#[serde(rename = "java")]
Java(RawJavaFrame),
#[serde(rename = "dart")]
Dart(RawDartFrame),
#[serde(rename = "apple")]
Apple(RawAppleFrame),
#[serde(rename = "custom")]
Custom(CustomFrame),
// TODO - remove once we're happy no clients are using this anymore
#[serde(rename = "javascript")]
LegacyJS(RawJSFrame),
}
impl RawFrame {
pub async fn resolve(
&self,
team_id: i32,
catalog: &Catalog,
debug_images: &[AppleDebugImage],
) -> Result<Vec<Frame>, UnhandledError> {
let frame_resolve_time = common_metrics::timing_guard(PER_FRAME_TIME, &[]);
let (res, lang_tag) = match self {
RawFrame::JavaScriptWeb(frame) => {
(to_vec(frame.resolve(team_id, catalog).await), "javascript")
}
RawFrame::LegacyJS(frame) => {
// TODO: monitor this metric and remove the legacy frame type when it hits 0
metrics::counter!(LEGACY_JS_FRAME_RESOLVED).increment(1);
(to_vec(frame.resolve(team_id, catalog).await), "javascript")
}
RawFrame::JavaScriptNode(frame) => {
(to_vec(frame.resolve(team_id, catalog).await), "javascript")
}
RawFrame::Dart(frame) => (to_vec(Ok(frame.into())), "dart"),
RawFrame::Apple(frame) => (
to_vec(frame.resolve(team_id, catalog, debug_images).await),
"apple",
),
RawFrame::Python(frame) => (to_vec(Ok(frame.into())), "python"),
RawFrame::Ruby(frame) => (to_vec(Ok(frame.into())), "ruby"),
RawFrame::Custom(frame) => (to_vec(Ok(frame.into())), "custom"),
RawFrame::Go(frame) => (to_vec(Ok(frame.into())), "go"),
RawFrame::Hermes(frame) => (to_vec(frame.resolve(team_id, catalog).await), "hermes"),
RawFrame::Java(frame) => (frame.resolve(team_id, catalog).await, "java"),
};
// The raw id of the frame is set after it's resolved
let res = res.map(|mut fs| {
fs.iter_mut()
.enumerate()
.for_each(|(index, f)| f.frame_id = self.frame_id(team_id, index));
fs
});
if res.is_err() {
frame_resolve_time.label("outcome", "failed")
} else {
frame_resolve_time.label("outcome", "success")
}
.label("lang", lang_tag)
.fin();
if let Ok(frames) = &res {
for frame in frames {
if frame.resolved {
metrics::counter!(FRAME_RESOLVED, "lang" => lang_tag).increment(1);
} else if let Some(err) = &frame.resolve_failure {
let reason = err.metric_reason();
match reason {
"network_error" | "invalid_data" | "symbol_not_found" => {
tracing::warn!(lang = lang_tag, reason = reason, error = %err, "frame resolution failed");
}
_ => {
tracing::debug!(lang = lang_tag, reason = reason, error = %err, "frame resolution failed");
}
}
metrics::counter!(FRAME_NOT_RESOLVED, "lang" => lang_tag, "reason" => reason)
.increment(1);
} else {
metrics::counter!(FRAME_NOT_RESOLVED, "lang" => lang_tag, "reason" => "unknown")
.increment(1);
}
}
}
res
}
pub fn symbol_set_ref(&self) -> Option<String> {
match self {
RawFrame::JavaScriptWeb(frame) | RawFrame::LegacyJS(frame) => frame.symbol_set_ref(),
RawFrame::JavaScriptNode(frame) => frame.chunk_id.clone(),
RawFrame::Hermes(frame) => frame.symbol_set_ref(),
RawFrame::Java(frame) => frame.symbol_set_ref(),
// Frames with no symbol sets
RawFrame::Python(_)
| RawFrame::Ruby(_)
| RawFrame::Go(_)
| RawFrame::Dart(_)
| RawFrame::Apple(_)
| RawFrame::Custom(_) => None,
}
}
pub fn raw_id(&self, team_id: i32) -> RawFrameId {
let hash_id = match self {
RawFrame::JavaScriptWeb(raw) | RawFrame::LegacyJS(raw) => raw.frame_id(),
RawFrame::JavaScriptNode(raw) => raw.frame_id(),
RawFrame::Python(raw) => raw.frame_id(),
RawFrame::Ruby(raw) => raw.frame_id(),
RawFrame::Go(raw) => raw.frame_id(),
RawFrame::Custom(raw) => raw.frame_id(),
RawFrame::Hermes(raw) => raw.frame_id(),
RawFrame::Java(raw) => raw.frame_id(),
RawFrame::Dart(raw) => raw.frame_id(),
RawFrame::Apple(raw) => raw.frame_id(),
};
RawFrameId::new(hash_id, team_id)
}
pub fn frame_id(&self, team_id: i32, index: usize) -> FrameId {
self.raw_id(team_id).to_full(index as i32)
}
pub fn is_suspicious(&self) -> bool {
match self {
RawFrame::JavaScriptWeb(frame) => frame.is_suspicious(),
_ => false,
}
}
}
// We emit a single, unified representation of a frame, which is what we pass on to users.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Frame {
// Renamed for legacy reasons - resolved frames have a full FrameId, not a RawFrameId
#[serde(rename = "raw_id")]
pub frame_id: FrameId, // The frame id this was resolved from. This has a custom serde impl to be string represented, and drops team_id on serialization
pub mangled_name: String, // Mangled name of the function
#[serde(skip_serializing_if = "Option::is_none")]
pub line: Option<u32>, // Line the function is define on, if known
#[serde(skip_serializing_if = "Option::is_none")]
pub column: Option<u32>, // Column the function is defined on, if known
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<String>, // Generally, the file name or source file path the function is defined in.
#[serde(skip_serializing_if = "Option::is_none")]
pub module: Option<String>, // The module the function is defined in, if known. Can include things like class names, namespaces, etc.
pub in_app: bool, // We hard-require clients to tell us this?
#[serde(skip_serializing_if = "Option::is_none")]
pub resolved_name: Option<String>, // The name of the function, after symbolification
pub lang: String, // The language of the frame. Always known (I guess?)
pub resolved: bool, // Did we manage to resolve the frame?
#[serde(
serialize_with = "frame_error_serde::serialize",
skip_deserializing,
skip_serializing_if = "Option::is_none",
default
)]
pub resolve_failure: Option<FrameError>, // If we failed to resolve the frame, why?
#[serde(default)] // Defaults to false
pub synthetic: bool, // Some SDKs construct stack traces, or partially reconstruct them. This flag indicates whether the frame is synthetic or not.
#[serde(default)] // Defaults to false
pub suspicious: bool, // We mark some frames as suspicious if we think they might be from our own SDK code.
// Random extra/internal data we want to tag onto frames, e.g. the raw input. For debugging
// purposes, all production code should assume this is None
#[serde(skip_serializing_if = "Option::is_none")]
pub junk_drawer: Option<HashMap<String, Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code_variables: Option<Value>,
// The lines of code surrounding the frame ptr, if known. We skip serialising this because
// it should never go in clickhouse / be queried over, but we do store it in PG for
// use in the frontend
#[serde(skip)]
pub context: Option<Context>,
#[serde(skip)]
pub release: Option<ReleaseRecord>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Context {
pub before: Vec<ContextLine>,
pub line: ContextLine,
pub after: Vec<ContextLine>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct ContextLine {
pub number: u32,
pub line: String,
}
impl FingerprintComponent for Frame {
fn update(&self, fp: &mut FingerprintBuilder) {
let get_part = |s: &FrameId, p: Vec<&str>| FingerprintRecordPart::Frame {
raw_id: s.to_string(),
pieces: p.into_iter().map(String::from).collect(),
};
let mut included_pieces = Vec::new();
// Include source and module in the fingerprint either way
if let Some(source) = &self.source {
fp.update(source.as_bytes());
included_pieces.push("Source file name");
}
if let Some(module) = &self.module {
fp.update(module.as_bytes());
included_pieces.push("Module name");
}
// If we've resolved this frame, include function name, and then return
if let Some(resolved) = &self.resolved_name {
fp.update(resolved.as_bytes());
included_pieces.push("Resolved function name");
fp.add_part(get_part(&self.frame_id, included_pieces));
return;
}
// Otherwise, get more granular
fp.update(self.mangled_name.as_bytes());
included_pieces.push("Mangled function name");
if let Some(line) = self.line {
fp.update(line.to_string().as_bytes());
included_pieces.push("Line number");
}
if let Some(column) = self.column {
fp.update(column.to_string().as_bytes());
included_pieces.push("Column number");
}
fp.update(self.lang.as_bytes());
included_pieces.push("Language");
fp.add_part(get_part(&self.frame_id, included_pieces));
}
}
impl Frame {
pub fn add_junk<T>(&mut self, key: impl ToString, val: T) -> Result<(), serde_json::Error>
where
T: Serialize,
{
let key = key.to_string();
let val = serde_json::to_value(val)?;
self.junk_drawer
.get_or_insert_with(HashMap::new)
.insert(key, val);
Ok(())
}
}
impl ContextLine {
pub fn new(number: u32, line: impl ToString) -> Self {
let line = line.to_string();
// We limit context line length to 300 chars
let mut constrained: String = line.to_string().chars().take(300).collect();
if line.len() > constrained.len() {
constrained.push_str("...✂️");
}
Self {
number,
line: sanitize_string(constrained),
}
}
pub fn new_rel(baseline: u32, offset: i32, line: impl ToString) -> Self {
let line = line.to_string();
// We limit context line length to 300 chars
let mut constrained: String = line.to_string().chars().take(300).collect();
if line.len() > constrained.len() {
constrained.push_str("...✂️");
}
let number = if offset >= 0 {
baseline.saturating_add(offset as u32)
} else {
baseline.saturating_sub((-offset) as u32)
};
Self {
number,
line: sanitize_string(constrained),
}
}
}
impl std::fmt::Display for Frame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Frame {}:", self.frame_id)?;
// Function name and location
write!(
f,
" {} (from {}) ",
self.resolved_name.as_deref().unwrap_or("unknown"),
self.mangled_name
)?;
if let Some(source) = &self.source {
write!(f, "in {source}")?;
match (self.line, self.column) {
(Some(line), Some(column)) => writeln!(f, ":{line}:{column}"),
(Some(line), None) => writeln!(f, ":{line}"),
(None, Some(column)) => writeln!(f, ":?:{column}"),
(None, None) => writeln!(f),
}?;
} else {
writeln!(f, "in unknown location")?;
}
// Metadata
writeln!(f, " in_app: {}", self.in_app)?;
writeln!(f, " lang: {}", self.lang)?;
writeln!(f, " resolved: {}", self.resolved)?;
writeln!(
f,
" resolve_failure: {}",
self.resolve_failure
.as_ref()
.map(|e| e.to_string())
.as_deref()
.unwrap_or("no failure")
)?;
// Context
writeln!(f, " context:")?;
if let Some(context) = &self.context {
for line in &context.before {
writeln!(f, " {}: {}", line.number, line.line)?;
}
writeln!(f, " > {}: {}", context.line.number, context.line.line)?;
for line in &context.after {
writeln!(f, " {}: {}", line.number, line.line)?;
}
} else {
writeln!(f, " no context")?;
}
// Junk drawer
writeln!(f, " junk drawer:")?;
if let Some(junk) = &self.junk_drawer {
if junk.is_empty() {
writeln!(f, " no junk")?;
} else {
for (key, value) in junk {
writeln!(f, " {key}: {value}")?;
}
}
} else {
writeln!(f, " no junk")?;
}
Ok(())
}
}
impl From<Frame> for FrameData {
fn from(frame: Frame) -> Self {
FrameData {
frame_id: frame.frame_id.clone(),
synthetic: frame.synthetic,
resolved_name: frame.resolved_name,
mangled_name: frame.mangled_name,
source: frame.source,
resolved: frame.resolved,
in_app: frame.in_app,
line: frame.line,
column: frame.column,
lang: frame.lang,
code_variables: frame.code_variables,
}
}
}
mod frame_error_serde {
use super::FrameError;
use serde::Serializer;
pub fn serialize<S>(value: &Option<FrameError>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// skip_serializing_if = "Option::is_none" guarantees value is Some here,
// but we match exhaustively to satisfy the type checker.
match value {
Some(err) => serializer.serialize_str(&err.to_string()),
None => {
unreachable!("skip_serializing_if = Option::is_none prevents None reaching here")
}
}
}
}
fn to_vec<T, E>(item: Result<T, E>) -> Result<Vec<T>, E> {
item.map(|t| vec![t])
}
#[cfg(test)]
mod test {
use crate::frames::RawFrame;
#[test]
fn ensure_custom_frames_work() {
let data = r#"
{
"function": "Task.Supervised.invoke_mfa/2",
"module": "Task.Supervised",
"filename": "lib/task/supervised.ex",
"resolved": false,
"in_app": true,
"lineno": 105,
"platform": "custom",
"lang": "elixir"
}
"#;
let frame: RawFrame = serde_json::from_str(data).unwrap();
match frame {
RawFrame::Custom(_) => {}
_ => panic!("Expected a custom frame"),
}
}
}