Skip to content

Commit 6c8d203

Browse files
authored
chore: refactor config class to a singleton (#6803)
This allows to pick up runtime configuration changes in the future as well as allowing to simplify the code in future PRs. One possible change is to remove `configure` and to move the remote configuration to just update the singleton instead. We can add events to activate and deactivate features as soon as updates are incoming that are not handled by a simple property access. That way most configurations will be changeable at runtime as soon as the remote configuration handles all entries. Drive by fix: * test: fix test agent unsubscribe method It is now going to remove the proper handler when being called. Before, it could never match the handler due to not iterating through the existing ones. It just tried to match for the method which would always fail.
1 parent 8c5ec43 commit 6c8d203

File tree

64 files changed

+528
-431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+528
-431
lines changed

benchmark/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const benchmark = require('./benchmark')
44
const proxyquire = require('proxyquire')
55

6-
const Config = require('../packages/dd-trace/src/config')
6+
const getConfig = require('../packages/dd-trace/src/config')
77
const DatadogTracer = require('../packages/dd-trace/src/tracer')
88
const DatadogSpanContext = require('../packages/dd-trace/src/opentracing/span_context')
99
const TextMapPropagator = require('../packages/dd-trace/src/opentracing/propagation/text_map')
@@ -22,7 +22,7 @@ const Sampler = require('../packages/dd-trace/src/sampler')
2222
const format = require('../packages/dd-trace/src/format')
2323
const { AgentEncoder: Agent04Encoder } = require('../packages/dd-trace/src/encode/0.4')
2424
const { AgentEncoder: Agent05Encoder } = require('../packages/dd-trace/src/encode/0.5')
25-
const config = new Config({ service: 'benchmark' })
25+
const config = getConfig({ service: 'benchmark' })
2626
const id = require('../packages/dd-trace/src/id')
2727
const Histogram = require('../packages/dd-trace/src/histogram')
2828
const histogram = new Histogram()

benchmark/openfeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const benchmark = require('./benchmark')
44
const proxyquire = require('proxyquire')
55
const { createSingleExposureEvent, createExposureEventArray } = require('./stubs/exposure-events')
66

7-
const Config = require('../packages/dd-trace/src/config')
7+
const getConfig = require('../packages/dd-trace/src/config')
88
const ExposuresWriter = proxyquire('../packages/dd-trace/src/openfeature/writers/exposures', {
99
'../../exporters/common/request': () => {}
1010
})
1111

12-
const config = new Config({ service: 'benchmark', version: '1.0.0', env: 'test' })
12+
const config = getConfig({ service: 'benchmark', version: '1.0.0', env: 'test' })
1313
const suite = benchmark('openfeature')
1414

1515
let writer

benchmark/sirun/debugger/start-devtools-client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

3-
const Config = require('../../../packages/dd-trace/src/config')
3+
const getConfig = require('../../../packages/dd-trace/src/config')
44
const { start } = require('../../../packages/dd-trace/src/debugger')
55
const { generateProbeConfig } = require('../../../packages/dd-trace/test/debugger/devtools_client/utils')
66

77
const breakpoint = {
88
file: process.env.BREAKPOINT_FILE,
99
line: process.env.BREAKPOINT_LINE
1010
}
11-
const config = new Config()
11+
const config = getConfig()
1212
const rc = {
1313
setProductHandler (product, cb) {
1414
const action = 'apply'

packages/datadog-plugin-http/test/server.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('Plugin', () => {
1717
let tracer
1818
let port
1919
let app
20+
let timeout
2021

2122
['http', 'node:http'].forEach(pluginToBeLoaded => {
2223
describe(`${pluginToBeLoaded}/server`, () => {
@@ -32,13 +33,15 @@ describe('Plugin', () => {
3233
afterEach(() => {
3334
appListener && appListener.close()
3435
app = null
36+
clearTimeout(timeout)
37+
timeout = null
3538
return agent.close({ ritmReset: false })
3639
})
3740

3841
describe('canceled request', () => {
3942
beforeEach(() => {
4043
listener = (req, res) => {
41-
setTimeout(() => {
44+
timeout = setTimeout(() => {
4245
app && app(req, res)
4346
res.writeHead(200)
4447
res.end()

packages/datadog-plugin-openai/test/services.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const services = require('../src/services')
4+
const { getConfigFresh } = require('../../dd-trace/test/helpers/config')
45

56
describe('Plugin', () => {
67
describe('openai services', () => {
@@ -10,23 +11,23 @@ describe('Plugin', () => {
1011
})
1112

1213
it('dogstatsd does not throw when missing .dogstatsd', () => {
13-
const service = services.init({
14+
const service = services.init(getConfigFresh({
1415
hostname: 'foo',
1516
service: 'bar',
1617
apiKey: 'my api key',
1718
interval: 1000
18-
})
19+
}))
1920

2021
service.metrics.increment('mykey')
2122
service.logger.log('hello')
2223
})
2324

2425
it('logger does not throw', () => {
25-
const service = services.init({
26+
const service = services.init(getConfigFresh({
2627
hostname: 'foo',
2728
service: 'bar',
2829
interval: 1000
29-
})
30+
}))
3031

3132
service.logger.log('hello')
3233
})

packages/dd-trace/src/config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs')
44
const os = require('os')
55
const uuid = require('crypto-randomuuid') // we need to keep the old uuid dep because of cypress
66
const { URL } = require('url')
7+
78
const log = require('./log')
89
const tagger = require('./tagger')
910
const set = require('../../datadog-core/src/utils/src/set')
@@ -1507,4 +1508,12 @@ function getAgentUrl (url, options) {
15071508
}
15081509
}
15091510

1510-
module.exports = Config
1511+
let configInstance = null
1512+
function getConfig (options) {
1513+
if (!configInstance) {
1514+
configInstance = new Config(options)
1515+
}
1516+
return configInstance
1517+
}
1518+
1519+
module.exports = getConfig

packages/dd-trace/src/proxy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
const NoopProxy = require('./noop/proxy')
33
const DatadogTracer = require('./tracer')
4-
const Config = require('./config')
4+
const getConfig = require('./config')
55
const runtimeMetrics = require('./runtime_metrics')
66
const log = require('./log')
77
const { setStartupLogPluginManager } = require('./startup-log')
@@ -98,7 +98,7 @@ class Tracer extends NoopProxy {
9898
this._initialized = true
9999

100100
try {
101-
const config = new Config(options) // TODO: support dynamic code config
101+
const config = getConfig(options) // TODO: support dynamic code config
102102

103103
if (config.crashtracking.enabled) {
104104
require('./crashtracking').start(config)

packages/dd-trace/test/appsec/attacker-fingerprinting.express.plugin.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3+
const path = require('node:path')
4+
35
const axios = require('axios')
46
const { assert } = require('chai')
5-
const path = require('path')
67

78
const agent = require('../plugins/agent')
89
const appsec = require('../../src/appsec')
9-
const Config = require('../../src/config')
10+
const { getConfigFresh } = require('../helpers/config')
1011
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
1112

1213
withVersions('express', 'express', expressVersion => {
@@ -40,7 +41,7 @@ withVersions('express', 'express', expressVersion => {
4041
})
4142

4243
beforeEach(() => {
43-
appsec.enable(new Config(
44+
appsec.enable(getConfigFresh(
4445
{
4546
appsec: {
4647
enabled: true,

packages/dd-trace/test/appsec/attacker-fingerprinting.fastify.plugin.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3+
const path = require('node:path')
4+
35
const Axios = require('axios')
46
const { assert } = require('chai')
5-
const path = require('path')
67

78
const agent = require('../plugins/agent')
89
const appsec = require('../../src/appsec')
9-
const Config = require('../../src/config')
10+
const { getConfigFresh } = require('../helpers/config')
1011
const { withVersions } = require('../setup/mocha')
1112

1213
withVersions('fastify', 'fastify', fastifyVersion => {
@@ -40,7 +41,7 @@ withVersions('fastify', 'fastify', fastifyVersion => {
4041
})
4142

4243
beforeEach(() => {
43-
appsec.enable(new Config(
44+
appsec.enable(getConfigFresh(
4445
{
4546
appsec: {
4647
enabled: true,

packages/dd-trace/test/appsec/attacker-fingerprinting.passport-http.plugin.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { assert } = require('chai')
55

66
const agent = require('../plugins/agent')
77
const appsec = require('../../src/appsec')
8-
const Config = require('../../src/config')
8+
const { getConfigFresh } = require('../helpers/config')
99
const { withVersions } = require('../setup/mocha')
1010

1111
function assertFingerprintInTraces (traces) {
@@ -27,7 +27,7 @@ withVersions('passport-http', 'passport-http', version => {
2727
})
2828

2929
before(() => {
30-
appsec.enable(new Config({
30+
appsec.enable(getConfigFresh({
3131
appsec: true
3232
}))
3333
})

0 commit comments

Comments
 (0)