Skip to content

Commit 00e74e4

Browse files
authored
fix(electron): skip IPC patching in processes without ipcMain (#8798)
1 parent f39f25f commit 00e74e4

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

packages/datadog-instrumentations/src/electron.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ addHook({ name: 'electron', versions: ['>=37.0.0'] }, electron => {
218218
wrap(ipcRenderer, 'removeAllListeners', createWrapRemoveAllListeners(listeners))
219219

220220
ipcRenderer.send('datadog:apm:renderer:patched')
221-
} else {
221+
} else if (ipcMain) {
222222
wrap(ipcMain, 'addListener', createWrapAddListener(mainReceiveCh, listeners))
223223
wrap(ipcMain, 'handle', createWrapAddListener(mainHandleCh, handlers))
224224
wrap(ipcMain, 'handleOnce', createWrapAddListener(mainHandleCh, handlers))

packages/datadog-plugin-electron/test/app/main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* eslint-disable no-console */
44

55
const { join } = require('path')
6-
const { BrowserWindow, app, ipcMain, net } = require('electron/main')
6+
const { BrowserWindow, app, ipcMain, net, utilityProcess } = require('electron/main')
77

88
const CONFIG_CHANNEL = 'datadog:bridge-config'
99

@@ -22,6 +22,7 @@ app.on('ready', () => {
2222
case 'send': return onSend()
2323
case 'receive': return onReceive()
2424
case 'handle': return onHandle()
25+
case 'utility-request': return onUtilityRequest(msg)
2526
}
2627
} catch (e) {
2728
console.error(e)
@@ -75,6 +76,11 @@ function onHandle () {
7576
})
7677
}
7778

79+
function onUtilityRequest ({ url }) {
80+
const child = utilityProcess.fork(join(__dirname, 'utility.js'))
81+
child.postMessage({ name: 'request', url })
82+
}
83+
7884
function loadWindow (onShow) {
7985
const mainWindow = new BrowserWindow({
8086
show: false,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
require('../tracer')
4+
5+
const { net } = require('electron')
6+
7+
process.parentPort.on('message', ({ data }) => {
8+
if (data.name !== 'request') return
9+
const req = net.request(data.url)
10+
req.on('response', res => {
11+
res.on('data', () => {})
12+
res.on('end', () => {})
13+
})
14+
req.on('error', () => {})
15+
req.end()
16+
})

packages/datadog-plugin-electron/test/index.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ describe('Plugin', () => {
205205
child.send({ name: 'send' })
206206
})
207207

208+
it('should do automatic instrumentation for net.request from a utility process', done => {
209+
agent
210+
.assertSomeTraces(traces => {
211+
const span = traces[0][0]
212+
const { meta } = span
213+
214+
assert.strictEqual(span.type, 'http')
215+
assert.strictEqual(span.name, 'http.request')
216+
assert.strictEqual(span.resource, 'GET')
217+
assert.strictEqual(span.service, 'test')
218+
assert.strictEqual(span.error, 0)
219+
220+
assert.strictEqual(meta.component, 'electron')
221+
assert.strictEqual(meta['span.kind'], 'client')
222+
assert.strictEqual(meta['http.url'], `http://127.0.0.1:${port}/utility`)
223+
assert.strictEqual(meta['http.method'], 'GET')
224+
assert.strictEqual(meta['http.status_code'], '200')
225+
})
226+
.then(done)
227+
.catch(done)
228+
229+
child.send({ name: 'utility-request', url: `http://127.0.0.1:${port}/utility` })
230+
})
231+
208232
it('should do automatic instrumentation for renderer IPC when sending', done => {
209233
agent
210234
.assertSomeTraces(traces => {

0 commit comments

Comments
 (0)