Skip to content
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
1 change: 0 additions & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare const __COMFYUI_FRONTEND_VERSION__: string
declare const __COMFYUI_FRONTEND_COMMIT__: string
declare const __SENTRY_ENABLED__: boolean
declare const __SENTRY_DSN__: string
declare const __ALGOLIA_APP_ID__: string
declare const __ALGOLIA_API_KEY__: string
Expand Down
2 changes: 0 additions & 2 deletions scripts/vite-define-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

type GlobalWithDefines = typeof globalThis & {
__COMFYUI_FRONTEND_VERSION__: string
__SENTRY_ENABLED__: boolean
__SENTRY_DSN__: string
__ALGOLIA_APP_ID__: string
__ALGOLIA_API_KEY__: string
Expand All @@ -20,7 +19,6 @@ const globalWithDefines = globalThis as GlobalWithDefines
// Set default values for Playwright test environment
globalWithDefines.__COMFYUI_FRONTEND_VERSION__ =
process.env.npm_package_version || '1.0.0'
globalWithDefines.__SENTRY_ENABLED__ = false
globalWithDefines.__SENTRY_DSN__ = ''
globalWithDefines.__ALGOLIA_APP_ID__ = ''
globalWithDefines.__ALGOLIA_API_KEY__ = ''
Expand Down
24 changes: 10 additions & 14 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
</template>

<script setup lang="ts">
import { captureException } from '@sentry/vue'
import BlockUI from 'primevue/blockui'
import { computed, onMounted, watch } from 'vue'

import GlobalDialog from '@/components/dialog/GlobalDialog.vue'
import config from '@/config'
import { isDesktop } from '@/platform/distribution/types'
import { reportError } from '@/platform/telemetry/reportError'
import { app } from '@/scripts/app'
import { useWorkspaceStore } from '@/stores/workspaceStore'
import { electronAPI } from '@/utils/envUtil'
Expand Down Expand Up @@ -49,11 +49,9 @@ function handleResourceError(url: string, tagName: string) {
console.error('[resource:loadError]', { url, tagName })

if (__DISTRIBUTION__ === 'cloud') {
captureException(new Error(`Resource load failed: ${url}`), {
tags: {
error_type: 'resource_load_error',
tag_name: tagName
}
reportError(new Error(`Resource load failed: ${url}`), {
errorType: 'resource_load_error',
tags: { tag_name: tagName }
})
}
}
Expand All @@ -77,18 +75,16 @@ onMounted(() => {
message: info.message
})
if (__DISTRIBUTION__ === 'cloud') {
captureException(event.payload, {
reportError(event.payload, {
errorType: 'vite_preload_error',
tags: {
error_type: 'vite_preload_error',
file_type: info.fileType,
chunk_name: info.chunkName ?? undefined
},
contexts: {
preload: {
url: info.url,
fileType: info.fileType,
chunkName: info.chunkName
}
context: {
url: info.url,
fileType: info.fileType,
chunkName: info.chunkName
}
})
}
Expand Down
5 changes: 4 additions & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
if (__DISTRIBUTION__ === 'cloud') {
const { initDatadogRum } = await import('@/platform/telemetry/initDatadogRum')
void initDatadogRum().catch(() => {})
const { flushErrorReports } = await import('@/platform/telemetry/reportError')
void initDatadogRum()
.then(flushErrorReports)
.catch(() => {})
}

await import('./main')
Expand Down
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
remoteConfig
} from '@/platform/remoteConfig/remoteConfig'
import { syncHostUserIdWithFirebaseAuth } from '@/platform/telemetry/hostUserIdSync'
import { flushErrorReports } from '@/platform/telemetry/reportError'
import '@/lib/litegraph/public/css/litegraph.css'
import router from '@/router'
import { isDesktop, isNightly } from '@/platform/distribution/types'
Expand Down Expand Up @@ -67,10 +68,16 @@ const sentryDsn = isCloud
? configValueOrDefault(remoteConfig.value, 'sentry_dsn', __SENTRY_DSN__)
: __SENTRY_DSN__

// __SENTRY_ENABLED__ is baked from the *build machine's* SENTRY_DSN, but cloud
// resolves its DSN at runtime from remote config. Trusting the build-time flag
// alone leaves every capture in the app silently inert whenever a cloud build
// runs without the env var, however valid the runtime DSN turns out to be.
const sentryEnabled = !import.meta.env.DEV && !!sentryDsn

Sentry.init({
app,
dsn: sentryDsn,
enabled: __SENTRY_ENABLED__,
enabled: sentryEnabled,
release: __COMFYUI_FRONTEND_VERSION__,
normalizeDepth: 8,
tracesSampleRate: isCloud ? 1.0 : 0,
Expand All @@ -92,6 +99,9 @@ Sentry.init({
defaultIntegrations: false
})
})

flushErrorReports()

// Assertion reporter receives pre-formatted messages (with "[Assertion failed]: " prefix).
// Strings here are intentionally not i18n'd: they're developer/nightly diagnostics,
// not user-facing in stable releases.
Expand Down
42 changes: 14 additions & 28 deletions src/platform/cloud/onboarding/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from '@sentry/vue'
import { isEmpty } from 'es-toolkit/compat'

import { reportError } from '@/platform/telemetry/reportError'
import { api } from '@/scripts/api'
import { toError } from '@/utils/errorUtil'

Expand All @@ -10,9 +11,6 @@ interface UserCloudStatus {

const ONBOARDING_SURVEY_KEY = 'onboarding_survey'

/**
* Helper function to capture API errors with Sentry
*/
function captureApiError(
error: Error,
endpoint: string,
Expand All @@ -21,25 +19,15 @@ function captureApiError(
operation?: string,
extraContext?: Record<string, unknown>
) {
const tags: Record<string, string | number> = {
api_endpoint: endpoint,
error_type: errorType
}

if (httpStatus !== undefined) {
tags.http_status = httpStatus
}

if (operation) {
tags.operation = operation
}

const sentryOptions: Sentry.ExclusiveEventHintOrCaptureContext = {
tags,
extra: extraContext ? { ...extraContext } : undefined
}

Sentry.captureException(error, sentryOptions)
reportError(error, {
errorType,
tags: {
api_endpoint: endpoint,
http_status: httpStatus,
operation
},
context: extraContext
})
}

/**
Expand Down Expand Up @@ -119,12 +107,10 @@ export async function getSurveyCompletedStatus(): Promise<boolean> {
return !isEmpty(data.value)
} catch (error) {
// Network/parse failure: same fail-safe policy as a non-ok response.
Sentry.captureException(error, {
tags: {
api_endpoint: '/settings/{key}',
error_type: 'network_error'
},
extra: {
reportError(error, {
errorType: 'network_error',
tags: { api_endpoint: '/settings/{key}' },
context: {
route_template: '/settings/{key}',
route_actual: `/settings/${ONBOARDING_SURVEY_KEY}`
},
Expand Down
167 changes: 167 additions & 0 deletions src/platform/telemetry/reportError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

const captureException = vi.fn()
const isEnabled = vi.fn()
const addError = vi.fn()
const getInitConfiguration = vi.fn()

vi.mock('@sentry/vue', () => ({
captureException: (...args: unknown[]) => captureException(...args),
isEnabled: () => isEnabled()
}))

vi.mock('@datadog/browser-rum', () => ({
datadogRum: {
addError: (...args: unknown[]) => addError(...args),
getInitConfiguration: () => getInitConfiguration()
}
}))

async function loadReportError() {
vi.resetModules()
return import('./reportError')
}

const sentryLive = (live: boolean) => isEnabled.mockReturnValue(live)
const datadogLive = (live: boolean) =>
getInitConfiguration.mockReturnValue(live ? {} : undefined)

describe('reportError', () => {
beforeEach(() => {
vi.clearAllMocks()
sentryLive(true)
datadogLive(true)
})

it('reaches both Sentry and Datadog from a single call', async () => {
const { reportError } = await loadReportError()
const error = new Error('boom')

reportError(error, {
errorType: 'workspace_auth_gate_initialization_failure'
})

expect(captureException).toHaveBeenCalledWith(
error,
expect.objectContaining({
tags: expect.objectContaining({
error_type: 'workspace_auth_gate_initialization_failure'
})
})
)
expect(addError).toHaveBeenCalledWith(
error,
expect.objectContaining({
error_type: 'workspace_auth_gate_initialization_failure'
})
)
})

it('still reports to Datadog when Sentry is inert', async () => {
sentryLive(false)
const { reportError } = await loadReportError()

reportError(new Error('boom'), { errorType: 'bootstrap_auth_wait_timeout' })

expect(captureException).not.toHaveBeenCalled()
expect(addError).toHaveBeenCalledOnce()
})

it('buffers reports raised before any sink is live, then flushes them', async () => {
sentryLive(false)
datadogLive(false)
const { reportError, flushErrorReports } = await loadReportError()

reportError(new Error('early'), { errorType: 'resource_load_error' })
expect(addError).not.toHaveBeenCalled()

datadogLive(true)
flushErrorReports()

expect(addError).toHaveBeenCalledWith(
expect.objectContaining({ message: 'early' }),
expect.objectContaining({ error_type: 'resource_load_error' })
)
})

it('does not replay a buffered report twice', async () => {
sentryLive(false)
datadogLive(false)
const { reportError, flushErrorReports } = await loadReportError()

reportError(new Error('early'), { errorType: 'resource_load_error' })

datadogLive(true)
flushErrorReports()
flushErrorReports()

expect(addError).toHaveBeenCalledOnce()
})

it('bounds the buffer so a boot-time error storm cannot grow without limit', async () => {
sentryLive(false)
datadogLive(false)
const { reportError, flushErrorReports } = await loadReportError()

for (let i = 0; i < 200; i++) {
reportError(new Error(`e${i}`), { errorType: 'resource_load_error' })
}

datadogLive(true)
flushErrorReports()

expect(addError.mock.calls.length).toBeLessThanOrEqual(25)
})

it('normalizes a non-Error cause', async () => {
const { reportError } = await loadReportError()

reportError('just a string', { errorType: 'bootstrap_auth_wait_timeout' })

expect(addError).toHaveBeenCalledWith(
expect.objectContaining({ message: 'just a string' }),
expect.anything()
)
})

it('drops undefined tag values rather than forwarding them', async () => {
const { reportError } = await loadReportError()

reportError(new Error('boom'), {
errorType: 'http_error',
tags: { api_endpoint: '/settings/{key}', http_status: undefined }
})

const [, context] = addError.mock.calls[0]
expect(context).not.toHaveProperty('http_status')
expect(context).toMatchObject({ api_endpoint: '/settings/{key}' })
})

it('does not throw out of flushErrorReports when a sink throws', async () => {
sentryLive(false)
datadogLive(false)
const { reportError, flushErrorReports } = await loadReportError()

reportError(new Error('early'), { errorType: 'resource_load_error' })

datadogLive(true)
addError.mockImplementation(() => {
throw new Error('datadog exploded')
})

expect(() => flushErrorReports()).not.toThrow()
})

it('does not throw when a sink throws', async () => {
captureException.mockImplementation(() => {
throw new Error('sentry exploded')
})
const { reportError } = await loadReportError()

expect(() =>
reportError(new Error('boom'), {
errorType: 'bootstrap_auth_wait_timeout'
})
).not.toThrow()
})
})
Loading
Loading