Skip to content

Commit 769920b

Browse files
committed
chore: Update Sentry events parsing
1 parent 68fc63e commit 769920b

File tree

1 file changed

+82
-82
lines changed

1 file changed

+82
-82
lines changed

postprocessing/src/sentry.rs

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ struct ExceptionData {
4747
}
4848
#[derive(Debug, serde::Deserialize)]
4949
struct RequestData {
50-
method: String,
50+
method: Option<String>,
5151
url: String,
5252
}
5353

5454
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
5555
struct ExceptionValue {
5656
r#type: String,
5757
value: Option<String>,
58-
stacktrace: Stacktrace,
58+
stacktrace: Option<Stacktrace>,
5959
}
6060

6161
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
@@ -80,15 +80,8 @@ struct Frame {
8080
#[serde(untagged)]
8181
#[serde(rename_all(deserialize = "snake_case"))]
8282
enum Metadata {
83-
Error {
84-
filename: String,
85-
function: String,
86-
r#type: String,
87-
value: String,
88-
},
89-
OnlyTitle {
90-
title: String,
91-
},
83+
Error { r#type: String, value: String },
84+
OnlyTitle { title: String },
9285
}
9386

9487
#[derive(Debug, serde::Serialize)]
@@ -166,90 +159,97 @@ fn should_be_skipped(event: &RawSentryEvent) -> bool {
166159
fn process_file(path: &PathBuf) -> Option<SentryEvent> {
167160
let file = File::open(path).expect("Can not open file");
168161
let raw_event: Result<RawSentryEvent, _> = serde_json::from_reader(file);
169-
if let Ok(raw_event) = raw_event {
170-
if should_be_skipped(&raw_event) {
171-
return None;
172-
}
173-
174-
let get_transaction = |tags: &[Tag]| -> Option<String> {
175-
for tag in tags {
176-
if tag.key == "transaction" {
177-
return Some(tag.value.clone());
178-
}
162+
match raw_event {
163+
Ok(raw_event) => {
164+
if should_be_skipped(&raw_event) {
165+
return None;
179166
}
180-
None
181-
};
182-
let transaction = get_transaction(&raw_event.tags);
183-
184-
let get_endpoint = |entries: &[Entry]| -> (Option<String>, Option<String>) {
185-
for entry in entries {
186-
match entry {
187-
Entry::Request {
188-
data: RequestData { method, url },
189-
} => {
190-
let path = Url::parse(url).expect("Invalid URL").path().to_owned();
191-
return (Some(method.clone()), Some(path));
167+
168+
let get_transaction = |tags: &[Tag]| -> Option<String> {
169+
for tag in tags {
170+
if tag.key == "transaction" {
171+
return Some(tag.value.clone());
192172
}
193-
Entry::Message | Entry::Breadcrumbs | Entry::Exception { .. } => continue,
194173
}
195-
}
196-
// It may be a Gunicorn-level error that is logged differently
197-
if let Some((_, path)) = raw_event.message.split_once("Error handling request ") {
174+
None
175+
};
176+
let transaction = get_transaction(&raw_event.tags);
177+
178+
let get_endpoint = |entries: &[Entry]| -> (Option<String>, Option<String>) {
198179
for entry in entries {
199180
match entry {
200-
// The only place where we can get the HTTP method is local variables in one of the
201-
// stackframes
202-
Entry::Exception {
203-
data: ExceptionData { values },
181+
Entry::Request {
182+
data: RequestData { method, url },
204183
} => {
205-
for ExceptionValue {
206-
stacktrace: Stacktrace { frames },
207-
..
208-
} in values
209-
{
210-
for Frame { vars, .. } in frames {
211-
if let Some(value) = vars["environ"]["REQUEST_METHOD"].as_str()
212-
{
213-
let method = &value[1..value.len() - 1];
214-
return (Some(method.to_owned()), Some(path.to_owned()));
184+
if let Some(method) = method {
185+
let path = Url::parse(url).expect("Invalid URL").path().to_owned();
186+
return (Some(method.clone()), Some(path));
187+
}
188+
}
189+
Entry::Message | Entry::Breadcrumbs | Entry::Exception { .. } => continue,
190+
}
191+
}
192+
// It may be a Gunicorn-level error that is logged differently
193+
if let Some((_, path)) = raw_event.message.split_once("Error handling request ") {
194+
for entry in entries {
195+
match entry {
196+
// The only place where we can get the HTTP method is local variables in one of the
197+
// stackframes
198+
Entry::Exception {
199+
data: ExceptionData { values },
200+
} => {
201+
for ExceptionValue { stacktrace, .. } in values {
202+
if let Some(Stacktrace { frames }) = stacktrace {
203+
for Frame { vars, .. } in frames {
204+
if let Some(value) =
205+
vars["environ"]["REQUEST_METHOD"].as_str()
206+
{
207+
let method = &value[1..value.len() - 1];
208+
return (
209+
Some(method.to_owned()),
210+
Some(path.to_owned()),
211+
);
212+
}
213+
}
215214
}
216215
}
217216
}
217+
Entry::Message | Entry::Breadcrumbs | Entry::Request { .. } => continue,
218218
}
219+
}
220+
};
221+
(None, None)
222+
};
223+
let (method, path) = get_endpoint(&raw_event.entries);
224+
225+
let exceptions = {
226+
let mut out = Vec::new();
227+
for entry in &raw_event.entries {
228+
match entry {
229+
Entry::Exception {
230+
data: ExceptionData { values },
231+
} => out.push(values.clone()),
219232
Entry::Message | Entry::Breadcrumbs | Entry::Request { .. } => continue,
220233
}
221234
}
235+
out
222236
};
223-
(None, None)
224-
};
225-
let (method, path) = get_endpoint(&raw_event.entries);
226-
227-
let exceptions = {
228-
let mut out = Vec::new();
229-
for entry in &raw_event.entries {
230-
match entry {
231-
Entry::Exception {
232-
data: ExceptionData { values },
233-
} => out.push(values.clone()),
234-
Entry::Message | Entry::Breadcrumbs | Entry::Request { .. } => continue,
235-
}
236-
}
237-
out
238-
};
239-
240-
Some(SentryEvent {
241-
event_id: raw_event.event_id,
242-
group_id: raw_event.group_id,
243-
title: raw_event.title,
244-
message: raw_event.message,
245-
culprit: raw_event.culprit,
246-
transaction,
247-
method,
248-
path,
249-
exceptions,
250-
metadata: raw_event.metadata,
251-
})
252-
} else {
253-
panic!("Invalid file: {:?}", path);
237+
238+
Some(SentryEvent {
239+
event_id: raw_event.event_id,
240+
group_id: raw_event.group_id,
241+
title: raw_event.title,
242+
message: raw_event.message,
243+
culprit: raw_event.culprit,
244+
transaction,
245+
method,
246+
path,
247+
exceptions,
248+
metadata: raw_event.metadata,
249+
})
250+
}
251+
Err(error) => {
252+
panic!("Invalid file: {:?} ({})", path, error);
253+
}
254254
}
255255
}

0 commit comments

Comments
 (0)