diff --git a/CHANGELOG.md b/CHANGELOG.md index e5b088bb..56720396 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - fix: Preload injection path (#1243) - fix: Preload `contextIsolation` issues (#1244) +- fix: Include `sentry.origin` with auto-generated logs (#1241) ## 7.1.0 diff --git a/src/main/integrations/child-process.ts b/src/main/integrations/child-process.ts index 2b80caeb..d4156ede 100644 --- a/src/main/integrations/child-process.ts +++ b/src/main/integrations/child-process.ts @@ -103,6 +103,7 @@ export const childProcessIntegration = defineIntegration((userOptions: Partial = {}; + const attributes: Record = { + 'sentry.origin': 'auto.electron.events', + }; if (breadcrumb.data?.id) { attributes.id = breadcrumb.data.id; diff --git a/src/main/integrations/net-breadcrumbs.ts b/src/main/integrations/net-breadcrumbs.ts index dafacb72..6fb716f9 100644 --- a/src/main/integrations/net-breadcrumbs.ts +++ b/src/main/integrations/net-breadcrumbs.ts @@ -4,7 +4,6 @@ import { defineIntegration, fill, getBreadcrumbLogLevelFromHttpStatusCode, - getClient, getTraceData, LRUMap, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, @@ -89,6 +88,7 @@ type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod; function createWrappedRequestFactory( options: NetOptions, + enableLogs: boolean, tracePropagationTargets: TracePropagationTargets | undefined, ): WrappedRequestMethodFactory { // We're caching results so we don't have to recompute regexp every time we create a request. @@ -136,6 +136,57 @@ function createWrappedRequestFactory( return true; }; + /** + * Captures Breadcrumb based on provided request/response pair + */ + const addRequestBreadcrumb = ( + event: string, + method: string, + url: string, + req: ClientRequest, + res?: IncomingMessage, + ): void => { + const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode); + + addBreadcrumb( + { + type: 'http', + category: 'electron.net', + data: { + url, + method: method, + status_code: res?.statusCode, + }, + level, + }, + { + event, + request: req, + response: res, + }, + ); + + if (!enableLogs) { + return; + } + + const attributes = { + 'sentry.origin': 'auto.electron.net', + statusCode: res?.statusCode, + }; + + switch (level) { + case 'error': + logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes); + break; + case 'warning': + logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes); + break; + default: + logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes); + } + }; + return function wrappedRequestMethodFactory(originalRequestMethod: RequestMethod): RequestMethod { return function requestMethod(this: typeof electronNet, reqOptions: RequestOptions): ClientRequest { const { url, method } = parseOptions(reqOptions); @@ -191,64 +242,26 @@ function createWrappedRequestFactory( }; } -/** - * Captures Breadcrumb based on provided request/response pair - */ -function addRequestBreadcrumb( - event: string, - method: string, - url: string, - req: ClientRequest, - res?: IncomingMessage, -): void { - const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode); - addBreadcrumb( - { - type: 'http', - category: 'electron.net', - data: { - url, - method: method, - status_code: res?.statusCode, - }, - level, - }, - { - event, - request: req, - response: res, - }, - ); - - const attributes = { statusCode: res?.statusCode }; - - switch (level) { - case 'error': - logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes); - break; - case 'warning': - logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes); - break; - default: - logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes); - } -} - /** * Electron 'net' module integration */ export const electronNetIntegration = defineIntegration((options: NetOptions = {}) => { return { name: 'ElectronNet', - setup() { - const clientOptions = getClient()?.getOptions(); + setup(client) { + const clientOptions = client.getOptions(); + const enableLogs = !!clientOptions?.enableLogs; // No need to instrument if we don't want to track anything if (options.breadcrumbs === false && options.tracing === false) { return; } - fill(electronNet, 'request', createWrappedRequestFactory(options, clientOptions?.tracePropagationTargets)); + fill( + electronNet, + 'request', + createWrappedRequestFactory(options, enableLogs, clientOptions?.tracePropagationTargets), + ); }, }; }); diff --git a/test/e2e/test-apps/other/renderer-error/test.ts b/test/e2e/test-apps/other/renderer-error/test.ts index d22917c9..6864520e 100644 --- a/test/e2e/test-apps/other/renderer-error/test.ts +++ b/test/e2e/test-apps/other/renderer-error/test.ts @@ -23,6 +23,7 @@ electronTestRunner(__dirname, async (ctx) => { trace_id: UUID_MATCHER, severity_number: 9, attributes: { + 'sentry.origin': { value: 'auto.electron.events', type: 'string' }, 'sentry.release': { value: 'javascript-logs@1.0.0', type: 'string' }, 'sentry.environment': { value: 'development', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.electron', type: 'string' },