Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace paths in events #340

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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';
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();
Expand All @@ -28,33 +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;
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
autoSessionTracking: false,
enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged,
beforeSend: async (event, hint) => {
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();

Expand Down Expand Up @@ -83,7 +58,7 @@ if (!gotTheLock) {
try {
const comfyDesktopApp = await ComfyDesktopApp.create(appWindow);
await comfyDesktopApp.initialize();
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;
Expand Down
67 changes: 67 additions & 0 deletions src/services/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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) => {
this.filterEvent(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<string, unknown>;
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();
Loading