From d1a32796a1b6c047951e6a659abce8e7d71f11ec Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 2 Aug 2023 12:06:58 +0200 Subject: [PATCH] Store the entire default event --- .../integrations/sentry-minidump/index.ts | 40 ++++++++++--------- src/main/store.ts | 15 +------ test/unit/store.test.ts | 6 ++- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/main/integrations/sentry-minidump/index.ts b/src/main/integrations/sentry-minidump/index.ts index 8ccb41620..02b934713 100644 --- a/src/main/integrations/sentry-minidump/index.ts +++ b/src/main/integrations/sentry-minidump/index.ts @@ -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 */ @@ -55,8 +54,6 @@ export class SentryMinidump implements Integration { this._scopeStore = new BufferedWriteStore(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 @@ -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, }); }); }); @@ -211,7 +208,7 @@ export class SentryMinidump implements Integration { * * Returns true if one or more minidumps were found */ - private async _sendNativeCrashes(event: Event): Promise { + private async _sendNativeCrashes(eventIn: Event): Promise { // 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: @@ -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'); } @@ -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', diff --git a/src/main/store.ts b/src/main/store.ts index 6400ddf2a..bffb0f3c1 100644 --- a/src/main/store.ts +++ b/src/main/store.ts @@ -127,15 +127,8 @@ export class BufferedWriteStore extends Store { * @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); } @@ -143,12 +136,6 @@ export class BufferedWriteStore extends Store { public override async set(data: T): Promise { 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, diff --git a/test/unit/store.test.ts b/test/unit/store.test.ts index e4b369565..4c82cf366 100644 --- a/test/unit/store.test.ts +++ b/test/unit/store.test.ts @@ -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');