Skip to content

Commit a9c287b

Browse files
authored
fix: Include sentry.origin with auto-generated logs (#1241)
1 parent 406c802 commit a9c287b

5 files changed

Lines changed: 67 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- fix: Preload injection path (#1243)
88
- fix: Preload `contextIsolation` issues (#1244)
9+
- fix: Include `sentry.origin` with auto-generated logs (#1241)
910

1011
## 7.1.0
1112

src/main/integrations/child-process.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const childProcessIntegration = defineIntegration((userOptions: Partial<O
103103

104104
if (enableLogs) {
105105
log(messageFmt, {
106+
'sentry.origin': 'auto.electron.child-process',
106107
exitCode: details.exitCode,
107108
name: details.name,
108109
serviceName: details.serviceName,
@@ -133,6 +134,7 @@ export const childProcessIntegration = defineIntegration((userOptions: Partial<O
133134

134135
if (enableLogs) {
135136
log(messageFmt, {
137+
'sentry.origin': 'auto.electron.child-process',
136138
exitCode: details.exitCode,
137139
});
138140
}

src/main/integrations/electron-breadcrumbs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ export const electronBreadcrumbsIntegration = defineIntegration(
144144

145145
addBreadcrumb(breadcrumb);
146146

147-
const attributes: Record<string, unknown> = {};
147+
const attributes: Record<string, unknown> = {
148+
'sentry.origin': 'auto.electron.events',
149+
};
148150

149151
if (breadcrumb.data?.id) {
150152
attributes.id = breadcrumb.data.id;

src/main/integrations/net-breadcrumbs.ts

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
defineIntegration,
55
fill,
66
getBreadcrumbLogLevelFromHttpStatusCode,
7-
getClient,
87
getTraceData,
98
LRUMap,
109
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -89,6 +88,7 @@ type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod;
8988

9089
function createWrappedRequestFactory(
9190
options: NetOptions,
91+
enableLogs: boolean,
9292
tracePropagationTargets: TracePropagationTargets | undefined,
9393
): WrappedRequestMethodFactory {
9494
// We're caching results so we don't have to recompute regexp every time we create a request.
@@ -136,6 +136,57 @@ function createWrappedRequestFactory(
136136
return true;
137137
};
138138

139+
/**
140+
* Captures Breadcrumb based on provided request/response pair
141+
*/
142+
const addRequestBreadcrumb = (
143+
event: string,
144+
method: string,
145+
url: string,
146+
req: ClientRequest,
147+
res?: IncomingMessage,
148+
): void => {
149+
const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode);
150+
151+
addBreadcrumb(
152+
{
153+
type: 'http',
154+
category: 'electron.net',
155+
data: {
156+
url,
157+
method: method,
158+
status_code: res?.statusCode,
159+
},
160+
level,
161+
},
162+
{
163+
event,
164+
request: req,
165+
response: res,
166+
},
167+
);
168+
169+
if (!enableLogs) {
170+
return;
171+
}
172+
173+
const attributes = {
174+
'sentry.origin': 'auto.electron.net',
175+
statusCode: res?.statusCode,
176+
};
177+
178+
switch (level) {
179+
case 'error':
180+
logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes);
181+
break;
182+
case 'warning':
183+
logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes);
184+
break;
185+
default:
186+
logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes);
187+
}
188+
};
189+
139190
return function wrappedRequestMethodFactory(originalRequestMethod: RequestMethod): RequestMethod {
140191
return function requestMethod(this: typeof electronNet, reqOptions: RequestOptions): ClientRequest {
141192
const { url, method } = parseOptions(reqOptions);
@@ -191,64 +242,26 @@ function createWrappedRequestFactory(
191242
};
192243
}
193244

194-
/**
195-
* Captures Breadcrumb based on provided request/response pair
196-
*/
197-
function addRequestBreadcrumb(
198-
event: string,
199-
method: string,
200-
url: string,
201-
req: ClientRequest,
202-
res?: IncomingMessage,
203-
): void {
204-
const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode);
205-
addBreadcrumb(
206-
{
207-
type: 'http',
208-
category: 'electron.net',
209-
data: {
210-
url,
211-
method: method,
212-
status_code: res?.statusCode,
213-
},
214-
level,
215-
},
216-
{
217-
event,
218-
request: req,
219-
response: res,
220-
},
221-
);
222-
223-
const attributes = { statusCode: res?.statusCode };
224-
225-
switch (level) {
226-
case 'error':
227-
logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes);
228-
break;
229-
case 'warning':
230-
logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes);
231-
break;
232-
default:
233-
logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes);
234-
}
235-
}
236-
237245
/**
238246
* Electron 'net' module integration
239247
*/
240248
export const electronNetIntegration = defineIntegration((options: NetOptions = {}) => {
241249
return {
242250
name: 'ElectronNet',
243-
setup() {
244-
const clientOptions = getClient()?.getOptions();
251+
setup(client) {
252+
const clientOptions = client.getOptions();
253+
const enableLogs = !!clientOptions?.enableLogs;
245254

246255
// No need to instrument if we don't want to track anything
247256
if (options.breadcrumbs === false && options.tracing === false) {
248257
return;
249258
}
250259

251-
fill(electronNet, 'request', createWrappedRequestFactory(options, clientOptions?.tracePropagationTargets));
260+
fill(
261+
electronNet,
262+
'request',
263+
createWrappedRequestFactory(options, enableLogs, clientOptions?.tracePropagationTargets),
264+
);
252265
},
253266
};
254267
});

test/e2e/test-apps/other/renderer-error/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ electronTestRunner(__dirname, async (ctx) => {
2323
trace_id: UUID_MATCHER,
2424
severity_number: 9,
2525
attributes: {
26+
'sentry.origin': { value: 'auto.electron.events', type: 'string' },
2627
'sentry.release': { value: 'javascript-logs@1.0.0', type: 'string' },
2728
'sentry.environment': { value: 'development', type: 'string' },
2829
'sentry.sdk.name': { value: 'sentry.javascript.electron', type: 'string' },

0 commit comments

Comments
 (0)