Skip to content

Commit

Permalink
Store the entire default event
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Aug 2, 2023
1 parent 2d553da commit d1a3279
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
40 changes: 21 additions & 19 deletions src/main/integrations/sentry-minidump/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { deleteMinidump, getMinidumpLoader, MinidumpLoader } from './minidump-lo

interface PreviousRun {
scope: Scope;
release: string;
environment: string;
event?: Event;
}

/** Sends minidumps via the Sentry uploader */
Expand Down Expand Up @@ -55,8 +54,6 @@ export class SentryMinidump implements Integration {

this._scopeStore = new BufferedWriteStore<PreviousRun>(sentryCachePath, 'scope_v3', {
scope: new Scope(),
release: currentRelease,
environment: currentEnvironment,
});

// We need to store the scope in a variable here so it can be attached to minidumps
Expand Down Expand Up @@ -195,11 +192,11 @@ export class SentryMinidump implements Integration {

// Since the initial scope read is async, we need to ensure that any writes do not beat that
// https://github.com/getsentry/sentry-electron/issues/585
setImmediate(() => {
setImmediate(async () => {
const event = await getEventDefaults(currentRelease, currentEnvironment);
void this._scopeStore?.set({
scope,
release: currentRelease,
environment: currentEnvironment,
event,
});
});
});
Expand All @@ -211,7 +208,7 @@ export class SentryMinidump implements Integration {
*
* Returns true if one or more minidumps were found
*/
private async _sendNativeCrashes(event: Event): Promise<boolean> {
private async _sendNativeCrashes(eventIn: Event): Promise<boolean> {
// Whenever we are called, assume that the crashes we are going to load down
// below have occurred recently. This means, we can use the same event data
// for all minidumps that we load now. There are two conditions:
Expand All @@ -224,6 +221,8 @@ export class SentryMinidump implements Integration {
// about it. Just use the breadcrumbs and context information we have
// right now and hope that the delay was not too long.

let event: Event | null = eventIn;

if (this._minidumpLoader === undefined) {
throw new SentryError('Invariant violation: Native crashes not enabled');
}
Expand All @@ -248,28 +247,31 @@ export class SentryMinidump implements Integration {
return false;
}

const previousRun = await this._scopeLastRun;
if (event?.tags?.['event.process'] === 'browser') {
const previousRun = await this._scopeLastRun;

const storedScope = Scope.clone(previousRun?.scope);
event = await storedScope.applyToEvent(event);

const storedScope = Scope.clone(previousRun?.scope);
let newEvent = await storedScope.applyToEvent(event);
if (event && previousRun) {
event.release = previousRun.event?.release;
event.environment = previousRun.event?.environment;
event.contexts = previousRun.event?.contexts;
}
}

const hubScope = hub.getScope();
newEvent = hubScope ? await hubScope.applyToEvent(event) : event;
event = hubScope && event ? await hubScope.applyToEvent(event) : event;

if (!newEvent) {
if (!event) {
return false;
}

if (previousRun) {
newEvent.release = previousRun.release;
newEvent.environment = previousRun.environment;
}

for (const minidump of minidumps) {
const data = await minidump.load();

if (data) {
captureEvent(newEvent, {
captureEvent(event, {
attachments: [
{
attachmentType: 'event.minidump',
Expand Down
15 changes: 1 addition & 14 deletions src/main/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,15 @@ export class BufferedWriteStore<T> extends Store<T> {
* @param id A unique filename to store this data.
* @param initial An initial value to initialize data with.
* @param throttleTime The minimum time between writes
* @param immediateFirstWrite Whether to write the first set immediately
*/
public constructor(
path: string,
id: string,
initial: T,
private readonly _throttleTime: number = 500,
private _immediateFirstWrite = true,
) {
public constructor(path: string, id: string, initial: T, private readonly _throttleTime: number = 500) {
super(path, id, initial);
}

/** @inheritdoc */
public override async set(data: T): Promise<void> {
this._data = data;

// If this is the first write, we write immediately
if (this._immediateFirstWrite) {
this._immediateFirstWrite = false;
return super.set(data);
}

this._pendingWrite = {
// We overwrite the data for the pending write so that the latest data is written in the next flush
data,
Expand Down
6 changes: 5 additions & 1 deletion test/unit/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ describe('Store', () => {
await expectFilesInDirectory(tempDir.name, 0);

await store.set({ num: 990, str: 'just a string' });
// Initial write should be immediate
// File should not be written after 100ms!
await delay(100);
await expectFilesInDirectory(tempDir.name, 0);
// Should have been written after 1 more second
await delay(1_000);
await expectFilesInDirectory(tempDir.name, 1);

const contents = await readFileAsync(join(tempDir.name, 'test-store.json'), 'utf-8');
Expand Down

0 comments on commit d1a3279

Please sign in to comment.