crash: beforeSend needs to add contexts to the event directly

This commit is contained in:
Mitchell Hashimoto
2024-09-02 09:55:47 -07:00
parent d499f7795b
commit d8cc19612f
2 changed files with 33 additions and 9 deletions

View File

@ -53,7 +53,7 @@ pub const Value = struct {
}
/// sentry_value_set_by_key_n
pub fn setByKey(self: Value, key: []const u8, value: Value) void {
pub fn set(self: Value, key: []const u8, value: Value) void {
_ = c.sentry_value_set_by_key_n(
self.value,
key.ptr,
@ -61,4 +61,15 @@ pub const Value = struct {
value.value,
);
}
/// sentry_value_set_by_key_n
pub fn get(self: Value, key: []const u8) ?Value {
const val: Value = .{ .value = c.sentry_value_get_by_key_n(
self.value,
key.ptr,
key.len,
) };
if (val.isNull()) return null;
return val;
}
};

View File

@ -143,7 +143,19 @@ fn beforeSend(
// If we don't have thread state we can't reliably determine
// metadata such as surface dimensions. In the future we can probably
// drop full app state (all surfaces, all windows, etc.).
const thr_state = thread_state orelse return event_val;
const thr_state = thread_state orelse {
log.debug("no thread state, skipping crash metadata", .{});
return event_val;
};
// Get our event contexts. At this point Sentry has already merged
// all the contexts so we should have this key. If not, we create it.
const event: sentry.Value = .{ .value = event_val };
const contexts = event.get("contexts") orelse contexts: {
const obj = sentry.Value.initObject();
event.set("contexts", obj);
break :contexts obj;
};
// Read the surface data. This is likely unsafe because on a crash
// other threads can continue running. We don't have race-safe way to
@ -152,31 +164,32 @@ fn beforeSend(
const obj = sentry.Value.initObject();
errdefer obj.decref();
const surface = thr_state.surface;
obj.setByKey(
obj.set(
"screen-width",
sentry.Value.initInt32(std.math.cast(i32, surface.screen_size.width) orelse -1),
);
obj.setByKey(
obj.set(
"screen-height",
sentry.Value.initInt32(std.math.cast(i32, surface.screen_size.height) orelse -1),
);
obj.setByKey(
obj.set(
"grid-columns",
sentry.Value.initInt32(std.math.cast(i32, surface.grid_size.columns) orelse -1),
);
obj.setByKey(
obj.set(
"grid-rows",
sentry.Value.initInt32(std.math.cast(i32, surface.grid_size.rows) orelse -1),
);
obj.setByKey(
obj.set(
"cell-width",
sentry.Value.initInt32(std.math.cast(i32, surface.cell_size.width) orelse -1),
);
obj.setByKey(
obj.set(
"cell-height",
sentry.Value.initInt32(std.math.cast(i32, surface.cell_size.height) orelse -1),
);
sentry.setContext("dimensions", obj);
contexts.set("dimensions", obj);
}
return event_val;