From 58bcd4a3f2f197251b6c362f2c23711d45a4d7b1 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Sun, 24 Nov 2024 12:27:17 +0000 Subject: [PATCH 1/4] Replace paths in events --- src/main.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index eb307bc4..9a3d1842 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,11 +31,41 @@ app.on('window-all-closed', () => { * The `alwaysSendCrashReports` variable is used to determine if crash reports should be sent. */ let alwaysSendCrashReports = false; + +// Used to filter out paths from strings in sentry events +let basePath: string | undefined; +const filterEvent = (obj: unknown) => { + if (!obj || !basePath) return obj; + + if (typeof obj === 'string') { + return obj.replaceAll(basePath, '[basePath]'); + } + + try { + if (typeof obj === 'object') { + for (const k in obj) { + try { + const record = obj as Record; + record[k] = filterEvent(record[k]); + } catch (error) { + // Failed to read/write key + } + } + } + } catch (error) { + // Failed to enumerate keys + } + + return obj; +}; + Sentry.init({ dsn: SENTRY_URL_ENDPOINT, autoSessionTracking: false, enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged, - beforeSend: async (event, hint) => { + beforeSend: async (event) => { + filterEvent(event); + if (event.extra?.comfyUIExecutionError || alwaysSendCrashReports) { return event; } @@ -83,6 +113,7 @@ if (!gotTheLock) { try { const comfyDesktopApp = await ComfyDesktopApp.create(appWindow); await comfyDesktopApp.initialize(); + basePath = comfyDesktopApp.basePath; alwaysSendCrashReports = comfyDesktopApp.comfySettings.get('Comfy-Desktop.SendStatistics'); const useExternalServer = process.env.USE_EXTERNAL_SERVER === 'true'; From 535edbca07c5e0a773c9b9e871c6a3a5cb92cfd1 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Sun, 24 Nov 2024 12:31:48 +0000 Subject: [PATCH 2/4] Format --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 9a3d1842..2be74f3e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,7 +65,7 @@ Sentry.init({ enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged, beforeSend: async (event) => { filterEvent(event); - + if (event.extra?.comfyUIExecutionError || alwaysSendCrashReports) { return event; } From 23dd579852e303243ab9020d815ce4c9b3c23854 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:32:48 +0000 Subject: [PATCH 3/4] Move sentry code to own file --- src/main.ts | 62 ++----------------------------------- src/services/sentry.ts | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 59 deletions(-) create mode 100644 src/services/sentry.ts diff --git a/src/main.ts b/src/main.ts index 2be74f3e..234490ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,6 @@ import { IPC_CHANNELS, DEFAULT_SERVER_ARGS, ProgressStatus, SENTRY_URL_ENDPOINT } from './constants'; import { app, dialog, ipcMain } from 'electron'; import log from 'electron-log/main'; -import * as Sentry from '@sentry/electron/main'; import { findAvailablePort } from './utils'; import dotenv from 'dotenv'; import { AppWindow } from './main-process/appWindow'; @@ -9,6 +8,7 @@ import { PathHandlers } from './handlers/pathHandlers'; import { AppInfoHandlers } from './handlers/appInfoHandlers'; import { ComfyDesktopApp } from './main-process/comfyDesktopApp'; import { LevelOption } from 'electron-log'; +import SentryLogging from './services/sentry'; dotenv.config(); log.initialize(); @@ -28,63 +28,8 @@ app.on('window-all-closed', () => { /** * Sentry needs to be initialized at the top level. - * The `alwaysSendCrashReports` variable is used to determine if crash reports should be sent. */ -let alwaysSendCrashReports = false; - -// Used to filter out paths from strings in sentry events -let basePath: string | undefined; -const filterEvent = (obj: unknown) => { - if (!obj || !basePath) return obj; - - if (typeof obj === 'string') { - return obj.replaceAll(basePath, '[basePath]'); - } - - try { - if (typeof obj === 'object') { - for (const k in obj) { - try { - const record = obj as Record; - record[k] = filterEvent(record[k]); - } catch (error) { - // Failed to read/write key - } - } - } - } catch (error) { - // Failed to enumerate keys - } - - return obj; -}; - -Sentry.init({ - dsn: SENTRY_URL_ENDPOINT, - autoSessionTracking: false, - enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged, - beforeSend: async (event) => { - filterEvent(event); - - if (event.extra?.comfyUIExecutionError || alwaysSendCrashReports) { - return event; - } - - const { response } = await dialog.showMessageBox({ - title: 'Send Crash Statistics', - message: `Would you like to send crash statistics to the team?`, - buttons: ['Always send crash reports', 'Do not send crash report'], - }); - - return response === 0 ? event : null; - }, - integrations: [ - Sentry.childProcessIntegration({ - breadcrumbs: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'], - events: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'], - }), - ], -}); +SentryLogging.init(); const gotTheLock = app.requestSingleInstanceLock(); @@ -113,8 +58,7 @@ if (!gotTheLock) { try { const comfyDesktopApp = await ComfyDesktopApp.create(appWindow); await comfyDesktopApp.initialize(); - basePath = comfyDesktopApp.basePath; - alwaysSendCrashReports = comfyDesktopApp.comfySettings.get('Comfy-Desktop.SendStatistics'); + SentryLogging.comfyDesktopApp = comfyDesktopApp; const useExternalServer = process.env.USE_EXTERNAL_SERVER === 'true'; const host = process.env.COMFY_HOST || DEFAULT_SERVER_ARGS.host; diff --git a/src/services/sentry.ts b/src/services/sentry.ts new file mode 100644 index 00000000..f5447cd8 --- /dev/null +++ b/src/services/sentry.ts @@ -0,0 +1,69 @@ +import * as Sentry from '@sentry/electron/main'; +import { SENTRY_URL_ENDPOINT } from '../constants'; +import { ComfyDesktopApp } from '../main-process/comfyDesktopApp'; +import { app, dialog } from 'electron'; + +class SentryLogging { + comfyDesktopApp: ComfyDesktopApp | undefined; + + init() { + Sentry.init({ + dsn: SENTRY_URL_ENDPOINT, + autoSessionTracking: false, + enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged, + beforeSend: async (event) => { + console.log(event) + this.filterEvent(event); + console.log(event) + + if ( + event.extra?.comfyUIExecutionError || + this.comfyDesktopApp?.comfySettings.get('Comfy-Desktop.SendStatistics') + ) { + return event; + } + + const { response } = await dialog.showMessageBox({ + title: 'Send Crash Statistics', + message: `Would you like to send crash statistics to the team?`, + buttons: ['Always send crash reports', 'Do not send crash report'], + }); + + return response === 0 ? event : null; + }, + integrations: [ + Sentry.childProcessIntegration({ + breadcrumbs: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'], + events: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'], + }), + ], + }); + } + + private filterEvent(obj: unknown) { + if (!obj || !this.comfyDesktopApp?.basePath) return obj; + + if (typeof obj === 'string') { + return obj.replaceAll(this.comfyDesktopApp.basePath, '[basePath]'); + } + + try { + if (typeof obj === 'object') { + for (const k in obj) { + try { + const record = obj as Record; + record[k] = this.filterEvent(record[k]); + } catch (error) { + // Failed to read/write key + } + } + } + } catch (error) { + // Failed to enumerate keys + } + + return obj; + } +} + +export default new SentryLogging(); From 849a3da006d8ba8e5cec0b322287edf7997db67e Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:33:26 +0000 Subject: [PATCH 4/4] Remove logs --- src/services/sentry.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/services/sentry.ts b/src/services/sentry.ts index f5447cd8..9c666e50 100644 --- a/src/services/sentry.ts +++ b/src/services/sentry.ts @@ -12,9 +12,7 @@ class SentryLogging { autoSessionTracking: false, enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged, beforeSend: async (event) => { - console.log(event) this.filterEvent(event); - console.log(event) if ( event.extra?.comfyUIExecutionError ||