Skip to content

Commit

Permalink
switch to internal diagnostics channel with node bug patch (#3002)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Apr 24, 2023
1 parent 3ca0123 commit 9a78f9a
Show file tree
Hide file tree
Showing 29 changed files with 222 additions and 78 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"eslint:recommended",
"standard"
],
"plugins": [
"mocha",
"n"
],
"env": {
"node": true,
"es2020": true
Expand All @@ -18,6 +22,7 @@
"object-curly-spacing": [2, "always"],
"import/no-extraneous-dependencies": 2,
"standard/no-callback-literal": 0,
"no-prototype-builtins": 0
"no-prototype-builtins": 0,
"n/no-restricted-require": [2, ["diagnostics_channel"]]
}
}
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dev,esbuild,MIT,Copyright (c) 2020 Evan Wallace
dev,eslint,MIT,Copyright JS Foundation and other contributors https://js.foundation
dev,eslint-config-standard,MIT,Copyright Feross Aboukhadijeh
dev,eslint-plugin-import,MIT,Copyright 2015 Ben Mosher
dev,eslint-plugin-n,MIT,Copyright 2015 Toru Nagashima
dev,eslint-plugin-node,MIT,Copyright 2015 Toru Nagashima
dev,eslint-plugin-promise,ISC,jden and other contributors
dev,eslint-plugin-standard,MIT,Copyright 2015 Jamund Ferguson
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"eslint": "^8.23.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-core/src/storage/async_resource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { createHook, executionAsyncResource } = require('async_hooks')
const { channel } = require('diagnostics_channel')
const { channel } = require('../../../diagnostics_channel')

const beforeCh = channel('dd-trace:storage:before')
const afterCh = channel('dd-trace:storage:after')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const dc = require('diagnostics_channel')
const dc = require('../../../diagnostics_channel')
const semver = require('semver')
const instrumentations = require('./instrumentations')
const { AsyncResource } = require('async_hooks')
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/helpers/register.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { channel } = require('diagnostics_channel')
const { channel } = require('../../../diagnostics_channel')
const path = require('path')
const semver = require('semver')
const Hook = require('./hook')
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/appsec/gateway/channels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const dc = require('diagnostics_channel')
const dc = require('../../../../diagnostics_channel')

// TODO: use TBD naming convention
// or directly use http plugin's channels
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/appsec/iast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { enableAllAnalyzers, disableAllAnalyzers } = require('./analyzers')
const web = require('../../plugins/util/web')
const { storage } = require('../../../../datadog-core')
const overheadController = require('./overhead-controller')
const dc = require('diagnostics_channel')
const dc = require('../../../../diagnostics_channel')
const iastContextFunctions = require('./iast-context')
const { enableTaintTracking, disableTaintTracking, createTransaction, removeTransaction } = require('./taint-tracking')

Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/appsec/iast/telemetry/logs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const dc = require('diagnostics_channel')
const dc = require('../../../../../diagnostics_channel')
const logCollector = require('./log_collector')
const { sendData } = require('../../../telemetry/send-data')
const log = require('../../../log')
Expand Down
2 changes: 2 additions & 0 deletions packages/dd-trace/src/dcitm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

// TODO: Figure out why we can't use the internal version.
// eslint-disable-next-line n/no-restricted-require
const dc = require('diagnostics_channel')

const CHANNEL_PREFIX = 'dd-trace:bundledModuleLoadStart'
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/iitm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const semver = require('semver')
const logger = require('./log')
const { addHook } = require('import-in-the-middle')
const dc = require('diagnostics_channel')
const dc = require('../../diagnostics_channel')

if (semver.satisfies(process.versions.node, '^12.20.0 || >=14.13.1')) {
const moduleLoadStartChannel = dc.channel('dd-trace:moduleLoadStart')
Expand Down
23 changes: 11 additions & 12 deletions packages/dd-trace/src/log/channels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const dc = require('diagnostics_channel')
const { channel } = require('../../../diagnostics_channel')

const Level = {
Debug: 'debug',
Expand All @@ -11,19 +11,18 @@ const Level = {

const defaultLevel = Level.Debug

class LogChannel extends dc.Channel {
constructor (name, logLevel) {
super(`datadog:log:${name}`)
this.logLevel = logLevel
}
}

// based on: https://github.com/trentm/node-bunyan#levels
const logChannels = {
[Level.Debug]: new LogChannel(Level.Debug, 20),
[Level.Info]: new LogChannel(Level.Info, 30),
[Level.Warn]: new LogChannel(Level.Warn, 40),
[Level.Error]: new LogChannel(Level.Error, 50)
[Level.Debug]: createLogChannel(Level.Debug, 20),
[Level.Info]: createLogChannel(Level.Info, 30),
[Level.Warn]: createLogChannel(Level.Warn, 40),
[Level.Error]: createLogChannel(Level.Error, 50)
}

function createLogChannel (name, logLevel) {
const logChannel = channel(`datadog:log:${name}`)
logChannel.logLevel = logLevel
return logChannel
}

function getChannelLogLevel (level) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/plugin_manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { channel } = require('diagnostics_channel')
const { channel } = require('../../diagnostics_channel')
const { isFalse } = require('./util')
const plugins = require('./plugins')
const log = require('./log')
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/plugins/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// TODO: move anything related to tracing to TracingPlugin instead

const dc = require('diagnostics_channel')
const dc = require('../../../diagnostics_channel')
const { storage } = require('../../../datadog-core')

class Subscription {
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/profiling/profilers/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { storage } = require('../../../../datadog-core')

const dc = require('diagnostics_channel')
const dc = require('../../../../diagnostics_channel')

const beforeCh = dc.channel('dd-trace:storage:before')
const afterCh = dc.channel('dd-trace:storage:after')
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/ritm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path')
const Module = require('module')
const parse = require('module-details-from-path')
const dc = require('diagnostics_channel')
const dc = require('../../diagnostics_channel')

const origRequire = Module.prototype.require

Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/telemetry/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path')
const parse = require('module-details-from-path')
const requirePackageJson = require('../require-package-json')
const { sendData } = require('./send-data')
const dc = require('diagnostics_channel')
const dc = require('../../../diagnostics_channel')
const { fileURLToPath } = require('url')

const savedDependencies = new Set()
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/telemetry/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const tracerVersion = require('../../../../package.json').version
const dc = require('diagnostics_channel')
const dc = require('../../../diagnostics_channel')
const os = require('os')
const dependencies = require('./dependencies')
const { sendData } = require('./send-data')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('vulnerability-analyzer', () => {
expect(vulnerabilityAnalyzer._subscriptions).to.have.lengthOf(1)
vulnerabilityAnalyzer._subscriptions[0].enable()

const dc = require('diagnostics_channel')
const dc = require('../../../../../diagnostics_channel')
expect(() => { dc.channel('dd-trace:test:error:sub').publish({}) }).to.not.throw()
})
})
2 changes: 1 addition & 1 deletion packages/dd-trace/test/appsec/iast/iast-log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('IAST log', () => {
error: sinon.stub()
}
telemetryLogs = proxyquire('../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': {
'../../../../../diagnostics_channel': {
channel: (name) => name === 'datadog:telemetry:start' ? telemetryStartChannel : telemetryStopChannel
}
})
Expand Down
20 changes: 10 additions & 10 deletions packages/dd-trace/test/appsec/iast/telemetry/logs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('telemetry logs', () => {
describe('start', () => {
it('should be enabled by default and subscribe', () => {
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc
'../../../../../diagnostics_channel': dc
})
logs.start()
defaultConfig.telemetry.logCollection = true
Expand All @@ -61,7 +61,7 @@ describe('telemetry logs', () => {

it('should be disabled and not subscribe if DD_TELEMETRY_LOG_COLLECTION_ENABLED = false', () => {
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc
'../../../../../diagnostics_channel': dc
})
logs.start()

Expand All @@ -76,7 +76,7 @@ describe('telemetry logs', () => {

let logCollectorCalled = 0
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'../../../telemetry/send-data': { sendData },
'./log_collector': {
drain: () => {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('telemetry logs', () => {
describe('stop', () => {
it('should unsubscribe configured listeners', () => {
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc
'../../../../../diagnostics_channel': dc
})
logs.start()
onTelemetryStart()(onTelemetryStartMsg)
Expand All @@ -118,7 +118,7 @@ describe('telemetry logs', () => {

it('should unsubscribe configured listeners when datadog:telemetry:stop is received', () => {
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc
'../../../../../diagnostics_channel': dc
})
logs.start()
onTelemetryStart()(onTelemetryStartMsg)
Expand All @@ -137,7 +137,7 @@ describe('telemetry logs', () => {
it('should be called with DEBUG level and error if config.telemetry.debug = true', () => {
const logCollectorAdd = sinon.stub()
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'./log_collector': {
add: logCollectorAdd
}
Expand All @@ -159,7 +159,7 @@ describe('telemetry logs', () => {
it('should be not called with DEBUG level if config.telemetry.debug = false', () => {
const logCollectorAdd = sinon.stub()
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'./log_collector': {
add: logCollectorAdd
}
Expand All @@ -175,7 +175,7 @@ describe('telemetry logs', () => {
it('should be called with WARN level', () => {
const logCollectorAdd = sinon.stub()
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'./log_collector': {
add: logCollectorAdd
}
Expand All @@ -191,7 +191,7 @@ describe('telemetry logs', () => {
it('should be called with ERROR level', () => {
const logCollectorAdd = sinon.stub()
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'./log_collector': {
add: logCollectorAdd
}
Expand All @@ -207,7 +207,7 @@ describe('telemetry logs', () => {
it('should be called with ERROR level and stack_trace', () => {
const logCollectorAdd = sinon.stub()
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
'diagnostics_channel': dc,
'../../../../../diagnostics_channel': dc,
'./log_collector': {
add: logCollectorAdd
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/iitm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('./setup/tap')

const { expect } = require('chai')
const semver = require('semver')
const dc = require('diagnostics_channel')
const dc = require('../../diagnostics_channel')

describe('iitm.js', () => {
let hookFn
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/plugin_manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('./setup/tap')

const { channel } = require('diagnostics_channel')
const { channel } = require('../../diagnostics_channel')
const proxyquire = require('proxyquire')

const loadChannel = channel('dd-trace:instrumentation:load')
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/plugins/log_plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const LogPlugin = require('../../src/plugins/log_plugin')
const Tracer = require('../../src/tracer')
const Config = require('../../src/config')

const { channel } = require('diagnostics_channel')
const { channel } = require('../../../diagnostics_channel')
const { expect } = require('chai')

const testLogChannel = channel('apm:test:log')
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/ritm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('./setup/tap')

const dc = require('diagnostics_channel')
const dc = require('../../diagnostics_channel')
const { assert } = require('chai')
const Hook = require('../src/ritm')

Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/telemetry/dependencies.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('../setup/tap')

const proxyquire = require('proxyquire')
const path = require('path')
const dc = require('diagnostics_channel')
const dc = require('../../../diagnostics_channel')
const moduleLoadStartChannel = dc.channel('dd-trace:moduleLoadStart')
const originalSetImmediate = global.setImmediate
describe('dependencies', () => {
Expand All @@ -13,7 +13,7 @@ describe('dependencies', () => {
const subscribe = sinon.stub()
const dc = { channel () { return { subscribe } } }
const dependencies = proxyquire('../../src/telemetry/dependencies', {
'diagnostics_channel': dc
'../../../diagnostics_channel': dc
})
dependencies.start()
expect(subscribe).to.have.been.calledOnce
Expand Down
3 changes: 3 additions & 0 deletions packages/diagnostics_channel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('./src')
Loading

0 comments on commit 9a78f9a

Please sign in to comment.