diff --git a/benchmark/core.js b/benchmark/core.js index 7b6ffe7c9bb..9f199ddd46d 100644 --- a/benchmark/core.js +++ b/benchmark/core.js @@ -3,7 +3,7 @@ const benchmark = require('./benchmark') const proxyquire = require('proxyquire') -const Config = require('../packages/dd-trace/src/config') +const getConfig = require('../packages/dd-trace/src/config') const DatadogTracer = require('../packages/dd-trace/src/tracer') const DatadogSpanContext = require('../packages/dd-trace/src/opentracing/span_context') const TextMapPropagator = require('../packages/dd-trace/src/opentracing/propagation/text_map') @@ -22,7 +22,7 @@ const Sampler = require('../packages/dd-trace/src/sampler') const format = require('../packages/dd-trace/src/format') const { AgentEncoder: Agent04Encoder } = require('../packages/dd-trace/src/encode/0.4') const { AgentEncoder: Agent05Encoder } = require('../packages/dd-trace/src/encode/0.5') -const config = new Config({ service: 'benchmark' }) +const config = getConfig({ service: 'benchmark' }) const id = require('../packages/dd-trace/src/id') const Histogram = require('../packages/dd-trace/src/histogram') const histogram = new Histogram() diff --git a/benchmark/openfeature.js b/benchmark/openfeature.js index 05e29f92a9d..e96d65f876b 100644 --- a/benchmark/openfeature.js +++ b/benchmark/openfeature.js @@ -4,12 +4,12 @@ const benchmark = require('./benchmark') const proxyquire = require('proxyquire') const { createSingleExposureEvent, createExposureEventArray } = require('./stubs/exposure-events') -const Config = require('../packages/dd-trace/src/config') +const getConfig = require('../packages/dd-trace/src/config') const ExposuresWriter = proxyquire('../packages/dd-trace/src/openfeature/writers/exposures', { '../../exporters/common/request': () => {} }) -const config = new Config({ service: 'benchmark', version: '1.0.0', env: 'test' }) +const config = getConfig({ service: 'benchmark', version: '1.0.0', env: 'test' }) const suite = benchmark('openfeature') let writer diff --git a/benchmark/sirun/debugger/start-devtools-client.js b/benchmark/sirun/debugger/start-devtools-client.js index f743a644912..754c21843c3 100644 --- a/benchmark/sirun/debugger/start-devtools-client.js +++ b/benchmark/sirun/debugger/start-devtools-client.js @@ -1,6 +1,6 @@ 'use strict' -const Config = require('../../../packages/dd-trace/src/config') +const getConfig = require('../../../packages/dd-trace/src/config') const { start } = require('../../../packages/dd-trace/src/debugger') const { generateProbeConfig } = require('../../../packages/dd-trace/test/debugger/devtools_client/utils') @@ -8,7 +8,7 @@ const breakpoint = { file: process.env.BREAKPOINT_FILE, line: process.env.BREAKPOINT_LINE } -const config = new Config() +const config = getConfig() const rc = { setProductHandler (product, cb) { const action = 'apply' diff --git a/packages/datadog-plugin-http/test/server.spec.js b/packages/datadog-plugin-http/test/server.spec.js index fc9370e7000..0cd5a65e557 100644 --- a/packages/datadog-plugin-http/test/server.spec.js +++ b/packages/datadog-plugin-http/test/server.spec.js @@ -17,6 +17,7 @@ describe('Plugin', () => { let tracer let port let app + let timeout ['http', 'node:http'].forEach(pluginToBeLoaded => { describe(`${pluginToBeLoaded}/server`, () => { @@ -32,13 +33,15 @@ describe('Plugin', () => { afterEach(() => { appListener && appListener.close() app = null + clearTimeout(timeout) + timeout = null return agent.close({ ritmReset: false }) }) describe('canceled request', () => { beforeEach(() => { listener = (req, res) => { - setTimeout(() => { + timeout = setTimeout(() => { app && app(req, res) res.writeHead(200) res.end() diff --git a/packages/datadog-plugin-openai/test/services.spec.js b/packages/datadog-plugin-openai/test/services.spec.js index bb1ef373f80..155eddff9d2 100644 --- a/packages/datadog-plugin-openai/test/services.spec.js +++ b/packages/datadog-plugin-openai/test/services.spec.js @@ -1,6 +1,7 @@ 'use strict' const services = require('../src/services') +const { getConfigFresh } = require('../../dd-trace/test/helpers/config') describe('Plugin', () => { describe('openai services', () => { @@ -10,23 +11,23 @@ describe('Plugin', () => { }) it('dogstatsd does not throw when missing .dogstatsd', () => { - const service = services.init({ + const service = services.init(getConfigFresh({ hostname: 'foo', service: 'bar', apiKey: 'my api key', interval: 1000 - }) + })) service.metrics.increment('mykey') service.logger.log('hello') }) it('logger does not throw', () => { - const service = services.init({ + const service = services.init(getConfigFresh({ hostname: 'foo', service: 'bar', interval: 1000 - }) + })) service.logger.log('hello') }) diff --git a/packages/dd-trace/src/config.js b/packages/dd-trace/src/config.js index fd02d3766ad..b20636224d9 100644 --- a/packages/dd-trace/src/config.js +++ b/packages/dd-trace/src/config.js @@ -4,6 +4,7 @@ const fs = require('fs') const os = require('os') const uuid = require('crypto-randomuuid') // we need to keep the old uuid dep because of cypress const { URL } = require('url') + const log = require('./log') const tagger = require('./tagger') const set = require('../../datadog-core/src/utils/src/set') @@ -1507,4 +1508,12 @@ function getAgentUrl (url, options) { } } -module.exports = Config +let configInstance = null +function getConfig (options) { + if (!configInstance) { + configInstance = new Config(options) + } + return configInstance +} + +module.exports = getConfig diff --git a/packages/dd-trace/src/proxy.js b/packages/dd-trace/src/proxy.js index 3c569c47ce1..1a90294840e 100644 --- a/packages/dd-trace/src/proxy.js +++ b/packages/dd-trace/src/proxy.js @@ -1,7 +1,7 @@ 'use strict' const NoopProxy = require('./noop/proxy') const DatadogTracer = require('./tracer') -const Config = require('./config') +const getConfig = require('./config') const runtimeMetrics = require('./runtime_metrics') const log = require('./log') const { setStartupLogPluginManager } = require('./startup-log') @@ -98,7 +98,7 @@ class Tracer extends NoopProxy { this._initialized = true try { - const config = new Config(options) // TODO: support dynamic code config + const config = getConfig(options) // TODO: support dynamic code config if (config.crashtracking.enabled) { require('./crashtracking').start(config) diff --git a/packages/dd-trace/test/appsec/attacker-fingerprinting.express.plugin.spec.js b/packages/dd-trace/test/appsec/attacker-fingerprinting.express.plugin.spec.js index fdb9b20faba..531a97af137 100644 --- a/packages/dd-trace/test/appsec/attacker-fingerprinting.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/attacker-fingerprinting.express.plugin.spec.js @@ -1,12 +1,13 @@ 'use strict' +const path = require('node:path') + const axios = require('axios') const { assert } = require('chai') -const path = require('path') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { withVersions } = require('../../../dd-trace/test/setup/mocha') withVersions('express', 'express', expressVersion => { @@ -40,7 +41,7 @@ withVersions('express', 'express', expressVersion => { }) beforeEach(() => { - appsec.enable(new Config( + appsec.enable(getConfigFresh( { appsec: { enabled: true, diff --git a/packages/dd-trace/test/appsec/attacker-fingerprinting.fastify.plugin.spec.js b/packages/dd-trace/test/appsec/attacker-fingerprinting.fastify.plugin.spec.js index f93cbc72793..d893bc4f339 100644 --- a/packages/dd-trace/test/appsec/attacker-fingerprinting.fastify.plugin.spec.js +++ b/packages/dd-trace/test/appsec/attacker-fingerprinting.fastify.plugin.spec.js @@ -1,12 +1,13 @@ 'use strict' +const path = require('node:path') + const Axios = require('axios') const { assert } = require('chai') -const path = require('path') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { withVersions } = require('../setup/mocha') withVersions('fastify', 'fastify', fastifyVersion => { @@ -40,7 +41,7 @@ withVersions('fastify', 'fastify', fastifyVersion => { }) beforeEach(() => { - appsec.enable(new Config( + appsec.enable(getConfigFresh( { appsec: { enabled: true, diff --git a/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-http.plugin.spec.js b/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-http.plugin.spec.js index b8f8e25495b..149385d2153 100644 --- a/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-http.plugin.spec.js +++ b/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-http.plugin.spec.js @@ -5,7 +5,7 @@ const { assert } = require('chai') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { withVersions } = require('../setup/mocha') function assertFingerprintInTraces (traces) { @@ -27,7 +27,7 @@ withVersions('passport-http', 'passport-http', version => { }) before(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: true })) }) diff --git a/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-local.plugin.spec.js b/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-local.plugin.spec.js index dc384f58227..784a0df8276 100644 --- a/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-local.plugin.spec.js +++ b/packages/dd-trace/test/appsec/attacker-fingerprinting.passport-local.plugin.spec.js @@ -5,7 +5,7 @@ const { assert } = require('chai') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { withVersions } = require('../setup/mocha') function assertFingerprintInTraces (traces) { @@ -27,7 +27,7 @@ withVersions('passport-local', 'passport-local', version => { }) before(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: true })) }) diff --git a/packages/dd-trace/test/appsec/attacker-fingerprinting.spec.js b/packages/dd-trace/test/appsec/attacker-fingerprinting.spec.js index d73c5bbbcc8..03208835278 100644 --- a/packages/dd-trace/test/appsec/attacker-fingerprinting.spec.js +++ b/packages/dd-trace/test/appsec/attacker-fingerprinting.spec.js @@ -2,10 +2,11 @@ const axios = require('axios') const { assert } = require('chai') + const agent = require('../plugins/agent') const tracer = require('../../../../index') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') describe('Attacker fingerprinting', () => { describe('SDK', () => { @@ -21,7 +22,7 @@ describe('Attacker fingerprinting', () => { } before(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ enabled: true })) }) diff --git a/packages/dd-trace/test/appsec/extended-data-collection.express.plugin.spec.js b/packages/dd-trace/test/appsec/extended-data-collection.express.plugin.spec.js index cf478e4bc5a..6b3db716e29 100644 --- a/packages/dd-trace/test/appsec/extended-data-collection.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/extended-data-collection.express.plugin.spec.js @@ -1,14 +1,16 @@ 'use strict' -const Config = require('../../src/config') -const path = require('path') +const assert = require('node:assert') +const path = require('node:path') + +const axios = require('axios') +const msgpack = require('@msgpack/msgpack') + const { withVersions } = require('../setup/mocha') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const axios = require('axios') -const assert = require('assert') -const msgpack = require('@msgpack/msgpack') const { createDeepObject } = require('./utils') +const { getConfigFresh } = require('../helpers/config') describe('extended data collection', () => { before(() => { @@ -68,7 +70,7 @@ describe('extended data collection', () => { }) beforeEach(() => { - appsec.enable(new Config( + appsec.enable(getConfigFresh( { appsec: { enabled: true, diff --git a/packages/dd-trace/test/appsec/extended-data-collection.fastify.plugin.spec.js b/packages/dd-trace/test/appsec/extended-data-collection.fastify.plugin.spec.js index 33e3d1a6357..03c2effdad1 100644 --- a/packages/dd-trace/test/appsec/extended-data-collection.fastify.plugin.spec.js +++ b/packages/dd-trace/test/appsec/extended-data-collection.fastify.plugin.spec.js @@ -1,14 +1,17 @@ 'use strict' -const Config = require('../../src/config') -const path = require('path') +const assert = require('node:assert') +const path = require('node:path') + +const axios = require('axios') +const msgpack = require('@msgpack/msgpack') + const { withVersions } = require('../setup/mocha') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const axios = require('axios') -const assert = require('assert') -const msgpack = require('@msgpack/msgpack') + const { createDeepObject } = require('./utils') +const { getConfigFresh } = require('../helpers/config') describe('extended data collection', () => { before(() => { @@ -69,7 +72,7 @@ describe('extended data collection', () => { }) beforeEach(() => { - appsec.enable(new Config( + appsec.enable(getConfigFresh( { appsec: { enabled: true, diff --git a/packages/dd-trace/test/appsec/graphql.test-utils.js b/packages/dd-trace/test/appsec/graphql.test-utils.js index 31b91cf3fa6..51789a6a6f6 100644 --- a/packages/dd-trace/test/appsec/graphql.test-utils.js +++ b/packages/dd-trace/test/appsec/graphql.test-utils.js @@ -10,7 +10,7 @@ const fs = require('node:fs') const { graphqlJson, json } = require('../../src/appsec/blocked_templates') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const schema = ` directive @case(format: String) on FIELD @@ -76,7 +76,7 @@ async function makeGraphqlRequest (port, variables, derivativeParam, extraHeader function graphqlCommonTests (config) { describe('Block with content', () => { beforeEach(() => { - appsec.enable(new Config({ appsec: { enabled: true, rules: path.join(__dirname, 'graphql-rules.json') } })) + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'graphql-rules.json') } })) }) afterEach(() => { @@ -151,10 +151,10 @@ function graphqlCommonTests (config) { describe('Block with custom content', () => { const blockedTemplateGraphql = path.join(__dirname, 'graphql.block.json') - const customGraphqlJson = fs.readFileSync(blockedTemplateGraphql) + const customGraphqlJson = fs.readFileSync(blockedTemplateGraphql, { encoding: 'utf8' }) beforeEach(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'graphql-rules.json'), @@ -181,7 +181,7 @@ function graphqlCommonTests (config) { describe('Block with redirect', () => { beforeEach(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'graphql-rules-redirect.json') diff --git a/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-password-analyzer.spec.js b/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-password-analyzer.spec.js index 623dbfaf398..e4b3a961b3f 100644 --- a/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-password-analyzer.spec.js +++ b/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-password-analyzer.spec.js @@ -10,7 +10,7 @@ const fs = require('node:fs') const os = require('node:os') const agent = require('../../../plugins/agent') -const Config = require('../../../../src/config') +const { getConfigFresh } = require('../../../helpers/config') const hardcodedPasswordAnalyzer = require('../../../../src/appsec/iast/analyzers/hardcoded-password-analyzer') const iast = require('../../../../src/appsec/iast') @@ -125,7 +125,7 @@ describe('Hardcoded Password Analyzer', () => { beforeEach(() => { const tracer = require('../../../../') - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-secret-analyzer.spec.js b/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-secret-analyzer.spec.js index feaafac8dd3..f7e035d1f26 100644 --- a/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-secret-analyzer.spec.js +++ b/packages/dd-trace/test/appsec/iast/analyzers/hardcoded-secret-analyzer.spec.js @@ -9,7 +9,7 @@ const fs = require('node:fs') const os = require('node:os') const agent = require('../../../plugins/agent') -const Config = require('../../../../src/config') +const { getConfigFresh } = require('../../../helpers/config') const { NameAndValue, ValueOnly } = require('../../../../src/appsec/iast/analyzers/hardcoded-rule-type') const hardcodedSecretAnalyzer = require('../../../../src/appsec/iast/analyzers/hardcoded-secret-analyzer') @@ -95,7 +95,7 @@ describe('Hardcoded Secret Analyzer', () => { beforeEach(() => { const tracer = require('../../../../') - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/index.spec.js b/packages/dd-trace/test/appsec/iast/index.spec.js index f2e9599cc47..fc6339ab0fb 100644 --- a/packages/dd-trace/test/appsec/iast/index.spec.js +++ b/packages/dd-trace/test/appsec/iast/index.spec.js @@ -6,7 +6,6 @@ const { describe, it, beforeEach, afterEach } = require('mocha') const proxyquire = require('proxyquire') const sinon = require('sinon') -const Config = require('../../../src/config') const agent = require('../../plugins/agent') const iast = require('../../../src/appsec/iast') const iastContextFunctions = require('../../../src/appsec/iast/iast-context') @@ -14,6 +13,7 @@ const overheadController = require('../../../src/appsec/iast/overhead-controller const vulnerabilityReporter = require('../../../src/appsec/iast/vulnerability-reporter') const { testInRequest } = require('./utils') const { IAST_MODULE } = require('../../../src/appsec/rasp/fs-plugin') +const { getConfigFresh } = require('../../helpers/config') describe('IAST Index', () => { beforeEach(() => { @@ -48,7 +48,7 @@ describe('IAST Index', () => { const originalReleaseRequest = overheadController.releaseRequest beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, @@ -112,7 +112,7 @@ describe('IAST Index', () => { let appsecFsPlugin let analyzers - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/overhead-controller.spec.js b/packages/dd-trace/test/appsec/iast/overhead-controller.spec.js index f3e3a9daed0..a4fba5440b2 100644 --- a/packages/dd-trace/test/appsec/iast/overhead-controller.spec.js +++ b/packages/dd-trace/test/appsec/iast/overhead-controller.spec.js @@ -1,21 +1,22 @@ 'use strict' +const { EventEmitter } = require('node:events') + const axios = require('axios') const { expect } = require('chai') const { describe, it, beforeEach, afterEach, before, after } = require('mocha') const proxyquire = require('proxyquire') const sinon = require('sinon') -const { EventEmitter } = require('node:events') const vulnerabilityReporter = require('../../../src/appsec/iast/vulnerability-reporter') const DatadogSpanContext = require('../../../src/opentracing/span_context') -const Config = require('../../../src/config') const id = require('../../../src/id') const iast = require('../../../src/appsec/iast') const rewriter = require('../../../src/appsec/iast/taint-tracking/rewriter') const { testInRequest } = require('./utils') const agent = require('../../plugins/agent') const vulnerabilities = require('../../../src/appsec/iast/vulnerabilities') +const { getConfigFresh } = require('../../helpers/config') describe('Overhead controller', () => { let oceContextKey, overheadController, web @@ -31,7 +32,7 @@ describe('Overhead controller', () => { }) oceContextKey = overheadController.OVERHEAD_CONTROLLER_CONTEXT_KEY - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: true } @@ -434,7 +435,7 @@ describe('Overhead controller', () => { }) it('should detect vulnerabilities only in one if max concurrent is 1', (done) => { - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, @@ -486,7 +487,7 @@ describe('Overhead controller', () => { }) it('should detect vulnerabilities in both if max concurrent is 2', (done) => { - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, @@ -542,7 +543,7 @@ describe('Overhead controller', () => { it('should recovery requests budget', function (done) { // 3 in parallel => 2 detects - 1 not detects // on finish the first => launch 2 - should detect 1 more - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, @@ -614,7 +615,7 @@ describe('Overhead controller', () => { }) it('should add _dd.iast.enabled tag even when no vulnerability is detected', (done) => { - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/plugin.spec.js b/packages/dd-trace/test/appsec/iast/taint-tracking/plugin.spec.js index eae8ff94080..4a433c1888e 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/plugin.spec.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/plugin.spec.js @@ -15,7 +15,7 @@ const { HTTP_REQUEST_URI, SQL_ROW_VALUE } = require('../../../../src/appsec/iast/taint-tracking/source-types') -const Config = require('../../../../src/config') +const { getConfigFresh } = require('../../../helpers/config') const middlewareNextChannel = dc.channel('apm:express:middleware:next') const queryReadFinishChannel = dc.channel('datadog:query:read:finish') @@ -43,7 +43,7 @@ describe('IAST Taint tracking plugin', () => { './operations': sinon.spy(taintTrackingOperations), '../../../../../datadog-core': datadogCore }) - const config = new Config() + const config = getConfigFresh() taintTrackingPlugin.enable(config.iast) }) @@ -292,7 +292,7 @@ describe('IAST Taint tracking plugin', () => { describe('taint database sources', () => { it('Should not taint if config is set to 0', () => { taintTrackingPlugin.disable() - const config = new Config() + const config = getConfigFresh() config.dbRowsToTaint = 0 taintTrackingPlugin.enable(config) @@ -395,7 +395,7 @@ describe('IAST Taint tracking plugin', () => { describe('with config set to 2', () => { beforeEach(() => { taintTrackingPlugin.disable() - const config = new Config() + const config = getConfigFresh() config.dbRowsToTaint = 2 taintTrackingPlugin.enable(config) }) diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/graphql.sources.test-utils.js b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/graphql.sources.test-utils.js index db1269b0621..783f86945e9 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/graphql.sources.test-utils.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/graphql.sources.test-utils.js @@ -6,7 +6,7 @@ const { describe, it, beforeEach, afterEach } = require('mocha') const agent = require('../../../../plugins/agent') const iast = require('../../../../../src/appsec/iast') -const Config = require('../../../../../src/config') +const { getConfigFresh } = require('../../../../helpers/config') const vulnerabilityReporter = require('../../../../../src/appsec/iast/vulnerability-reporter') const overheadController = require('../../../../../src/appsec/iast/overhead-controller') @@ -70,7 +70,7 @@ async function makeGraphqlRequest (port, query, variables = {}) { function graphqlCommonTests (config) { describe('Graphql sources tests', () => { beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.cookie.plugin.spec.js b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.cookie.plugin.spec.js index d258b0e31c6..6c525dbc176 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.cookie.plugin.spec.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.cookie.plugin.spec.js @@ -4,7 +4,7 @@ const axios = require('axios') const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') -const Config = require('../../../../../src/config') +const { getConfigFresh } = require('../../../../helpers/config') const { storage } = require('../../../../../../datadog-core') const iast = require('../../../../../src/appsec/iast') const iastContextFunctions = require('../../../../../src/appsec/iast/iast-context') @@ -40,7 +40,7 @@ describe('Cookies sourcing with cookies', () => { function tests (config) { beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.express.plugin.spec.js b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.express.plugin.spec.js index e260a902179..9ddc4df55f5 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.express.plugin.spec.js @@ -4,9 +4,9 @@ const axios = require('axios') const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') const semver = require('semver') + const { NODE_MAJOR } = require('../../../../../../../version') const agent = require('../../../../plugins/agent') -const Config = require('../../../../../src/config') const { storage } = require('../../../../../../datadog-core') const iast = require('../../../../../src/appsec/iast') const iastContextFunctions = require('../../../../../src/appsec/iast/iast-context') @@ -16,6 +16,7 @@ const { HTTP_REQUEST_PATH_PARAM, HTTP_REQUEST_URI } = require('../../../../../src/appsec/iast/taint-tracking/source-types') +const { getConfigFresh } = require('../../../../helpers/config') describe('URI sourcing with express', () => { let express @@ -36,7 +37,7 @@ describe('URI sourcing with express', () => { }) beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, @@ -113,7 +114,7 @@ describe('Path params sourcing with express', () => { }) beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.fastify.plugin.spec.js b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.fastify.plugin.spec.js index c86e462fd32..435effba587 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.fastify.plugin.spec.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.fastify.plugin.spec.js @@ -5,7 +5,6 @@ const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') const agent = require('../../../../plugins/agent') -const Config = require('../../../../../src/config') const { storage } = require('../../../../../../datadog-core') const iast = require('../../../../../src/appsec/iast') const iastContextFunctions = require('../../../../../src/appsec/iast/iast-context') @@ -15,6 +14,7 @@ const { HTTP_REQUEST_PATH_PARAM, HTTP_REQUEST_URI } = require('../../../../../src/appsec/iast/taint-tracking/source-types') +const { getConfigFresh } = require('../../../../helpers/config') describe('URI sourcing with fastify', () => { let fastify @@ -30,7 +30,7 @@ describe('URI sourcing with fastify', () => { }) beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, @@ -88,7 +88,7 @@ describe('Path params sourcing with fastify', () => { }) beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.headers.spec.js b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.headers.spec.js index e4997e703fd..1a9e7957036 100644 --- a/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.headers.spec.js +++ b/packages/dd-trace/test/appsec/iast/taint-tracking/sources/taint-tracking.headers.spec.js @@ -4,7 +4,7 @@ const axios = require('axios') const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') -const Config = require('../../../../../src/config') +const { getConfigFresh } = require('../../../../helpers/config') const { storage } = require('../../../../../../datadog-core') const iast = require('../../../../../src/appsec/iast') const iastContextFunctions = require('../../../../../src/appsec/iast/iast-context') @@ -28,7 +28,7 @@ describe('Headers sourcing', () => { function tests (config) { beforeEach(() => { - iast.enable(new Config({ + iast.enable(getConfigFresh({ experimental: { iast: { enabled: true, diff --git a/packages/dd-trace/test/appsec/iast/telemetry/index.spec.js b/packages/dd-trace/test/appsec/iast/telemetry/index.spec.js index 3b37ca5b7b1..0262f6ea7b7 100644 --- a/packages/dd-trace/test/appsec/iast/telemetry/index.spec.js +++ b/packages/dd-trace/test/appsec/iast/telemetry/index.spec.js @@ -7,7 +7,7 @@ const { describe, it, beforeEach, afterEach } = require('mocha') const sinon = require('sinon') const { Verbosity } = require('../../../../src/appsec/iast/telemetry/verbosity') -const Config = require('../../../../src/config') +const { getConfigFresh } = require('../../../helpers/config') const iast = require('../../../../src/appsec/iast') const agent = require('../../../plugins/agent') const { testInRequest } = require('../utils') @@ -174,8 +174,7 @@ describe('Telemetry', () => { DD_IAST_ENABLED: 'true', DD_IAST_REQUEST_SAMPLING: '100' } - const config = new Config() - iast.enable(config) + iast.enable(getConfigFresh()) }) afterEach(() => { diff --git a/packages/dd-trace/test/appsec/iast/utils.js b/packages/dd-trace/test/appsec/iast/utils.js index ddcc8e0e731..58932cbb7a6 100644 --- a/packages/dd-trace/test/appsec/iast/utils.js +++ b/packages/dd-trace/test/appsec/iast/utils.js @@ -11,7 +11,7 @@ const { describe, it, beforeEach, afterEach, before, after } = require('mocha') const agent = require('../../plugins/agent') const rewriter = require('../../../src/appsec/iast/taint-tracking/rewriter') const iast = require('../../../src/appsec/iast') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const vulnerabilityReporter = require('../../../src/appsec/iast/vulnerability-reporter') const overheadController = require('../../../src/appsec/iast/overhead-controller') const { getWebSpan } = require('../utils') @@ -74,7 +74,7 @@ function testOutsideRequestHasVulnerability (fnToTest, vulnerability, plugins, t }) beforeEach(() => { const tracer = require('../../..') - const config = new Config({ + const config = getConfigFresh({ experimental: { iast: { enabled: true, @@ -125,7 +125,7 @@ function beforeEachIastTest (iastConfig) { beforeEach(() => { overheadController.clearGlobalRouteMap() vulnerabilityReporter.clearCache() - const config = new Config({ + const config = getConfigFresh({ iast: iastConfig }) iast.enable(config) diff --git a/packages/dd-trace/test/appsec/index.body-parser.plugin.spec.js b/packages/dd-trace/test/appsec/index.body-parser.plugin.spec.js index 087952cb225..31c3e5b2d5d 100644 --- a/packages/dd-trace/test/appsec/index.body-parser.plugin.spec.js +++ b/packages/dd-trace/test/appsec/index.body-parser.plugin.spec.js @@ -1,12 +1,14 @@ 'use strict' -const axios = require('axios') const path = require('node:path') + +const axios = require('axios') const { expect } = require('chai') const sinon = require('sinon') + const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { json } = require('../../src/appsec/blocked_templates') const { withVersions } = require('../setup/mocha') @@ -37,7 +39,12 @@ withVersions('body-parser', 'body-parser', version => { beforeEach(async () => { requestBody = sinon.stub() - appsec.enable(new Config({ appsec: { enabled: true, rules: path.join(__dirname, 'body-parser-rules.json') } })) + appsec.enable(getConfigFresh({ + appsec: { + enabled: true, + rules: path.join(__dirname, 'body-parser-rules.json') + } + })) }) afterEach(() => { diff --git a/packages/dd-trace/test/appsec/index.cookie-parser.plugin.spec.js b/packages/dd-trace/test/appsec/index.cookie-parser.plugin.spec.js index 930c34bed87..87b84b504b0 100644 --- a/packages/dd-trace/test/appsec/index.cookie-parser.plugin.spec.js +++ b/packages/dd-trace/test/appsec/index.cookie-parser.plugin.spec.js @@ -2,12 +2,13 @@ const assert = require('node:assert') const path = require('node:path') + const axios = require('axios') const sinon = require('sinon') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { json } = require('../../src/appsec/blocked_templates') const { withVersions } = require('../setup/mocha') @@ -38,7 +39,12 @@ withVersions('cookie-parser', 'cookie-parser', version => { beforeEach(async () => { requestCookie = sinon.stub() - appsec.enable(new Config({ appsec: { enabled: true, rules: path.join(__dirname, 'cookie-parser-rules.json') } })) + appsec.enable(getConfigFresh({ + appsec: { + enabled: true, + rules: path.join(__dirname, 'cookie-parser-rules.json') + } + })) }) afterEach(() => { diff --git a/packages/dd-trace/test/appsec/index.express.plugin.spec.js b/packages/dd-trace/test/appsec/index.express.plugin.spec.js index 84d40e1b72e..9aad71f7fd9 100644 --- a/packages/dd-trace/test/appsec/index.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/index.express.plugin.spec.js @@ -12,10 +12,11 @@ const zlib = require('node:zlib') const { NODE_MAJOR } = require('../../../../version') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') const { json } = require('../../src/appsec/blocked_templates') const { withVersions } = require('../setup/mocha') +const { getConfigFresh } = require('../helpers/config') + withVersions('express', 'express', version => { if (semver.intersects(version, '<=4.10.5') && NODE_MAJOR >= 24) { describe.skip(`refusing to run tests as express@${version} is incompatible with Node.js ${NODE_MAJOR}`) @@ -72,7 +73,7 @@ withVersions('express', 'express', version => { }) beforeEach(async () => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'rules-example.json') @@ -222,7 +223,7 @@ withVersions('express', 'express', version => { beforeEach(async () => { requestBody = sinon.stub() - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'rules-example.json') @@ -298,7 +299,7 @@ withVersions('express', 'express', version => { }) beforeEach(() => { - config = new Config({ + config = getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'api_security_rules.json'), diff --git a/packages/dd-trace/test/appsec/index.fastify.plugin.spec.js b/packages/dd-trace/test/appsec/index.fastify.plugin.spec.js index ff2ee61af22..97eea82b26f 100644 --- a/packages/dd-trace/test/appsec/index.fastify.plugin.spec.js +++ b/packages/dd-trace/test/appsec/index.fastify.plugin.spec.js @@ -1,19 +1,21 @@ 'use strict' +const path = require('node:path') +const zlib = require('node:zlib') +const fs = require('node:fs') + const Axios = require('axios') const { assert } = require('chai') const semver = require('semver') const sinon = require('sinon') -const path = require('node:path') -const zlib = require('node:zlib') -const fs = require('node:fs') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') const { json } = require('../../src/appsec/blocked_templates') const { withVersions } = require('../setup/mocha') +const { getConfigFresh } = require('../helpers/config') + withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersion) => { describe('Suspicious request blocking - query', () => { let server, requestBody, axios @@ -47,7 +49,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi beforeEach(async () => { requestBody = sinon.stub() - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'rules-example.json') @@ -112,7 +114,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi beforeEach(async () => { requestBody = sinon.stub() - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'body-parser-rules.json') @@ -220,7 +222,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi }) beforeEach(async () => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'body-parser-rules.json') @@ -309,7 +311,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi }) beforeEach(async () => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'rules-example.json') @@ -488,7 +490,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi beforeEach(async () => { requestCookie = sinon.stub() appsec.enable( - new Config({ + getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'cookie-parser-rules.json') @@ -581,7 +583,7 @@ withVersions('fastify', 'fastify', '>=2', (fastifyVersion, _, fastifyLoadedVersi beforeEach(() => { uploadSpy = sinon.stub() - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'body-parser-rules.json') @@ -679,7 +681,7 @@ describe('Api Security - Fastify', () => { }) beforeEach(() => { - config = new Config({ + config = getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'api_security_rules.json'), diff --git a/packages/dd-trace/test/appsec/index.sequelize.plugin.spec.js b/packages/dd-trace/test/appsec/index.sequelize.plugin.spec.js index 0c57e0c5947..615713ae44f 100644 --- a/packages/dd-trace/test/appsec/index.sequelize.plugin.spec.js +++ b/packages/dd-trace/test/appsec/index.sequelize.plugin.spec.js @@ -1,10 +1,12 @@ 'use strict' const path = require('path') + const axios = require('axios') + const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const { withVersions } = require('../setup/mocha') describe('sequelize', () => { @@ -16,7 +18,7 @@ describe('sequelize', () => { // init tracer before(async () => { await agent.load(['express', 'http'], { client: false }, { flushInterval: 1 }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'rules-example.json'), diff --git a/packages/dd-trace/test/appsec/index.spec.js b/packages/dd-trace/test/appsec/index.spec.js index 3a4492a3172..6f31139988a 100644 --- a/packages/dd-trace/test/appsec/index.spec.js +++ b/packages/dd-trace/test/appsec/index.spec.js @@ -1,6 +1,7 @@ 'use strict' const fs = require('node:fs') + const axios = require('axios') const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') @@ -29,11 +30,11 @@ const { } = require('../../src/appsec/channels') const Reporter = require('../../src/appsec/reporter') const agent = require('../plugins/agent') -const Config = require('../../src/config') const blockedTemplate = require('../../src/appsec/blocked_templates') const { storage } = require('../../../datadog-core') const telemetryMetrics = require('../../src/telemetry/metrics') const addresses = require('../../src/appsec/addresses') +const { getConfigFresh } = require('../helpers/config') const resultActions = { actions: { @@ -1252,7 +1253,7 @@ describe('AppSec Index', function () { appsecNamespace.reset() - config = new Config({ + config = getConfigFresh({ appsec: { enabled: true } @@ -1350,7 +1351,7 @@ describe('IP blocking', function () { }) beforeEach(() => { - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rasp: { diff --git a/packages/dd-trace/test/appsec/rasp/command_injection.express.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/command_injection.express.plugin.spec.js index c60f9f91f49..0cfadc6f378 100644 --- a/packages/dd-trace/test/appsec/rasp/command_injection.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/command_injection.express.plugin.spec.js @@ -8,7 +8,7 @@ const { describe, it, beforeEach, before, after } = require('mocha') const agent = require('../../plugins/agent') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const { withVersions } = require('../../setup/mocha') const { checkRaspExecutedAndHasThreat, checkRaspExecutedAndNotThreat } = require('./utils') @@ -71,7 +71,7 @@ describe('RASP - command_injection', () => { app(req, res) }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/rasp/lfi.express.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/lfi.express.plugin.spec.js index 836361995e9..7ab1d8d81de 100644 --- a/packages/dd-trace/test/appsec/rasp/lfi.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/lfi.express.plugin.spec.js @@ -1,17 +1,19 @@ 'use strict' -const { NODE_MAJOR } = require('../../../../../version') -const semver = require('semver') +const os = require('node:os') +const fs = require('node:fs') +const path = require('node:path') + const Axios = require('axios') -const os = require('os') -const fs = require('fs') +const { assert } = require('chai') +const semver = require('semver') + +const { NODE_MAJOR } = require('../../../../../version') const agent = require('../../plugins/agent') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') const { withVersions } = require('../../setup/mocha') -const path = require('path') -const { assert } = require('chai') const { checkRaspExecutedAndNotThreat, checkRaspExecutedAndHasThreat } = require('./utils') +const { getConfigFresh } = require('../../helpers/config') describe('RASP - lfi', () => { let axios @@ -58,7 +60,7 @@ describe('RASP - lfi', () => { app(req, res) }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'lfi_rasp_rules.json'), @@ -471,7 +473,7 @@ describe('RASP - lfi', () => { } }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'lfi_rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/rasp/rasp_blocking.fastify.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/rasp_blocking.fastify.plugin.spec.js index b2bc250316d..eae7b94f2f2 100644 --- a/packages/dd-trace/test/appsec/rasp/rasp_blocking.fastify.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/rasp_blocking.fastify.plugin.spec.js @@ -2,7 +2,7 @@ const path = require('node:path') const agent = require('../../plugins/agent') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const appsec = require('../../../src/appsec') const Axios = require('axios') @@ -21,7 +21,7 @@ describe('RASP - fastify blocking', () => { before(async () => { await agent.load(['http', 'fastify'], { client: false }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/rasp/sql_injection.mysql2.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/sql_injection.mysql2.plugin.spec.js index 22f806c6837..afeb744f11a 100644 --- a/packages/dd-trace/test/appsec/rasp/sql_injection.mysql2.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/sql_injection.mysql2.plugin.spec.js @@ -2,7 +2,7 @@ const agent = require('../../plugins/agent') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') +const getConfig = require('../../../src/config') const { withVersions } = require('../../setup/mocha') const path = require('path') const Axios = require('axios') @@ -33,7 +33,7 @@ describe('RASP - sql_injection', () => { app(req, res) }) - appsec.enable(new Config({ + appsec.enable(getConfig({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/rasp/sql_injection.pg.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/sql_injection.pg.plugin.spec.js index 0d236b86b9e..301844876aa 100644 --- a/packages/dd-trace/test/appsec/rasp/sql_injection.pg.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/sql_injection.pg.plugin.spec.js @@ -10,7 +10,7 @@ const agent = require('../../plugins/agent') const appsec = require('../../../src/appsec') const { wafRunFinished } = require('../../../src/appsec/channels') const addresses = require('../../../src/appsec/addresses') -const Config = require('../../../src/config') +const getConfig = require('../../../src/config') const { withVersions } = require('../../setup/mocha') const { checkRaspExecutedAndNotThreat, checkRaspExecutedAndHasThreat } = require('./utils') @@ -40,7 +40,7 @@ describe('RASP - sql_injection', () => { app(req, res) }) - appsec.enable(new Config({ + appsec.enable(getConfig({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/rasp/ssrf.express.plugin.spec.js b/packages/dd-trace/test/appsec/rasp/ssrf.express.plugin.spec.js index d25211c7f7e..a89704a8cc8 100644 --- a/packages/dd-trace/test/appsec/rasp/ssrf.express.plugin.spec.js +++ b/packages/dd-trace/test/appsec/rasp/ssrf.express.plugin.spec.js @@ -1,12 +1,15 @@ 'use strict' +const path = require('node:path') + const Axios = require('axios') +const { assert } = require('chai') +const { describe, it, beforeEach, before, after } = require('mocha') + +const { getConfigFresh } = require('../../helpers/config') const agent = require('../../plugins/agent') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') const { withVersions } = require('../../setup/mocha') -const path = require('path') -const { assert } = require('chai') const { checkRaspExecutedAndNotThreat, checkRaspExecutedAndHasThreat } = require('./utils') function noop () {} @@ -27,7 +30,7 @@ describe('RASP - ssrf', () => { app(req, res) }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), @@ -68,8 +71,11 @@ describe('RASP - ssrf', () => { ['http', 'https'].forEach(protocol => { describe(`Test using ${protocol}`, () => { it('Should not detect threat', async () => { + // Hack to enforce the module to be loaded once before the actual request + const module = require(protocol) + app = (req, res) => { - const clientRequest = require(protocol).get(`${protocol}://${req.query.host}`) + const clientRequest = module.get(`${protocol}://${req.query.host}`) clientRequest.on('error', noop) res.end('end') } @@ -232,7 +238,7 @@ describe('RASP - ssrf', () => { } }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'resources', 'rasp_rules.json'), diff --git a/packages/dd-trace/test/appsec/reporter.spec.js b/packages/dd-trace/test/appsec/reporter.spec.js index 898f8e533c8..f556f6e1956 100644 --- a/packages/dd-trace/test/appsec/reporter.spec.js +++ b/packages/dd-trace/test/appsec/reporter.spec.js @@ -10,6 +10,11 @@ const zlib = require('node:zlib') const { storage } = require('../../../datadog-core') const { ASM } = require('../../src/standalone/product') const { USER_KEEP } = require('../../../../ext/priority') +const { getConfigFresh } = require('../helpers/config') + +function getAppSecConfig (options) { + return getConfigFresh({ appsec: options }).appsec +} describe('reporter', () => { let Reporter @@ -69,7 +74,7 @@ describe('reporter', () => { afterEach(() => { sinon.restore() - Reporter.init(defaultReporterConfig) + Reporter.init(getAppSecConfig(defaultReporterConfig)) Reporter.metricsQueue.clear() }) @@ -499,19 +504,18 @@ describe('reporter', () => { }) it('should report request body in meta struct on rasp event when enabled', () => { - Reporter.init( - { - rateLimit: 100, - extendedHeadersCollection: { - enabled: false, - redaction: true, - maxHeaders: 50 - }, - rasp: { - bodyCollection: true - } + const config = getAppSecConfig({ + rateLimit: 100, + extendedHeadersCollection: { + enabled: false, + redaction: true, + maxHeaders: 50 + }, + rasp: { + bodyCollection: true } - ) + }) + Reporter.init(config) Reporter.reportAttack({ events: [ @@ -530,19 +534,18 @@ describe('reporter', () => { }) it('should not report request body in meta struct on rasp event when disabled', () => { - Reporter.init( - { - rateLimit: 100, - extendedHeadersCollection: { - enabled: false, - redaction: true, - maxHeaders: 50 - }, - rasp: { - bodyCollection: false - } + const config = getAppSecConfig({ + rateLimit: 100, + extendedHeadersCollection: { + enabled: false, + redaction: true, + maxHeaders: 50 + }, + rasp: { + bodyCollection: false } - ) + }) + Reporter.init(config) Reporter.reportAttack({ events: [ @@ -612,19 +615,18 @@ describe('reporter', () => { }) it('should set request body size exceeded when reporter request body has been truncated', () => { - Reporter.init( - { - rateLimit: 100, - extendedHeadersCollection: { - enabled: false, - redaction: true, - maxHeaders: 50 - }, - rasp: { - bodyCollection: true - } + const config = getAppSecConfig({ + rateLimit: 100, + extendedHeadersCollection: { + enabled: false, + redaction: true, + maxHeaders: 50 + }, + rasp: { + bodyCollection: true } - ) + }) + Reporter.init(config) req.body = requestBody @@ -645,19 +647,18 @@ describe('reporter', () => { }) it('should set request body size exceeded metric for old and new approaches when both events happen', () => { - Reporter.init( - { - rateLimit: 100, - extendedHeadersCollection: { - enabled: false, - redaction: true, - maxHeaders: 50 - }, - rasp: { - bodyCollection: true - } + const config = getAppSecConfig({ + rateLimit: 100, + extendedHeadersCollection: { + enabled: false, + redaction: true, + maxHeaders: 50 + }, + rasp: { + bodyCollection: true } - ) + }) + Reporter.init(config) req.body = requestBody @@ -1100,7 +1101,7 @@ describe('reporter', () => { })) after(() => { - Reporter.init(defaultReporterConfig) + Reporter.init(getAppSecConfig(defaultReporterConfig)) }) it('should collect extended headers on appsec event', () => { @@ -1118,7 +1119,7 @@ describe('reporter', () => { } span.context()._tags['appsec.event'] = 'true' - Reporter.init({ + const config = getAppSecConfig({ rateLimit: 100, extendedHeadersCollection: { enabled: true, @@ -1129,6 +1130,7 @@ describe('reporter', () => { bodyCollection: false } }) + Reporter.init(config) Reporter.finishRequest(req, res) const expectedTags = { @@ -1161,19 +1163,19 @@ describe('reporter', () => { const discardedReqHeadersCount = extendedRequestHeaders.length - reportedExtReqHeadersCount const discardedResHeadersCount = extendedResponseHeaders.length - maxHeaders - Reporter.init( - { - rateLimit: 100, - extendedHeadersCollection: { - enabled: true, - redaction: false, - maxHeaders - }, - rasp: { - bodyCollection: false - } + const config = getAppSecConfig({ + rateLimit: 100, + extendedHeadersCollection: { + enabled: true, + redaction: false, + maxHeaders + }, + rasp: { + bodyCollection: false } - ) + }) + + Reporter.init(config) Reporter.finishRequest(req, res) const expectedTags = { @@ -1207,7 +1209,7 @@ describe('reporter', () => { } span.context()._tags['appsec.event'] = 'true' - Reporter.init({ + const config = getAppSecConfig({ rateLimit: 100, extendedHeadersCollection: { enabled: true, @@ -1218,6 +1220,8 @@ describe('reporter', () => { bodyCollection: false } }) + + Reporter.init(config) Reporter.finishRequest(req, res) const expectedTags = { diff --git a/packages/dd-trace/test/appsec/response_blocking.spec.js b/packages/dd-trace/test/appsec/response_blocking.spec.js index 8d28ba46e7a..db7311d485d 100644 --- a/packages/dd-trace/test/appsec/response_blocking.spec.js +++ b/packages/dd-trace/test/appsec/response_blocking.spec.js @@ -4,12 +4,13 @@ const Axios = require('axios') const { assert } = require('chai') const { describe, it, beforeEach, afterEach, before, after } = require('mocha') const sinon = require('sinon') + const path = require('node:path') const fs = require('node:fs') const agent = require('../plugins/agent') const appsec = require('../../src/appsec') -const Config = require('../../src/config') +const { getConfigFresh } = require('../helpers/config') const WafContext = require('../../src/appsec/waf/waf_context_wrapper') const blockingResponse = JSON.parse(require('../../src/appsec/blocked_templates').json) @@ -52,7 +53,7 @@ describe('HTTP Response Blocking', () => { .once('error', reject) }) - appsec.enable(new Config({ + appsec.enable(getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, 'response_blocking_rules.json'), diff --git a/packages/dd-trace/test/appsec/rule_manager.spec.js b/packages/dd-trace/test/appsec/rule_manager.spec.js index f62f8bed94f..3e8e7b37f3c 100644 --- a/packages/dd-trace/test/appsec/rule_manager.spec.js +++ b/packages/dd-trace/test/appsec/rule_manager.spec.js @@ -8,19 +8,19 @@ const fs = require('node:fs') const path = require('node:path') const { loadRules, clearAllRules } = require('../../src/appsec/rule_manager') -const Config = require('../../src/config') const { ACKNOWLEDGED, UNACKNOWLEDGED, ERROR } = require('../../src/remote_config/apply_states') const rules = require('../../src/appsec/recommended.json') const waf = require('../../src/appsec/waf') const blocking = require('../../src/appsec/blocking') +const { getConfigFresh } = require('../helpers/config') describe('AppSec Rule Manager', () => { let config beforeEach(() => { clearAllRules() - config = new Config() + config = getConfigFresh() sinon.stub(waf, 'init') sinon.stub(waf, 'destroy') @@ -165,7 +165,7 @@ describe('AppSec Rule Manager', () => { RuleManager.clearAllRules() - config = new Config() + config = getConfigFresh() RuleManager.loadRules(config.appsec) sinon.resetHistory() }) diff --git a/packages/dd-trace/test/appsec/sdk/set_user.spec.js b/packages/dd-trace/test/appsec/sdk/set_user.spec.js index f7640c69484..f74225f7ff6 100644 --- a/packages/dd-trace/test/appsec/sdk/set_user.spec.js +++ b/packages/dd-trace/test/appsec/sdk/set_user.spec.js @@ -10,7 +10,7 @@ const path = require('node:path') const agent = require('../../plugins/agent') const tracer = require('../../../../../index') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') describe('set_user', () => { describe('Internal API', () => { @@ -94,7 +94,7 @@ describe('set_user', () => { }) describe('Integration with the tracer', () => { - const config = new Config({ + const config = getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, './user_blocking_rules.json') diff --git a/packages/dd-trace/test/appsec/sdk/user_blocking-integration.spec.js b/packages/dd-trace/test/appsec/sdk/user_blocking-integration.spec.js index e930274e3ce..5747016624f 100644 --- a/packages/dd-trace/test/appsec/sdk/user_blocking-integration.spec.js +++ b/packages/dd-trace/test/appsec/sdk/user_blocking-integration.spec.js @@ -6,13 +6,13 @@ const { describe, it, beforeEach, afterEach } = require('mocha') const agent = require('../../plugins/agent') const tracer = require('../../../../../index') const appsec = require('../../../src/appsec') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const axios = require('axios') const path = require('path') const blocking = require('../../../src/appsec/blocking') describe('user_blocking - Integration with the tracer', () => { - const config = new Config({ + const config = getConfigFresh({ appsec: { enabled: true, rules: path.join(__dirname, './user_blocking_rules.json') diff --git a/packages/dd-trace/test/appsec/sdk/utils.spec.js b/packages/dd-trace/test/appsec/sdk/utils.spec.js index 157d69e4411..44823804779 100644 --- a/packages/dd-trace/test/appsec/sdk/utils.spec.js +++ b/packages/dd-trace/test/appsec/sdk/utils.spec.js @@ -4,14 +4,14 @@ const { assert } = require('chai') const { getRootSpan } = require('../../../src/appsec/sdk/utils') const DatadogTracer = require('../../../src/tracer') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const id = require('../../../src/id') describe('Appsec SDK utils', () => { let tracer before(() => { - tracer = new DatadogTracer(new Config({ + tracer = new DatadogTracer(getConfigFresh({ enabled: true })) }) diff --git a/packages/dd-trace/test/appsec/telemetry/index.spec.js b/packages/dd-trace/test/appsec/telemetry/index.spec.js index 3a5e9464a58..a08182b856b 100644 --- a/packages/dd-trace/test/appsec/telemetry/index.spec.js +++ b/packages/dd-trace/test/appsec/telemetry/index.spec.js @@ -4,7 +4,7 @@ const { assert } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') const sinon = require('sinon') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const appsecTelemetry = require('../../../src/appsec/telemetry') const telemetryMetrics = require('../../../src/telemetry/metrics') @@ -38,7 +38,7 @@ describe('appsec enabled metric', () => { }) it('should not gauge nor interval', () => { - const config = new Config() + const config = getConfigFresh() global.setInterval = sinon.stub() appsecTelemetry.enable(config) @@ -53,7 +53,7 @@ describe('appsec enabled metric', () => { }) it('should call to gauge.track metric when is enabled by remote config', () => { - const config = new Config() + const config = getConfigFresh() appsecTelemetry.enable(config) @@ -68,7 +68,7 @@ describe('appsec enabled metric', () => { it('should call to gauge.track metric when is enabled by environment variable', () => { process.env.DD_APPSEC_ENABLED = 'true' - const config = new Config() + const config = getConfigFresh() appsecTelemetry.enable(config) @@ -82,7 +82,7 @@ describe('appsec enabled metric', () => { }) it('should call to gauge.track metric when is enabled by code', () => { - const config = new Config({ appsec: true }) + const config = getConfigFresh({ appsec: true }) appsecTelemetry.enable(config) @@ -96,7 +96,7 @@ describe('appsec enabled metric', () => { }) it('should call to gauge.track metric with unknown where is calculated', () => { - const config = new Config({ appsec: true }) + const config = getConfigFresh({ appsec: true }) config.getOrigin = () => 'calculated' appsecTelemetry.enable(config) @@ -115,7 +115,7 @@ describe('appsec enabled metric', () => { global.setInterval = sinon.stub() global.setInterval.returns({ unref }) - const config = new Config() + const config = getConfigFresh() config.telemetry.heartbeatInterval = 10_000 // in milliseconds appsecTelemetry.enable(config) diff --git a/packages/dd-trace/test/appsec/telemetry/rasp.spec.js b/packages/dd-trace/test/appsec/telemetry/rasp.spec.js index f44b8d77e75..c2b3ddeab95 100644 --- a/packages/dd-trace/test/appsec/telemetry/rasp.spec.js +++ b/packages/dd-trace/test/appsec/telemetry/rasp.spec.js @@ -8,7 +8,7 @@ const telemetryMetrics = require('../../../src/telemetry/metrics') const appsecNamespace = telemetryMetrics.manager.namespace('appsec') const appsecTelemetry = require('../../../src/appsec/telemetry') -const Config = require('../../../src/config') +const getConfig = require('../../../src/config') describe('Appsec Rasp Telemetry metrics', () => { const wafVersion = '0.0.1' @@ -31,7 +31,7 @@ describe('Appsec Rasp Telemetry metrics', () => { describe('if enabled', () => { beforeEach(() => { - const config = new Config() + const config = getConfig() config.telemetry.enabled = true config.telemetry.metrics = true diff --git a/packages/dd-trace/test/appsec/telemetry/user.spec.js b/packages/dd-trace/test/appsec/telemetry/user.spec.js index d434e6f41e8..070d3205d36 100644 --- a/packages/dd-trace/test/appsec/telemetry/user.spec.js +++ b/packages/dd-trace/test/appsec/telemetry/user.spec.js @@ -8,7 +8,7 @@ const telemetryMetrics = require('../../../src/telemetry/metrics') const appsecNamespace = telemetryMetrics.manager.namespace('appsec') const appsecTelemetry = require('../../../src/appsec/telemetry') -const Config = require('../../../src/config') +const getConfig = require('../../../src/config') describe('Appsec User Telemetry metrics', () => { let count, inc @@ -27,7 +27,7 @@ describe('Appsec User Telemetry metrics', () => { describe('if enabled', () => { beforeEach(() => { - const config = new Config() + const config = getConfig() config.telemetry.enabled = true config.telemetry.metrics = true diff --git a/packages/dd-trace/test/appsec/telemetry/waf.spec.js b/packages/dd-trace/test/appsec/telemetry/waf.spec.js index 99805494b62..934533a81cd 100644 --- a/packages/dd-trace/test/appsec/telemetry/waf.spec.js +++ b/packages/dd-trace/test/appsec/telemetry/waf.spec.js @@ -8,7 +8,7 @@ const telemetryMetrics = require('../../../src/telemetry/metrics') const appsecNamespace = telemetryMetrics.manager.namespace('appsec') const appsecTelemetry = require('../../../src/appsec/telemetry') -const Config = require('../../../src/config') +const getConfig = require('../../../src/config') describe('Appsec Waf Telemetry metrics', () => { const wafVersion = '0.0.1' @@ -41,7 +41,7 @@ describe('Appsec Waf Telemetry metrics', () => { } beforeEach(() => { - const config = new Config() + const config = getConfig() config.telemetry.enabled = true config.telemetry.metrics = true diff --git a/packages/dd-trace/test/appsec/utils.js b/packages/dd-trace/test/appsec/utils.js index 311ccd0184e..0983531abe9 100644 --- a/packages/dd-trace/test/appsec/utils.js +++ b/packages/dd-trace/test/appsec/utils.js @@ -9,7 +9,7 @@ function getWebSpan (traces) { } } - throw new Error('web span not found') + throw new Error('Sanity check failed: web span not found', { cause: traces }) } function createDeepObject (sheetValue, currentLevel = 1, max = 20) { diff --git a/packages/dd-trace/test/appsec/waf/index.spec.js b/packages/dd-trace/test/appsec/waf/index.spec.js index 7d2194d9fdd..57731b3ec0e 100644 --- a/packages/dd-trace/test/appsec/waf/index.spec.js +++ b/packages/dd-trace/test/appsec/waf/index.spec.js @@ -5,7 +5,7 @@ const { expect } = require('chai') const { describe, it, beforeEach, afterEach } = require('mocha') const sinon = require('sinon') const proxyquire = require('proxyquire') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const rules = require('../../../src/appsec/recommended.json') const Reporter = require('../../../src/appsec/reporter') const { match } = require('sinon') @@ -26,7 +26,7 @@ describe('WAF Manager', () => { let keepTrace, updateRateLimitedMetric, limiterStub beforeEach(() => { - config = new Config() + config = getConfigFresh() limiterStub = { isAllowed: sinon.stub().returns(true) diff --git a/packages/dd-trace/test/ci-visibility/dynamic-instrumentation/target-app/test-visibility-dynamic-instrumentation-script.js b/packages/dd-trace/test/ci-visibility/dynamic-instrumentation/target-app/test-visibility-dynamic-instrumentation-script.js index 27a9b1ec30f..c856c865c78 100644 --- a/packages/dd-trace/test/ci-visibility/dynamic-instrumentation/target-app/test-visibility-dynamic-instrumentation-script.js +++ b/packages/dd-trace/test/ci-visibility/dynamic-instrumentation/target-app/test-visibility-dynamic-instrumentation-script.js @@ -3,12 +3,12 @@ const path = require('path') const getDiClient = require('../../../../src/ci-visibility/dynamic-instrumentation') const sum = require('./di-dependency') -const Config = require('../../../../src/config') +const getConfig = require('../../../../src/config') // keep process alive const intervalId = setInterval(() => {}, 5000) -const diClient = getDiClient(new Config()) +const diClient = getDiClient(getConfig()) diClient.start() diff --git a/packages/dd-trace/test/config.spec.js b/packages/dd-trace/test/config.spec.js index 8edd0106aaf..4ad8e7f2a7c 100644 --- a/packages/dd-trace/test/config.spec.js +++ b/packages/dd-trace/test/config.spec.js @@ -5,7 +5,7 @@ const sinon = require('sinon') const { it, describe, beforeEach, afterEach, context } = require('tap').mocha const proxyquire = require('proxyquire') -const { readFileSync } = require('node:fs') +const { readFileSync, mkdtempSync, rmSync, writeFileSync } = require('node:fs') const assert = require('node:assert/strict') const { once } = require('node:events') const path = require('node:path') @@ -18,7 +18,7 @@ const { assertObjectContains } = require('../../../integration-tests/helpers') const { DD_MAJOR } = require('../../../version') describe('Config', () => { - let Config + let getConfig let log let pkg let env @@ -49,13 +49,14 @@ describe('Config', () => { './pkg': pkg }) - Config = proxyquire('../src/config', { + // Reload the config module with each call to getConfig to ensure we get a new instance of the config. + getConfig = (options) => proxyquire.noPreserveCache()('../src/config', { './config_defaults': configDefaults, './log': log, './telemetry': { updateConfig }, fs, os - }) + })(options) } beforeEach(() => { @@ -72,7 +73,10 @@ describe('Config', () => { existsSync: (param) => { existsSyncParam = param return existsSyncReturn - } + }, + rmSync, + mkdtempSync, + writeFileSync, } os = { type () { @@ -125,7 +129,7 @@ describe('Config', () => { it('should set new runtimeMetricsRuntimeId from deprecated DD_TRACE_EXPERIMENTAL_RUNTIME_ID_ENABLED', async () => { process.env.DD_TRACE_EXPERIMENTAL_RUNTIME_ID_ENABLED = 'true' assert.strictEqual(process.env.DD_RUNTIME_METRICS_RUNTIME_ID_ENABLED, undefined) - const config = new Config() + const config = getConfig() expect(config).to.have.property('runtimeMetricsRuntimeId', true) assert.strictEqual(getEnvironmentVariable('DD_RUNTIME_METRICS_RUNTIME_ID_ENABLED'), 'true') delete process.env.DD_TRACE_EXPERIMENTAL_RUNTIME_ID_ENABLED @@ -153,7 +157,7 @@ describe('Config', () => { reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.property('debug', true) expect(config).to.have.property('logger', undefined) @@ -182,7 +186,7 @@ describe('Config', () => { // required if we want to check updates to config.debug and config.logLevel which is fetched from logger reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.property('debug', false) expect(config).to.have.property('service', 'service') @@ -212,7 +216,7 @@ describe('Config', () => { // required if we want to check updates to config.debug and config.logLevel which is fetched from logger reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.property('debug', true) expect(config).to.have.property('service', 'otel_service') @@ -233,7 +237,7 @@ describe('Config', () => { it('should correctly map OTEL_RESOURCE_ATTRIBUTES', () => { process.env.OTEL_RESOURCE_ATTRIBUTES = 'deployment.environment=test1,service.name=test2,service.version=5,foo=bar1,baz=qux1' - const config = new Config() + const config = getConfig() expect(config).to.have.property('env', 'test1') expect(config).to.have.property('service', 'test2') @@ -244,32 +248,32 @@ describe('Config', () => { it('should correctly map OTEL_TRACES_SAMPLER and OTEL_TRACES_SAMPLER_ARG', () => { process.env.OTEL_TRACES_SAMPLER = 'always_on' process.env.OTEL_TRACES_SAMPLER_ARG = '0.1' - let config = new Config() + let config = getConfig() expect(config).to.have.property('sampleRate', 1.0) process.env.OTEL_TRACES_SAMPLER = 'always_off' - config = new Config() + config = getConfig() expect(config).to.have.property('sampleRate', 0.0) process.env.OTEL_TRACES_SAMPLER = 'traceidratio' - config = new Config() + config = getConfig() expect(config).to.have.property('sampleRate', 0.1) process.env.OTEL_TRACES_SAMPLER = 'parentbased_always_on' - config = new Config() + config = getConfig() expect(config).to.have.property('sampleRate', 1.0) process.env.OTEL_TRACES_SAMPLER = 'parentbased_always_off' - config = new Config() + config = getConfig() expect(config).to.have.property('sampleRate', 0.0) process.env.OTEL_TRACES_SAMPLER = 'parentbased_traceidratio' - config = new Config() + config = getConfig() expect(config).to.have.property('sampleRate', 0.1) }) it('should initialize with the correct defaults', () => { - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('apmTracingEnabled', true) expect(config).to.have.property('appKey', undefined) @@ -517,7 +521,7 @@ describe('Config', () => { }) it('should support logging', () => { - const config = new Config({ + const config = getConfig({ logger: {}, debug: true }) @@ -527,7 +531,7 @@ describe('Config', () => { }) it('should not warn on undefined DD_TRACE_SPAN_ATTRIBUTE_SCHEMA', () => { - const config = new Config({ + const config = getConfig({ logger: {}, debug: true }) @@ -539,7 +543,7 @@ describe('Config', () => { pkg.name = 'test' reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.property('service', 'test') expect(config.tags).to.have.property('service', 'test') @@ -549,7 +553,7 @@ describe('Config', () => { pkg.version = '1.2.3' reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.property('version', '1.2.3') expect(config.tags).to.have.property('version', '1.2.3') @@ -678,7 +682,7 @@ describe('Config', () => { // required if we want to check updates to config.debug and config.logLevel which is fetched from logger reloadLoggerAndConfig() - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('apmTracingEnabled', false) expect(config).to.have.property('appKey', 'myAppKey') @@ -895,7 +899,7 @@ describe('Config', () => { it('should ignore empty strings', () => { process.env.DD_TAGS = 'service:,env:,version:' - let config = new Config() + let config = getConfig() expect(config).to.have.property('service', 'node') expect(config).to.have.property('env', undefined) @@ -903,7 +907,7 @@ describe('Config', () => { process.env.DD_TAGS = 'service: env: version:' - config = new Config() + config = getConfig() expect(config).to.have.property('service', 'node') expect(config).to.have.property('env', undefined) @@ -913,13 +917,13 @@ describe('Config', () => { it('should support space separated tags when experimental mode enabled', () => { process.env.DD_TAGS = 'key1:value1 key2:value2' - let config = new Config() + let config = getConfig() expect(config.tags).to.include({ key1: 'value1', key2: 'value2' }) process.env.DD_TAGS = 'env:test aKey:aVal bKey:bVal cKey:' - config = new Config() + config = getConfig() expect(config.tags).to.have.property('env', 'test') expect(config.tags).to.have.property('aKey', 'aVal') @@ -928,20 +932,20 @@ describe('Config', () => { process.env.DD_TAGS = 'env:test,aKey:aVal bKey:bVal cKey:' - config = new Config() + config = getConfig() expect(config.tags).to.have.property('env', 'test') expect(config.tags).to.have.property('aKey', 'aVal bKey:bVal cKey:') process.env.DD_TAGS = 'a:b:c:d' - config = new Config() + config = getConfig() expect(config.tags).to.have.property('a', 'b:c:d') process.env.DD_TAGS = 'a,1' - config = new Config() + config = getConfig() expect(config.tags).to.have.property('a', '') expect(config.tags).to.have.property('1', '') @@ -952,7 +956,7 @@ describe('Config', () => { process.env.DD_TRACE_PROPAGATION_EXTRACT_FIRST = 'TRUE' process.env.DD_RUNTIME_METRICS_ENABLED = '0' - const config = new Config() + const config = getConfig() expect(config).to.have.property('tracing', false) expect(config).to.have.property('tracePropagationExtractFirst', true) @@ -968,7 +972,7 @@ describe('Config', () => { process.env.DD_SERVICE = 'service' process.env.DD_ENV = 'test' - const config = new Config() + const config = getConfig() expect(config).to.have.property('tracing', false) expect(config).to.have.nested.property('dogstatsd.hostname', 'agent') @@ -985,7 +989,7 @@ describe('Config', () => { process.env.DD_TRACE_PROPAGATION_STYLE_INJECT = 'tracecontext' process.env.DD_TRACE_PROPAGATION_STYLE_EXTRACT = 'tracecontext' - const config = new Config() + const config = getConfig() expect(config).to.have.nested.deep.property('tracePropagationStyle.inject', ['tracecontext']) expect(config).to.have.nested.deep.property('tracePropagationStyle.extract', ['tracecontext']) @@ -994,7 +998,7 @@ describe('Config', () => { it('should enable crash tracking for SSI by default', () => { process.env.DD_INJECTION_ENABLED = 'tracer' - const config = new Config() + const config = getConfig() expect(config).to.have.nested.deep.property('crashtracking.enabled', true) }) @@ -1003,7 +1007,7 @@ describe('Config', () => { process.env.DD_CRASHTRACKING_ENABLED = 'false' process.env.DD_INJECTION_ENABLED = 'tracer' - const config = new Config() + const config = getConfig() expect(config).to.have.nested.deep.property('crashtracking.enabled', false) }) @@ -1012,7 +1016,7 @@ describe('Config', () => { process.env.DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE = 'anonymous' process.env.DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING = 'extended' - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('appsec.eventTracking.mode', 'anonymous') }) @@ -1029,7 +1033,7 @@ describe('Config', () => { { service: 'authsvc', sampleRate: 1.0 }, { sampleRate: 0.1 } ] - const config = new Config({ + const config = getConfig({ appsec: false, clientIpEnabled: true, clientIpHeader: 'x-true-client-ip', @@ -1300,7 +1304,7 @@ describe('Config', () => { it('should initialize from the options with url taking precedence', () => { const logger = {} const tags = { foo: 'bar' } - const config = new Config({ + const config = getConfig({ hostname: 'agent', url: 'https://agent2:7777', site: 'datadoghq.eu', @@ -1333,8 +1337,7 @@ describe('Config', () => { process.env.DD_TRACE_PROPAGATION_STYLE_EXTRACT = 'datadog' process.env.DD_TRACE_PROPAGATION_STYLE = 'datadog' - // eslint-disable-next-line no-new - new Config() + getConfig() expect(log.warn).to.have.been.calledWith('Use either the DD_TRACE_PROPAGATION_STYLE ' + 'environment variable or separate DD_TRACE_PROPAGATION_STYLE_INJECT and ' + @@ -1345,8 +1348,7 @@ describe('Config', () => { process.env.DD_TRACE_PROPAGATION_STYLE_INJECT = 'datadog' process.env.DD_TRACE_PROPAGATION_STYLE = 'datadog' - // eslint-disable-next-line no-new - new Config() + getConfig() expect(log.warn).to.have.been.calledWith('Use either the DD_TRACE_PROPAGATION_STYLE ' + 'environment variable or separate DD_TRACE_PROPAGATION_STYLE_INJECT and ' + @@ -1356,7 +1358,7 @@ describe('Config', () => { it('should warn if defaulting to v0 span attribute schema', () => { process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = 'foo' - const config = new Config() + const config = getConfig() expect(log.warn).to.have.been.calledWith('Unexpected input for config.spanAttributeSchema, picked default', 'v0') expect(config).to.have.property('spanAttributeSchema', 'v0') @@ -1366,7 +1368,7 @@ describe('Config', () => { process.env.DD_GRPC_CLIENT_ERROR_STATUSES = '3,13,400-403' process.env.DD_GRPC_SERVER_ERROR_STATUSES = '3,13,400-403' - let config = new Config() + let config = getConfig() expect(config.grpc.client.error.statuses).to.deep.equal([3, 13, 400, 401, 402, 403]) expect(config.grpc.server.error.statuses).to.deep.equal([3, 13, 400, 401, 402, 403]) @@ -1374,7 +1376,7 @@ describe('Config', () => { process.env.DD_GRPC_CLIENT_ERROR_STATUSES = '1' process.env.DD_GRPC_SERVER_ERROR_STATUSES = '1' - config = new Config() + config = getConfig() expect(config.grpc.client.error.statuses).to.deep.equal([1]) expect(config.grpc.server.error.statuses).to.deep.equal([1]) @@ -1382,7 +1384,7 @@ describe('Config', () => { process.env.DD_GRPC_CLIENT_ERROR_STATUSES = '2,10,13-15' process.env.DD_GRPC_SERVER_ERROR_STATUSES = '2,10,13-15' - config = new Config() + config = getConfig() expect(config.grpc.client.error.statuses).to.deep.equal([2, 10, 13, 14, 15]) expect(config.grpc.server.error.statuses).to.deep.equal([2, 10, 13, 14, 15]) @@ -1392,38 +1394,38 @@ describe('Config', () => { it('should activate peer service only if explicitly true in v0', () => { process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = 'v0' process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'true' - let config = new Config() + let config = getConfig() expect(config).to.have.property('spanComputePeerService', true) process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'foo' - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', false) process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'false' - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', false) delete process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', false) }) it('should activate peer service in v1 unless explicitly false', () => { process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = 'v1' process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'false' - let config = new Config() + let config = getConfig() expect(config).to.have.property('spanComputePeerService', false) process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'foo' - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', true) process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'true' - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', true) delete process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED - config = new Config() + config = getConfig() expect(config).to.have.property('spanComputePeerService', true) }) }) @@ -1434,7 +1436,7 @@ describe('Config', () => { process.env.DD_TRACE_GLOBAL_TAGS = 'foo:foo' process.env.DD_TAGS = 'foo:bar,baz:qux' - const config = new Config() + const config = getConfig() expect(config).to.have.property('hostname', 'agent') expect(config.tags).to.include({ foo: 'foo', baz: 'qux' }) @@ -1518,7 +1520,7 @@ describe('Config', () => { process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = 'v0' process.env.DD_VERSION = '0.0.0' - const config = new Config({ + const config = getConfig({ apmTracingEnabled: true, appsec: { apiSecurity: { @@ -1725,7 +1727,7 @@ describe('Config', () => { }) it('should give priority to non-experimental options', () => { - const config = new Config({ + const config = getConfig({ appsec: { apiSecurity: { enabled: true, @@ -1879,7 +1881,7 @@ describe('Config', () => { process.env.DD_SERVICE_NAME = 'service' process.env.DD_ENV = 'test' - const config = new Config({ + const config = getConfig({ url: 'https://agent3:7778', protocol: 'http', hostname: 'server', @@ -1901,7 +1903,7 @@ describe('Config', () => { process.env.DD_VERSION = '1.0.0' process.env.DD_TAGS = 'service=foo,env=bar,version=0.0.0' - const config = new Config() + const config = getConfig() expect(config.tags).to.include({ service: 'test', @@ -1911,15 +1913,15 @@ describe('Config', () => { }) it('should sanitize the sample rate to be between 0 and 1', () => { - expect(new Config({ sampleRate: -1 })).to.have.property('sampleRate', 0) - expect(new Config({ sampleRate: 2 })).to.have.property('sampleRate', 1) - expect(new Config({ sampleRate: NaN })).to.have.property('sampleRate', undefined) + expect(getConfig({ sampleRate: -1 })).to.have.property('sampleRate', 0) + expect(getConfig({ sampleRate: 2 })).to.have.property('sampleRate', 1) + expect(getConfig({ sampleRate: NaN })).to.have.property('sampleRate', undefined) }) it('should ignore empty service names', () => { process.env.DD_SERVICE = '' - const config = new Config() + const config = getConfig() expect(config.tags).to.include({ service: 'node' @@ -1927,7 +1929,7 @@ describe('Config', () => { }) it('should support tags for setting primary fields', () => { - const config = new Config({ + const config = getConfig({ tags: { service: 'service', env: 'test', @@ -1943,14 +1945,14 @@ describe('Config', () => { it('should trim whitespace characters around keys', () => { process.env.DD_TAGS = 'foo:bar, baz:qux' - const config = new Config() + const config = getConfig() expect(config.tags).to.include({ foo: 'bar', baz: 'qux' }) }) it('should not transform the lookup parameter', () => { const lookup = () => 'test' - const config = new Config({ lookup }) + const config = getConfig({ lookup }) expect(config.lookup).to.equal(lookup) }) @@ -1958,7 +1960,7 @@ describe('Config', () => { it('should not set DD_INSTRUMENTATION_TELEMETRY_ENABLED if AWS_LAMBDA_FUNCTION_NAME is present', () => { process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-great-lambda-function' - const config = new Config() + const config = getConfig() expect(config.telemetry.enabled).to.be.false }) @@ -1968,7 +1970,7 @@ describe('Config', () => { process.env.FUNCTION_NAME = 'function_name' process.env.GCP_PROJECT = 'project_name' - const config = new Config() + const config = getConfig() expect(config.telemetry.enabled).to.be.false }) @@ -1978,7 +1980,7 @@ describe('Config', () => { process.env.K_SERVICE = 'function_name' process.env.FUNCTION_TARGET = 'function_target' - const config = new Config() + const config = getConfig() expect(config.telemetry.enabled).to.be.false }) @@ -1989,13 +1991,13 @@ describe('Config', () => { process.env.FUNCTIONS_EXTENSION_VERSION = '4' process.env.WEBSITE_SKU = 'Dynamic' - const config = new Config() + const config = getConfig() expect(config.telemetry.enabled).to.be.false }) it('should set telemetry default values', () => { - const config = new Config() + const config = getConfig() expect(config.telemetry).to.not.be.undefined expect(config.telemetry.enabled).to.be.true @@ -2009,7 +2011,7 @@ describe('Config', () => { const origTelemetryHeartbeatIntervalValue = process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL = '42' - const config = new Config() + const config = getConfig() expect(config.telemetry.heartbeatInterval).to.eq(42000) @@ -2020,7 +2022,7 @@ describe('Config', () => { const origTraceTelemetryValue = process.env.DD_INSTRUMENTATION_TELEMETRY_ENABLED process.env.DD_INSTRUMENTATION_TELEMETRY_ENABLED = 'false' - const config = new Config() + const config = getConfig() expect(config.telemetry.enabled).to.be.false @@ -2031,7 +2033,7 @@ describe('Config', () => { const origTelemetryMetricsEnabledValue = process.env.DD_TELEMETRY_METRICS_ENABLED process.env.DD_TELEMETRY_METRICS_ENABLED = 'false' - const config = new Config() + const config = getConfig() expect(config.telemetry.metrics).to.be.false @@ -2042,7 +2044,7 @@ describe('Config', () => { const origLogsValue = process.env.DD_TELEMETRY_LOG_COLLECTION_ENABLED process.env.DD_TELEMETRY_LOG_COLLECTION_ENABLED = 'false' - const config = new Config() + const config = getConfig() expect(config.telemetry.logCollection).to.be.false @@ -2053,7 +2055,7 @@ describe('Config', () => { const origTelemetryDebugValue = process.env.DD_TELEMETRY_DEBUG process.env.DD_TELEMETRY_DEBUG = 'true' - const config = new Config() + const config = getConfig() expect(config.telemetry.debug).to.be.true @@ -2063,7 +2065,7 @@ describe('Config', () => { it('should not set DD_REMOTE_CONFIGURATION_ENABLED if AWS_LAMBDA_FUNCTION_NAME is present', () => { process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-great-lambda-function' - const config = new Config() + const config = getConfig() expect(config.remoteConfig.enabled).to.be.false }) @@ -2072,7 +2074,7 @@ describe('Config', () => { process.env.FUNCTION_NAME = 'function_name' process.env.GCP_PROJECT = 'project_name' - const config = new Config() + const config = getConfig() expect(config.remoteConfig.enabled).to.be.false }) @@ -2081,7 +2083,7 @@ describe('Config', () => { process.env.K_SERVICE = 'function_name' process.env.FUNCTION_TARGET = 'function_target' - const config = new Config() + const config = getConfig() expect(config.remoteConfig.enabled).to.be.false }) @@ -2091,13 +2093,13 @@ describe('Config', () => { process.env.FUNCTIONS_EXTENSION_VERSION = '4' process.env.WEBSITE_SKU = 'Dynamic' - const config = new Config() + const config = getConfig() expect(config.remoteConfig.enabled).to.be.false }) it('should send empty array when remote config is called on empty options', () => { - const config = new Config() + const config = getConfig() config.configure({}, true) @@ -2106,7 +2108,7 @@ describe('Config', () => { }) it('should send remote config changes to telemetry', () => { - const config = new Config() + const config = getConfig() config.configure({ tracing_sampling_rate: 0 @@ -2118,7 +2120,7 @@ describe('Config', () => { }) it('should reformat tags from sampling rules when set through remote configuration', () => { - const config = new Config() + const config = getConfig() config.configure({ tracing_sampling_rules: [ @@ -2147,7 +2149,7 @@ describe('Config', () => { }) it('should have consistent runtime-id after remote configuration updates tags', () => { - const config = new Config() + const config = getConfig() const runtimeId = config.tags['runtime-id'] config.configure({ tracing_tags: { foo: 'bar' } @@ -2158,7 +2160,7 @@ describe('Config', () => { }) it('should ignore invalid iast.requestSampling', () => { - const config = new Config({ + const config = getConfig({ experimental: { iast: { requestSampling: 105 @@ -2172,7 +2174,7 @@ describe('Config', () => { const path = './fixtures/config/span-sampling-rules.json' process.env.DD_SPAN_SAMPLING_RULES_FILE = require.resolve(path) - const config = new Config() + const config = getConfig() expect(config.sampler).to.have.deep.nested.property('spanSamplingRules', [ { service: 'mysql', name: 'mysql.query', sampleRate: 0.0, maxPerSecond: 1 }, @@ -2186,7 +2188,7 @@ describe('Config', () => { const error = new Error('file not found') fs.readFileSync = () => { throw error } - const config = new Config({ + const config = getConfig({ appsec: { enabled: true, rules: 'path/to/rules.json', @@ -2214,7 +2216,7 @@ describe('Config', () => { it('should enable api security with DD_EXPERIMENTAL_API_SECURITY_ENABLED', () => { process.env.DD_EXPERIMENTAL_API_SECURITY_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config.appsec.apiSecurity.enabled).to.be.true }) @@ -2222,7 +2224,7 @@ describe('Config', () => { it('should disable api security with DD_EXPERIMENTAL_API_SECURITY_ENABLED', () => { process.env.DD_EXPERIMENTAL_API_SECURITY_ENABLED = 'false' - const config = new Config() + const config = getConfig() expect(config.appsec.apiSecurity.enabled).to.be.false }) @@ -2231,7 +2233,7 @@ describe('Config', () => { process.env.DD_EXPERIMENTAL_API_SECURITY_ENABLED = 'false' process.env.DD_API_SECURITY_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config.appsec.apiSecurity.enabled).to.be.true }) @@ -2240,7 +2242,7 @@ describe('Config', () => { process.env.DD_DOGSTATSD_HOSTNAME = 'dsd-agent' process.env.DD_DOGSTATSD_HOST = 'localhost' - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('dogstatsd.hostname', 'localhost') }) @@ -2249,14 +2251,14 @@ describe('Config', () => { context('on windows', () => { it('should not be used', () => { osType = 'Windows_NT' - const config = new Config() + const config = getConfig() expect(config.url).to.be.undefined }) }) context('socket does not exist', () => { it('should not be used', () => { - const config = new Config() + const config = getConfig() expect(config.url).to.be.undefined }) @@ -2267,7 +2269,7 @@ describe('Config', () => { }) it('should be used when no options and no env vars', () => { - const config = new Config() + const config = getConfig() expect(existsSyncParam).to.equal('/var/run/datadog/apm.socket') expect(config.url.toString()).to.equal('unix:///var/run/datadog/apm.socket') @@ -2276,7 +2278,7 @@ describe('Config', () => { it('should not be used when DD_TRACE_AGENT_URL provided', () => { process.env.DD_TRACE_AGENT_URL = 'https://example.com/' - const config = new Config() + const config = getConfig() expect(config.url.toString()).to.equal('https://example.com/') }) @@ -2284,13 +2286,13 @@ describe('Config', () => { it('should not be used when DD_TRACE_URL provided', () => { process.env.DD_TRACE_URL = 'https://example.com/' - const config = new Config() + const config = getConfig() expect(config.url.toString()).to.equal('https://example.com/') }) it('should not be used when options.url provided', () => { - const config = new Config({ url: 'https://example.com/' }) + const config = getConfig({ url: 'https://example.com/' }) expect(config.url.toString()).to.equal('https://example.com/') }) @@ -2298,13 +2300,13 @@ describe('Config', () => { it('should not be used when DD_TRACE_AGENT_PORT provided', () => { process.env.DD_TRACE_AGENT_PORT = '12345' - const config = new Config() + const config = getConfig() expect(config.url).to.be.undefined }) it('should not be used when options.port provided', () => { - const config = new Config({ port: 12345 }) + const config = getConfig({ port: 12345 }) expect(config.url).to.be.undefined }) @@ -2312,7 +2314,7 @@ describe('Config', () => { it('should not be used when DD_TRACE_AGENT_HOSTNAME provided', () => { process.env.DD_TRACE_AGENT_HOSTNAME = 'example.com' - const config = new Config() + const config = getConfig() expect(config.url).to.be.undefined }) @@ -2320,13 +2322,13 @@ describe('Config', () => { it('should not be used when DD_AGENT_HOST provided', () => { process.env.DD_AGENT_HOST = 'example.com' - const config = new Config() + const config = getConfig() expect(config.url).to.be.undefined }) it('should not be used when options.hostname provided', () => { - const config = new Config({ hostname: 'example.com' }) + const config = getConfig({ hostname: 'example.com' }) expect(config.url).to.be.undefined }) @@ -2353,104 +2355,104 @@ describe('Config', () => { options = { isCiVisibility: true } }) it('should activate git upload by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isGitUploadEnabled', true) }) it('should disable git upload if the DD_CIVISIBILITY_GIT_UPLOAD_ENABLED is set to false', () => { process.env.DD_CIVISIBILITY_GIT_UPLOAD_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isGitUploadEnabled', false) }) it('should activate ITR by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isIntelligentTestRunnerEnabled', true) }) it('should disable ITR if DD_CIVISIBILITY_ITR_ENABLED is set to false', () => { process.env.DD_CIVISIBILITY_ITR_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isIntelligentTestRunnerEnabled', false) }) it('should enable manual testing API by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isManualApiEnabled', true) }) it('should disable manual testing API if DD_CIVISIBILITY_MANUAL_API_ENABLED is set to false', () => { process.env.DD_CIVISIBILITY_MANUAL_API_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isManualApiEnabled', false) }) it('should disable memcached command tagging by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('memcachedCommandEnabled', false) }) it('should enable memcached command tagging if DD_TRACE_MEMCACHED_COMMAND_ENABLED is enabled', () => { process.env.DD_TRACE_MEMCACHED_COMMAND_ENABLED = 'true' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('memcachedCommandEnabled', true) }) it('should enable telemetry', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.nested.property('telemetry.enabled', true) }) it('should enable early flake detection by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isEarlyFlakeDetectionEnabled', true) }) it('should disable early flake detection if DD_CIVISIBILITY_EARLY_FLAKE_DETECTION_ENABLED is false', () => { process.env.DD_CIVISIBILITY_EARLY_FLAKE_DETECTION_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isEarlyFlakeDetectionEnabled', false) }) it('should enable flaky test retries by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isFlakyTestRetriesEnabled', true) }) it('should disable flaky test retries if isFlakyTestRetriesEnabled is false', () => { process.env.DD_CIVISIBILITY_FLAKY_RETRY_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isFlakyTestRetriesEnabled', false) }) it('should read DD_CIVISIBILITY_FLAKY_RETRY_COUNT if present', () => { process.env.DD_CIVISIBILITY_FLAKY_RETRY_COUNT = '4' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('flakyTestRetriesCount', 4) }) it('should default DD_CIVISIBILITY_FLAKY_RETRY_COUNT to 5', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('flakyTestRetriesCount', 5) }) it('should round non integer values of DD_CIVISIBILITY_FLAKY_RETRY_COUNT', () => { process.env.DD_CIVISIBILITY_FLAKY_RETRY_COUNT = '4.1' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('flakyTestRetriesCount', 4) }) it('should set the default to DD_CIVISIBILITY_FLAKY_RETRY_COUNT if it is not a number', () => { process.env.DD_CIVISIBILITY_FLAKY_RETRY_COUNT = 'a' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('flakyTestRetriesCount', 5) }) it('should set the session name if DD_TEST_SESSION_NAME is set', () => { process.env.DD_TEST_SESSION_NAME = 'my-test-session' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('ciVisibilityTestSessionName', 'my-test-session') }) it('should not enable agentless log submission by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('ciVisAgentlessLogSubmissionEnabled', false) }) it('should enable agentless log submission if DD_AGENTLESS_LOG_SUBMISSION_ENABLED is true', () => { process.env.DD_AGENTLESS_LOG_SUBMISSION_ENABLED = 'true' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('ciVisAgentlessLogSubmissionEnabled', true) }) it('should set isTestDynamicInstrumentationEnabled by default', () => { - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isTestDynamicInstrumentationEnabled', true) }) it('should set isTestDynamicInstrumentationEnabled to false if DD_TEST_FAILED_TEST_REPLAY_ENABLED is false', () => { process.env.DD_TEST_FAILED_TEST_REPLAY_ENABLED = 'false' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isTestDynamicInstrumentationEnabled', false) }) }) @@ -2458,14 +2460,14 @@ describe('Config', () => { it('should not activate intelligent test runner or git metadata upload', () => { process.env.DD_CIVISIBILITY_ITR_ENABLED = 'true' process.env.DD_CIVISIBILITY_GIT_UPLOAD_ENABLED = 'true' - const config = new Config(options) + const config = getConfig(options) expect(config).to.have.property('isIntelligentTestRunnerEnabled', false) expect(config).to.have.property('isGitUploadEnabled', false) }) }) it('disables telemetry if inside a jest worker', () => { process.env.JEST_WORKER_ID = '1' - const config = new Config(options) + const config = getConfig(options) expect(config.telemetry.enabled).to.be.false }) }) @@ -2490,32 +2492,32 @@ describe('Config', () => { it('reads DD_GIT_* env vars', () => { process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) }) it('reads DD_GIT_* env vars and filters out user data', () => { process.env.DD_GIT_REPOSITORY_URL = 'https://user:password@github.com/DataDog/dd-trace-js.git' - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('repositoryUrl', 'https://github.com/DataDog/dd-trace-js.git') }) it('reads DD_TAGS env var', () => { process.env.DD_TAGS = `git.commit.sha:${DUMMY_COMMIT_SHA},git.repository_url:${DUMMY_REPOSITORY_URL}` process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) }) it('reads git.properties if it is available', () => { process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) }) it('does not crash if git.properties is not available', () => { process.env.DD_GIT_PROPERTIES_FILE = '/does/not/exist' expect(() => { - const config = new Config({}) + const config = getConfig({}) expect(config).to.be.an('object') }).to.not.throw() }) @@ -2523,39 +2525,39 @@ describe('Config', () => { process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA process.env.DD_GIT_REPOSITORY_URL = 'https://github.com:DataDog/dd-trace-js.git' - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) expect(config).to.have.property('repositoryUrl', 'https://github.com:DataDog/dd-trace-js.git') }) it('still reads git.properties if one of the env vars is missing', () => { process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) }) it('reads git.properties and filters out credentials', () => { process.env.DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties.credentials') - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') expect(config).to.have.property('repositoryUrl', 'https://github.com/datadog/dd-trace-js') }) it('does not read git metadata if DD_TRACE_GIT_METADATA_ENABLED is false', () => { process.env.DD_TRACE_GIT_METADATA_ENABLED = 'false' - const config = new Config({}) + const config = getConfig({}) expect(config).not.to.have.property('commitSHA') expect(config).not.to.have.property('repositoryUrl') }) it('reads .git/ folder if it is available', () => { process.env.DD_GIT_FOLDER_PATH = DD_GIT_FOLDER_PATH - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('repositoryUrl', 'git@github.com:DataDog/dd-trace-js.git') expect(config).to.have.property('commitSHA', '964886d9ec0c9fc68778e4abb0aab4d9982ce2b5') }) it('does not crash if .git/ folder is not available', () => { process.env.DD_GIT_FOLDER_PATH = '/does/not/exist/' expect(() => { - const config = new Config({}) + const config = getConfig({}) expect(config).to.be.an('object') }).to.not.throw() }) @@ -2563,14 +2565,14 @@ describe('Config', () => { process.env.DD_GIT_FOLDER_PATH = DD_GIT_FOLDER_PATH process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA process.env.DD_GIT_REPOSITORY_URL = 'https://github.com:DataDog/dd-trace-js.git' - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) expect(config).to.have.property('repositoryUrl', 'https://github.com:DataDog/dd-trace-js.git') }) it('still reads .git/ if one of the env vars is missing', () => { process.env.DD_GIT_FOLDER_PATH = DD_GIT_FOLDER_PATH process.env.DD_GIT_REPOSITORY_URL = 'git@github.com:DataDog/dummy-dd-trace-js.git' - const config = new Config({}) + const config = getConfig({}) expect(config).to.have.property('commitSHA', '964886d9ec0c9fc68778e4abb0aab4d9982ce2b5') expect(config).to.have.property('repositoryUrl', 'git@github.com:DataDog/dummy-dd-trace-js.git') }) @@ -2578,7 +2580,7 @@ describe('Config', () => { context('llmobs config', () => { it('should disable llmobs by default', () => { - const config = new Config() + const config = getConfig() expect(config.llmobs.enabled).to.be.false // check origin computation @@ -2589,7 +2591,7 @@ describe('Config', () => { it('should enable llmobs if DD_LLMOBS_ENABLED is set to true', () => { process.env.DD_LLMOBS_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config.llmobs.enabled).to.be.true // check origin computation @@ -2600,7 +2602,7 @@ describe('Config', () => { it('should disable llmobs if DD_LLMOBS_ENABLED is set to false', () => { process.env.DD_LLMOBS_ENABLED = 'false' - const config = new Config() + const config = getConfig() expect(config.llmobs.enabled).to.be.false // check origin computation @@ -2610,7 +2612,7 @@ describe('Config', () => { }) it('should enable llmobs with options and DD_LLMOBS_ENABLED is not set', () => { - const config = new Config({ llmobs: {} }) + const config = getConfig({ llmobs: {} }) expect(config.llmobs.enabled).to.be.true // check origin computation @@ -2621,7 +2623,7 @@ describe('Config', () => { it('should have DD_LLMOBS_ENABLED take priority over options', () => { process.env.DD_LLMOBS_ENABLED = 'false' - const config = new Config({ llmobs: {} }) + const config = getConfig({ llmobs: {} }) expect(config.llmobs.enabled).to.be.false // check origin computation @@ -2645,7 +2647,7 @@ describe('Config', () => { }) it('defaults', () => { - const taggingConfig = new Config().cloudPayloadTagging + const taggingConfig = getConfig().cloudPayloadTagging expect(taggingConfig).to.have.property('requestsEnabled', false) expect(taggingConfig).to.have.property('responsesEnabled', false) expect(taggingConfig).to.have.property('maxDepth', 10) @@ -2653,7 +2655,7 @@ describe('Config', () => { it('enabling requests with no additional filter', () => { process.env.DD_TRACE_CLOUD_REQUEST_PAYLOAD_TAGGING = 'all' - const taggingConfig = new Config().cloudPayloadTagging + const taggingConfig = getConfig().cloudPayloadTagging expect(taggingConfig).to.have.property('requestsEnabled', true) expect(taggingConfig).to.have.property('responsesEnabled', false) expect(taggingConfig).to.have.property('maxDepth', 10) @@ -2665,7 +2667,7 @@ describe('Config', () => { it('enabling requests with an additional filter', () => { process.env.DD_TRACE_CLOUD_REQUEST_PAYLOAD_TAGGING = '$.foo.bar' - const taggingConfig = new Config().cloudPayloadTagging + const taggingConfig = getConfig().cloudPayloadTagging expect(taggingConfig).to.have.property('requestsEnabled', true) expect(taggingConfig).to.have.property('responsesEnabled', false) expect(taggingConfig).to.have.property('maxDepth', 10) @@ -2677,7 +2679,7 @@ describe('Config', () => { it('enabling responses with no additional filter', () => { process.env.DD_TRACE_CLOUD_RESPONSE_PAYLOAD_TAGGING = 'all' - const taggingConfig = new Config().cloudPayloadTagging + const taggingConfig = getConfig().cloudPayloadTagging expect(taggingConfig).to.have.property('requestsEnabled', false) expect(taggingConfig).to.have.property('responsesEnabled', true) expect(taggingConfig).to.have.property('maxDepth', 10) @@ -2689,7 +2691,7 @@ describe('Config', () => { it('enabling responses with an additional filter', () => { process.env.DD_TRACE_CLOUD_RESPONSE_PAYLOAD_TAGGING = '$.foo.bar' - const taggingConfig = new Config().cloudPayloadTagging + const taggingConfig = getConfig().cloudPayloadTagging expect(taggingConfig).to.have.property('requestsEnabled', false) expect(taggingConfig).to.have.property('responsesEnabled', true) expect(taggingConfig).to.have.property('maxDepth', 10) @@ -2704,7 +2706,7 @@ describe('Config', () => { process.env.DD_TRACE_CLOUD_RESPONSE_PAYLOAD_TAGGING = 'all' process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH = '7' - let { cloudPayloadTagging } = new Config() + let { cloudPayloadTagging } = getConfig() assertObjectContains(cloudPayloadTagging, { maxDepth: 7, requestsEnabled: true, @@ -2713,7 +2715,7 @@ describe('Config', () => { delete process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH - ;({ cloudPayloadTagging } = new Config({ cloudPayloadTagging: { maxDepth: 7 } })) + ;({ cloudPayloadTagging } = getConfig({ cloudPayloadTagging: { maxDepth: 7 } })) assertObjectContains(cloudPayloadTagging, { maxDepth: 7, requestsEnabled: true, @@ -2724,14 +2726,14 @@ describe('Config', () => { it('use default max depth if max depth is not a number', () => { process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH = 'abc' - let { cloudPayloadTagging } = new Config() + let { cloudPayloadTagging } = getConfig() assertObjectContains(cloudPayloadTagging, { maxDepth: 10, }) delete process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH - ;({ cloudPayloadTagging } = new Config({ cloudPayloadTagging: { maxDepth: NaN } })) + ;({ cloudPayloadTagging } = getConfig({ cloudPayloadTagging: { maxDepth: NaN } })) assertObjectContains(cloudPayloadTagging, { maxDepth: 10, }) @@ -2742,7 +2744,7 @@ describe('Config', () => { it('should disable apm tracing with legacy DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED', () => { process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED = '1' - const config = new Config() + const config = getConfig() expect(config).to.have.property('apmTracingEnabled', false) }) @@ -2750,21 +2752,21 @@ describe('Config', () => { process.env.DD_APM_TRACING_ENABLED = '1' process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config).to.have.property('apmTracingEnabled', true) }) it('should disable apm tracing with legacy experimental.appsec.standalone.enabled option', () => { process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED = '0' - const config = new Config({ experimental: { appsec: { standalone: { enabled: true } } } }) + const config = getConfig({ experimental: { appsec: { standalone: { enabled: true } } } }) expect(config).to.have.property('apmTracingEnabled', false) }) it('should win apmTracingEnabled option', () => { process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED = 'true' - const config = new Config({ + const config = getConfig({ apmTracingEnabled: false, experimental: { appsec: { standalone: { enabled: true } } } }) @@ -2774,7 +2776,7 @@ describe('Config', () => { it('should not affect stats', () => { process.env.DD_TRACE_STATS_COMPUTATION_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config).to.have.property('apmTracingEnabled', true) expect(config).to.have.nested.property('stats.enabled', true) @@ -2787,7 +2789,7 @@ describe('Config', () => { process.env.DD_APM_TRACING_ENABLED = 'false' process.env.DD_TRACE_STATS_COMPUTATION_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config).to.have.property('apmTracingEnabled', false) expect(config).to.have.nested.property('stats.enabled', false) @@ -2797,7 +2799,7 @@ describe('Config', () => { }) it('should disable stats if config property is used', () => { - const config = new Config({ + const config = getConfig({ apmTracingEnabled: false }) expect(config).to.have.property('apmTracingEnabled', false) @@ -2831,7 +2833,7 @@ describe('Config', () => { apm_configuration_default: DD_RUNTIME_METRICS_ENABLED: true `) - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('runtimeMetrics.enabled', true) }) @@ -2848,13 +2850,13 @@ rules: configuration: DD_SERVICE: my-service `) - const config = new Config() + const config = getConfig() expect(config).to.have.property('service', 'my-service') }) it('should respect the priority sources', () => { // 1. Default - const config1 = new Config() + const config1 = getConfig() expect(config1).to.have.property('service', 'node') // 2. Local stable > Default @@ -2870,7 +2872,7 @@ rules: configuration: DD_SERVICE: service_local_stable `) - const config2 = new Config() + const config2 = getConfig() expect(config2).to.have.property( 'service', 'service_local_stable', @@ -2879,7 +2881,7 @@ rules: // 3. Env > Local stable > Default process.env.DD_SERVICE = 'service_env' - const config3 = new Config() + const config3 = getConfig() expect(config3).to.have.property( 'service', 'service_env', @@ -2899,7 +2901,7 @@ rules: configuration: DD_SERVICE: service_fleet_stable `) - const config4 = new Config() + const config4 = getConfig() expect(config4).to.have.property( 'service', 'service_fleet_stable', @@ -2907,7 +2909,7 @@ rules: ) // 5. Code > Fleet Stable > Env > Local stable > Default - const config5 = new Config({ service: 'service_code' }) + const config5 = getConfig({ service: 'service_code' }) expect(config5).to.have.property( 'service', 'service_code', @@ -2926,7 +2928,7 @@ apm_configuration_default: const stableConfig = new StableConfig() expect(stableConfig.warnings).to.have.lengthOf(0) - const config = new Config() + const config = getConfig() expect(config).to.have.nested.property('runtimeMetrics.enabled', true) }) @@ -2964,7 +2966,7 @@ apm_configuration_default: `) process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-great-lambda-function' - const stableConfig = new Config() + const stableConfig = getConfig() expect(stableConfig).to.not.have.property('stableConfig') }) @@ -2992,7 +2994,7 @@ apm_configuration_default: DD_DYNAMIC_INSTRUMENTATION_PROBE_FILE: "/tmp/probes" `) - const config = new Config() + const config = getConfig() // Tracing expect(config).to.have.nested.property('traceId128BitGenerationEnabled', true) @@ -3038,7 +3040,7 @@ apm_configuration_default: DD_TRACE_CLOUD_REQUEST_PAYLOAD_TAGGING: "all" DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH: 5 `) - let config = new Config() + let config = getConfig() expect(config).to.have.property('apiKey', 'local-api-key') expect(config).to.have.property('appKey', 'local-app-key') expect(config).to.have.nested.property('installSignature.id', 'local-install-id') @@ -3052,7 +3054,7 @@ apm_configuration_default: process.env.DD_APP_KEY = 'env-app-key' process.env.DD_INSTRUMENTATION_INSTALL_ID = 'env-install-id' process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH = '7' - config = new Config() + config = getConfig() expect(config).to.have.property('apiKey', 'env-api-key') expect(config).to.have.property('appKey', 'env-app-key') expect(config).to.have.nested.property('installSignature.id', 'env-install-id') @@ -3078,7 +3080,7 @@ rules: DD_TRACE_CLOUD_RESPONSE_PAYLOAD_TAGGING: "all" DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH: 15 `) - config = new Config() + config = getConfig() expect(config).to.have.property('apiKey', 'fleet-api-key') expect(config).to.have.property('appKey', 'fleet-app-key') expect(config).to.have.nested.property('installSignature.id', 'fleet-install-id') @@ -3102,7 +3104,7 @@ rules: }) it('should return default value', () => { - const config = new Config() + const config = getConfig() expect(config.getOrigin('appsec.enabled')).to.be.equal('default') }) @@ -3110,13 +3112,13 @@ rules: it('should return env_var', () => { process.env.DD_APPSEC_ENABLED = 'true' - const config = new Config() + const config = getConfig() expect(config.getOrigin('appsec.enabled')).to.be.equal('env_var') }) it('should return code', () => { - const config = new Config({ + const config = getConfig({ appsec: true }) diff --git a/packages/dd-trace/test/debugger/config.spec.js b/packages/dd-trace/test/debugger/config.spec.js index a23b2bf2975..33a7d0c39cb 100644 --- a/packages/dd-trace/test/debugger/config.spec.js +++ b/packages/dd-trace/test/debugger/config.spec.js @@ -4,11 +4,11 @@ require('../setup/mocha') const assert = require('node:assert') const getDebuggerConfig = require('../../src/debugger/config') -const Config = require('../../src/config') +const getConfig = require('../../src/config') describe('getDebuggerConfig', function () { it('should only contain the allowed properties', function () { - const tracerConfig = new Config({ + const tracerConfig = getConfig({ url: new URL('http://example.com:1234') }) const config = getDebuggerConfig(tracerConfig) @@ -37,7 +37,7 @@ describe('getDebuggerConfig', function () { }) it('should be able to send the config over a MessageChannel', function () { - const config = getDebuggerConfig(new Config()) + const config = getDebuggerConfig(getConfig()) const channel = new MessageChannel() channel.port1.on('message', (message) => { assert.deepStrictEqual(message, config) diff --git a/packages/dd-trace/test/flare.spec.js b/packages/dd-trace/test/flare.spec.js index c04ef2d97d0..b31343c94d2 100644 --- a/packages/dd-trace/test/flare.spec.js +++ b/packages/dd-trace/test/flare.spec.js @@ -1,17 +1,17 @@ 'use strict' +const http = require('node:http') + const { expect } = require('chai') const { channel } = require('dc-polyfill') const express = require('express') const upload = require('multer')() const proxyquire = require('proxyquire').noCallThru() - -const http = require('node:http') +const { describe, it, beforeEach, afterEach } = require('tap').mocha require('./setup/core') -const { describe, it, beforeEach, afterEach } = require('tap').mocha -const Config = require('../src/config') +const { getConfigFresh } = require('./helpers/config') const debugChannel = channel('datadog:log:debug') @@ -60,7 +60,7 @@ describe('Flare', () => { beforeEach(createServer) beforeEach(() => { - tracerConfig = new Config({ + tracerConfig = getConfigFresh({ url: `http://127.0.0.1:${port}` }) diff --git a/packages/dd-trace/test/helpers/config.js b/packages/dd-trace/test/helpers/config.js new file mode 100644 index 00000000000..0e83e73f4be --- /dev/null +++ b/packages/dd-trace/test/helpers/config.js @@ -0,0 +1,14 @@ +'use strict' + +const proxyquire = require('proxyquire') + +// Resolve the config module from within the test package +const CONFIG_PATH = require.resolve('../../src/config') + +function getConfigFresh (options) { + return proxyquire.noPreserveCache()(CONFIG_PATH, {})(options) +} + +module.exports = { + getConfigFresh, +} diff --git a/packages/dd-trace/test/llmobs/sdk/index.spec.js b/packages/dd-trace/test/llmobs/sdk/index.spec.js index 8f468830b41..77996165cd6 100644 --- a/packages/dd-trace/test/llmobs/sdk/index.spec.js +++ b/packages/dd-trace/test/llmobs/sdk/index.spec.js @@ -5,7 +5,7 @@ const { channel } = require('dc-polyfill') const { describe, it, beforeEach, afterEach, before, after } = require('mocha') const sinon = require('sinon') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const LLMObsTagger = require('../../../src/llmobs/tagger') const LLMObsEvalMetricsWriter = require('../../../src/llmobs/writers/evaluations') @@ -94,7 +94,7 @@ describe('sdk', () => { describe('enable', () => { it('enables llmobs if it is disabled', () => { - const config = new Config({}) + const config = getConfigFresh({}) const llmobsModule = { enable: sinon.stub(), disable () {} @@ -126,7 +126,7 @@ describe('sdk', () => { }) it('does not enable llmobs if env var conflicts', () => { - const config = new Config({}) + const config = getConfigFresh({}) const llmobsModule = { enable: sinon.stub() } @@ -149,7 +149,7 @@ describe('sdk', () => { disable: sinon.stub() } - const config = new Config({ + const config = getConfigFresh({ llmobs: {} }) diff --git a/packages/dd-trace/test/opentelemetry/logs.spec.js b/packages/dd-trace/test/opentelemetry/logs.spec.js index 84fb6c8d2d2..37ad96d83d1 100644 --- a/packages/dd-trace/test/opentelemetry/logs.spec.js +++ b/packages/dd-trace/test/opentelemetry/logs.spec.js @@ -13,6 +13,7 @@ const proxyquire = require('proxyquire') const { logs } = require('@opentelemetry/api-logs') const { trace, context } = require('@opentelemetry/api') const { protoLogsService } = require('../../src/opentelemetry/otlp/protobuf_loader').getProtobufTypes() +const { getConfigFresh } = require('../helpers/config') describe('OpenTelemetry Logs', () => { let originalEnv @@ -20,7 +21,16 @@ describe('OpenTelemetry Logs', () => { function setupTracer (enabled = true, maxExportBatchSize = '1') { process.env.DD_LOGS_OTEL_ENABLED = enabled ? 'true' : 'false' process.env.OTEL_BSP_MAX_EXPORT_BATCH_SIZE = maxExportBatchSize // Force immediate export - const tracer = require('../../') + + const proxy = proxyquire.noPreserveCache()('../../src/proxy', { + './config': getConfigFresh, + }) + const TracerProxy = proxyquire.noPreserveCache()('../../src', { + './proxy': proxy + }) + const tracer = proxyquire.noPreserveCache()('../../', { + './src': TracerProxy + }) tracer._initialized = false tracer.init() return { tracer, logs, loggerProvider: logs.getLoggerProvider() } @@ -353,6 +363,7 @@ describe('OpenTelemetry Logs', () => { // Emit with an invalid severity number (999) logger.emit({ + // @ts-expect-error - check invalid severity number severityNumber: 999, body: 'Test message with invalid severity' }) diff --git a/packages/dd-trace/test/opentracing/propagation/text_map.spec.js b/packages/dd-trace/test/opentracing/propagation/text_map.spec.js index 077788d3a27..77063b99c94 100644 --- a/packages/dd-trace/test/opentracing/propagation/text_map.spec.js +++ b/packages/dd-trace/test/opentracing/propagation/text_map.spec.js @@ -7,7 +7,7 @@ const proxyquire = require('proxyquire') require('../../setup/core') -const Config = require('../../../src/config') +const { getConfigFresh } = require('../../helpers/config') const id = require('../../../src/id') const SpanContext = require('../../../src/opentracing/span_context') const TraceState = require('../../../src/opentracing/propagation/tracestate') @@ -63,7 +63,7 @@ describe('TextMapPropagator', () => { '../../log': log, '../../telemetry/metrics': telemetryMetrics }) - config = new Config({ tagsHeaderMaxLength: 512 }) + config = getConfigFresh({ tagsHeaderMaxLength: 512 }) propagator = new TextMapPropagator(config) textMap = { 'x-datadog-trace-id': '123', @@ -481,7 +481,7 @@ describe('TextMapPropagator', () => { }) it('should extract opentracing baggage when baggage is not a propagation style ', () => { - config = new Config({ + config = getConfigFresh({ tracePropagationStyle: { extract: ['datadog', 'tracecontext'], inject: ['datadog', 'tracecontext'] @@ -500,7 +500,7 @@ describe('TextMapPropagator', () => { }) it('should extract otel baggage items with special characters', () => { - config = new Config() + config = getConfigFresh() propagator = new TextMapPropagator(config) const carrier = { 'x-datadog-trace-id': '123', @@ -577,7 +577,7 @@ describe('TextMapPropagator', () => { }) // should not add baggage when key list is empty - config = new Config({ + config = getConfigFresh({ baggageTagKeys: '' }) propagator = new TextMapPropagator(config) @@ -585,7 +585,7 @@ describe('TextMapPropagator', () => { expect(spanContextC._trace.tags).to.deep.equal({}) // should not add baggage when key list is empty - config = new Config({ + config = getConfigFresh({ baggageTagKeys: 'customKey' }) propagator = new TextMapPropagator(config) @@ -600,7 +600,7 @@ describe('TextMapPropagator', () => { }) // should add all baggage to span tags - config = new Config({ + config = getConfigFresh({ baggageTagKeys: '*' }) propagator = new TextMapPropagator(config) @@ -653,7 +653,7 @@ describe('TextMapPropagator', () => { it('should extract baggage when it is the only propagation style', () => { removeAllBaggageItems() - config = new Config({ + config = getConfigFresh({ tracePropagationStyle: { extract: ['baggage'] } @@ -1396,7 +1396,7 @@ describe('TextMapPropagator', () => { it('should reset span links when Trace_Propagation_Behavior_Extract is set to ignore', () => { process.env.DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT = 'ignore' - config = new Config({ + config = getConfigFresh({ tracePropagationStyle: { extract: ['tracecontext', 'datadog'] } @@ -1410,7 +1410,7 @@ describe('TextMapPropagator', () => { it('should set span link to extracted trace when Trace_Propagation_Behavior_Extract is set to restart', () => { process.env.DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT = 'restart' - config = new Config({ + config = getConfigFresh({ tracePropagationStyle: { extract: ['tracecontext', 'datadog'] } @@ -1428,7 +1428,7 @@ describe('TextMapPropagator', () => { it('should not extract baggage when Trace_Propagation_Behavior_Extract is set to ignore', () => { process.env.DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT = 'ignore' - config = new Config({ + config = getConfigFresh({ tracePropagationStyle: { extract: ['tracecontext', 'datadog', 'baggage'] } diff --git a/packages/dd-trace/test/opentracing/span.spec.js b/packages/dd-trace/test/opentracing/span.spec.js index a5b3d807e29..acd74b223bd 100644 --- a/packages/dd-trace/test/opentracing/span.spec.js +++ b/packages/dd-trace/test/opentracing/span.spec.js @@ -8,7 +8,7 @@ const proxyquire = require('proxyquire') require('../setup/core') -const Config = require('../../src/config') +const getConfig = require('../../src/config') const TextMapPropagator = require('../../src/opentracing/propagation/text_map') const startCh = channel('dd-trace:span:start') @@ -110,7 +110,7 @@ describe('Span', () => { }) it('should generate new timing when the parent was extracted', () => { - const propagator = new TextMapPropagator(new Config()) + const propagator = new TextMapPropagator(getConfig()) const parent = propagator.extract({ 'x-datadog-trace-id': '1234', 'x-datadog-parent-id': '5678' diff --git a/packages/dd-trace/test/plugins/agent.js b/packages/dd-trace/test/plugins/agent.js index d63816c7c1e..c6d4f0a2e98 100644 --- a/packages/dd-trace/test/plugins/agent.js +++ b/packages/dd-trace/test/plugins/agent.js @@ -11,6 +11,7 @@ const ritm = require('../../src/ritm') const { storage } = require('../../../datadog-core') const { assertObjectContains } = require('../../../../integration-tests/helpers') const { expect } = require('chai') +const proxyquire = require('proxyquire') const traceHandlers = new Set() const statsHandlers = new Set() @@ -370,6 +371,7 @@ module.exports = { /** * Load the plugin on the tracer with an optional config and start a mock agent. * + * @overload * @param {String|String[]} pluginNames - Name or list of names of plugins to load * @param {Record} [config] * @param {Record} [tracerConfig={}] @@ -395,7 +397,17 @@ module.exports = { currentIntegrationName = getCurrentIntegrationName() - tracer = require('../..') + const getConfigFresh = (options) => proxyquire.noPreserveCache()('../../src/config', {})(options) + const proxy = proxyquire('../../src/proxy', { + './config': getConfigFresh + }) + const TracerProxy = proxyquire('../../src', { + './proxy': proxy + }) + tracer = proxyquire('../../', { + './src': TracerProxy + }) + agent = express() agent.use(bodyParser.raw({ limit: Infinity, type: 'application/msgpack' })) agent.use(bodyParser.text({ limit: Infinity, type: 'application/json' })) @@ -514,15 +526,19 @@ module.exports = { * @param {Function} handler */ subscribe (handler) { - traceHandlers.add({ handler }) // TODO: SHOULD BE .add(handler) SO WE CAN DELETE + traceHandlers.add({ handler }) }, /** - * Remove a handler (TODO: THIS DOES NOTHING) + * Remove a handler * @param {Function} handler */ unsubscribe (handler) { - traceHandlers.delete(handler) + for (const traceHandler of traceHandlers) { + if (traceHandler.handler === handler) { + traceHandlers.delete(traceHandler) + } + } }, /** diff --git a/packages/dd-trace/test/plugins/log_plugin.spec.js b/packages/dd-trace/test/plugins/log_plugin.spec.js index 094d64606a1..e9ee3dea026 100644 --- a/packages/dd-trace/test/plugins/log_plugin.spec.js +++ b/packages/dd-trace/test/plugins/log_plugin.spec.js @@ -8,7 +8,7 @@ require('../setup/core') const LogPlugin = require('../../src/plugins/log_plugin') const Tracer = require('../../src/tracer') -const Config = require('../../src/config') +const getConfig = require('../../src/config') const testLogChannel = channel('apm:test:log') @@ -22,7 +22,7 @@ const config = { version: '1.2.3' } -const tracer = new Tracer(new Config({ +const tracer = new Tracer(getConfig({ logInjection: true, enabled: true, ...config diff --git a/packages/dd-trace/test/startup-log.spec.js b/packages/dd-trace/test/startup-log.spec.js index 6a41c409855..14afa5c953c 100644 --- a/packages/dd-trace/test/startup-log.spec.js +++ b/packages/dd-trace/test/startup-log.spec.js @@ -8,7 +8,7 @@ const os = require('node:os') require('./setup/core') -const Config = require('../src/config') +const { getConfigFresh } = require('./helpers/config') const SamplingRule = require('../src/sampling_rule') const tracerVersion = require('../../../package.json').version @@ -125,7 +125,7 @@ describe('profiling_enabled', () => { } = require('../src/startup-log') process.env.DD_PROFILING_ENABLED = envVar process.env.DD_TRACE_STARTUP_LOGS = 'true' - setStartupLogConfig(new Config()) + setStartupLogConfig(getConfigFresh()) setStartupLogPluginManager({ _pluginsByName: {} }) startupLog() /* eslint-disable-next-line no-console */ diff --git a/packages/dd-trace/test/tracer.spec.js b/packages/dd-trace/test/tracer.spec.js index dcea7aac0de..e3751568bb1 100644 --- a/packages/dd-trace/test/tracer.spec.js +++ b/packages/dd-trace/test/tracer.spec.js @@ -8,7 +8,7 @@ require('./setup/core') const Tracer = require('../src/tracer') const Span = require('../src/opentracing/span') -const Config = require('../src/config') +const getConfig = require('../src/config') const tags = require('../../../ext/tags') const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants') @@ -23,7 +23,7 @@ describe('Tracer', () => { let config beforeEach(() => { - config = new Config({ service: 'service' }) + config = getConfig({ service: 'service' }) tracer = new Tracer(config) tracer._exporter.setUrl = sinon.stub()