Skip to content

Commit 8c00ddf

Browse files
committed
have Config re-use the stable config entries from ConfigEnvSources
1 parent a9faac2 commit 8c00ddf

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

packages/dd-trace/src/config-env-sources.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ class ConfigEnvSources {
99

1010
let localStableConfig = {}
1111
let fleetStableConfig = {}
12+
let stableConfigWarnings = []
1213

1314
if (!isServerless) {
1415
const result = this.#loadStableConfig()
1516
if (result) {
1617
localStableConfig = result.localEntries
1718
fleetStableConfig = result.fleetEntries
19+
stableConfigWarnings = result.warnings
1820
}
1921
}
2022

2123
// Expose raw stable config on the instance
2224
this.localStableConfig = localStableConfig
2325
this.fleetStableConfig = fleetStableConfig
26+
this.stableConfigWarnings = stableConfigWarnings
2427
}
2528

2629
#loadStableConfig () {
2730
try {
2831
const StableConfig = require('./config_stable')
2932
const instance = new StableConfig()
3033
return {
31-
instance,
3234
localEntries: instance.localEntries ?? {},
3335
fleetEntries: instance.fleetEntries ?? {},
3436
warnings: instance.warnings ?? []

packages/dd-trace/src/config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { isInServerlessEnvironment, getIsGCPFunction, getIsAzureFunction } = requ
1818
const { ORIGIN_KEY } = require('./constants')
1919
const { appendRules } = require('./payload-tagging/config')
2020
const { getEnvironmentVariable: getEnv, getEnvironmentVariables } = require('./config-helper')
21+
const { getConfigEnvSources } = require('./config-env-sources')
2122
const defaults = require('./config_defaults')
2223
const path = require('path')
2324
const { DD_MAJOR } = require('../../../version')
@@ -272,8 +273,12 @@ class Config {
272273
constructor (options = {}) {
273274
if (!isInServerlessEnvironment()) {
274275
// Bail out early if we're in a serverless environment, stable config isn't supported
275-
const StableConfig = require('./config_stable')
276-
this.stableConfig = new StableConfig()
276+
const configEnvSources = getConfigEnvSources()
277+
this.stableConfig = {
278+
fleetEntries: configEnvSources.fleetStableConfig,
279+
localEntries: configEnvSources.localStableConfig,
280+
warnings: configEnvSources.stableConfigWarnings
281+
}
277282
}
278283

279284
const envs = getEnvironmentVariables()

packages/dd-trace/src/config_stable.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const fs = require('fs')
55
const { getEnvironmentVariable } = require('../../dd-trace/src/config-helper')
66

77
class StableConfig {
8-
// Cache file contents by path so that StableConfig uses a stable view of the
9-
// configuration files across multiple instantiations in the same process.
10-
static #fileContentsCache = new Map()
118
constructor () {
129
this.warnings = [] // Logger hasn't been initialized yet, so we can't use log.warn
1310
this.localEntries = {}
@@ -20,7 +17,6 @@ class StableConfig {
2017
return
2118
}
2219

23-
// cache the file reads
2420
const localConfig = this._readConfigFromPath(localConfigPath)
2521
const fleetConfig = this._readConfigFromPath(fleetConfigPath)
2622
if (!localConfig && !fleetConfig) {
@@ -64,23 +60,13 @@ class StableConfig {
6460
}
6561

6662
_readConfigFromPath (path) {
67-
const cache = StableConfig.#fileContentsCache
68-
69-
if (cache.has(path)) {
70-
return cache.get(path)
71-
}
72-
7363
try {
74-
const contents = fs.readFileSync(path, 'utf8')
75-
cache.set(path, contents)
76-
return contents
64+
return fs.readFileSync(path, 'utf8')
7765
} catch (err) {
7866
if (err.code !== 'ENOENT') {
7967
this.warnings.push(`Error reading config file at ${path}. ${err.code}: ${err.message}`)
8068
}
81-
const contents = '' // Always return a string to avoid undefined.toString() errors
82-
cache.set(path, contents)
83-
return contents
69+
return '' // Always return a string to avoid undefined.toString() errors
8470
}
8571
}
8672

packages/dd-trace/test/config.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require('./setup/core')
1414

1515
const { GRPC_CLIENT_ERROR_STATUSES, GRPC_SERVER_ERROR_STATUSES } = require('../src/constants')
1616
const { getEnvironmentVariable, getEnvironmentVariables } = require('../src/config-helper')
17+
const { resetConfigEnvSources } = require('../src/config-env-sources')
1718
const { assertObjectContains } = require('../../../integration-tests/helpers')
1819
const { DD_MAJOR } = require('../../../version')
1920

@@ -2819,6 +2820,7 @@ describe('Config', () => {
28192820
tempDir = fs.mkdtempSync(path.join(baseTempDir, 'config-test-'))
28202821
process.env.DD_TEST_LOCAL_CONFIG_PATH = path.join(tempDir, 'local.yaml')
28212822
process.env.DD_TEST_FLEET_CONFIG_PATH = path.join(tempDir, 'fleet.yaml')
2823+
resetConfigEnvSources()
28222824
})
28232825

28242826
afterEach(() => {
@@ -2872,6 +2874,8 @@ rules:
28722874
configuration:
28732875
DD_SERVICE: service_local_stable
28742876
`)
2877+
resetConfigEnvSources()
2878+
28752879
const config2 = getConfig()
28762880
expect(config2).to.have.property(
28772881
'service',
@@ -2881,6 +2885,8 @@ rules:
28812885

28822886
// 3. Env > Local stable > Default
28832887
process.env.DD_SERVICE = 'service_env'
2888+
resetConfigEnvSources()
2889+
28842890
const config3 = getConfig()
28852891
expect(config3).to.have.property(
28862892
'service',
@@ -2901,6 +2907,7 @@ rules:
29012907
configuration:
29022908
DD_SERVICE: service_fleet_stable
29032909
`)
2910+
resetConfigEnvSources()
29042911
const config4 = getConfig()
29052912
expect(config4).to.have.property(
29062913
'service',
@@ -3040,6 +3047,7 @@ apm_configuration_default:
30403047
DD_TRACE_CLOUD_REQUEST_PAYLOAD_TAGGING: "all"
30413048
DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH: 5
30423049
`)
3050+
resetConfigEnvSources()
30433051
let config = getConfig()
30443052
expect(config).to.have.property('apiKey', 'local-api-key')
30453053
expect(config).to.have.property('appKey', 'local-app-key')
@@ -3054,6 +3062,7 @@ apm_configuration_default:
30543062
process.env.DD_APP_KEY = 'env-app-key'
30553063
process.env.DD_INSTRUMENTATION_INSTALL_ID = 'env-install-id'
30563064
process.env.DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH = '7'
3065+
resetConfigEnvSources()
30573066
config = getConfig()
30583067
expect(config).to.have.property('apiKey', 'env-api-key')
30593068
expect(config).to.have.property('appKey', 'env-app-key')
@@ -3080,6 +3089,7 @@ rules:
30803089
DD_TRACE_CLOUD_RESPONSE_PAYLOAD_TAGGING: "all"
30813090
DD_TRACE_CLOUD_PAYLOAD_TAGGING_MAX_DEPTH: 15
30823091
`)
3092+
resetConfigEnvSources()
30833093
config = getConfig()
30843094
expect(config).to.have.property('apiKey', 'fleet-api-key')
30853095
expect(config).to.have.property('appKey', 'fleet-app-key')

0 commit comments

Comments
 (0)