Skip to content

Commit 0a3fc0d

Browse files
committed
[#404] Configuration Reconstructing
1 parent 67336d2 commit 0a3fc0d

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

lib/config-builder.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const fs = require('fs')
1010
const path = require('path')
11+
const { randomBytes } = require('node:crypto')
1112

1213
class Config {
1314
constructor(config) {
@@ -19,6 +20,12 @@ class Config {
1920
class ConfigBuilder {
2021
constructor(agentStartupUserDefinedJson = {}) {
2122
this.agentStartupUserDefinedJson = agentStartupUserDefinedJson
23+
this.handlers = [
24+
new AgentIdGenerator(),
25+
new ApplicationNameValidator()
26+
]
27+
this.errorMessages = []
28+
this.warnMessages = []
2229
}
2330

2431
setDefaultJson(json) {
@@ -31,6 +38,11 @@ class ConfigBuilder {
3138
return this
3239
}
3340

41+
addHandler(handler) {
42+
this.handlers.push(handler)
43+
return this
44+
}
45+
3446
build() {
3547
if (!this.defaultJson) {
3648
this.defaultJson = require('./pinpoint-config-default')
@@ -46,6 +58,11 @@ class ConfigBuilder {
4658
makeEnvironmentConfig(),
4759
this.agentStartupUserDefinedJson
4860
)
61+
62+
for (const handler of this.handlers) {
63+
const changes = handler.handle(config)
64+
applyConfigChanges(config, changes)
65+
}
4966
return new Config(config)
5067
}
5168

@@ -64,7 +81,7 @@ class ConfigBuilder {
6481
const fileContent = fs.readFileSync(configFilePath, 'utf8')
6582
return JSON.parse(fileContent)
6683
} catch (e) {
67-
console.error('Failed to read or parse pinpoint-config.json:', e)
84+
this.warnMessages.push(`Failed to read or parse pinpoint-config.json at ${configFilePath}: ${e.message}`)
6885
return {}
6986
}
7087
}
@@ -81,6 +98,47 @@ class ConfigBuilder {
8198
}
8299
}
83100

101+
function applyConfigChanges(target, changes) {
102+
if (!changes || typeof changes !== 'object') {
103+
return
104+
}
105+
106+
for (const [key, value] of Object.entries(changes)) {
107+
if (value === undefined) {
108+
continue
109+
}
110+
111+
if (isPlainObject(value) && isPlainObject(target[key])) {
112+
applyConfigChanges(target[key], value)
113+
} else {
114+
target[key] = value
115+
}
116+
}
117+
}
118+
119+
function isPlainObject(value) {
120+
return value !== null && typeof value === 'object' && !Array.isArray(value)
121+
}
122+
123+
class AgentIdGenerator {
124+
handle(config) {
125+
if (config.agentId?.length > 0) {
126+
return {}
127+
}
128+
129+
return { agentId: randomBytes(8).toString('hex') }
130+
}
131+
}
132+
133+
class ApplicationNameValidator {
134+
handle(config) {
135+
if (config.applicationName?.length > 1) {
136+
return {}
137+
}
138+
return { enable: false }
139+
}
140+
}
141+
84142
function makeEnvironmentConfig() {
85143
return {
86144
agentId: valueOfString('PINPOINT_AGENT_ID'),
@@ -95,7 +153,8 @@ function makeEnvironmentConfig() {
95153
traceExclusionUrl: makeTraceExclusionUrlEnvironmentConfig(),
96154
sqlStats: valueOfBoolean('PINPOINT_PROFILER_SQL_STAT')
97155
},
98-
plugins: new PluginsEnvironmentConfigBuilder().build()
156+
plugins: makePluginsEnvironmentConfig(),
157+
enable: valueOfBoolean('PINPOINT_ENABLE')
99158
}
100159
}
101160

@@ -135,7 +194,7 @@ function makeSamplingEnvironmentConfig() {
135194
}
136195

137196
function makeLogLevelsEnvironmentConfig() {
138-
const value = process.env[envName]
197+
const value = process.env['PINPOINT_LOGGER_LEVELS']
139198
if (typeof value !== 'string' || value.trim().length === 0) {
140199
return undefined
141200
}

test/config.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const test = require('tape')
88
const { clear, getConfig, readConfigJson, readRootConfigFile, getMainModulePath } = require('../lib/config')
99
const { HttpStatusCodeErrorsBuilder, getHttpStatusCodeErrors, clearHttpStatusCodeErrors } = require('../lib/instrumentation/http/http-status-code-errors-builder')
1010
const log = require('../lib/utils/log/logger')
11+
const { ConfigBuilder } = require('../lib/config-builder')
1112

1213
test('Agent ID required field', function (t) {
1314
t.plan(1)
@@ -16,8 +17,7 @@ test('Agent ID required field', function (t) {
1617
delete process.env.PINPOINT_AGENT_ID
1718
delete process.env.PINPOINT_APPLICATION_NAME
1819

19-
const conf = getConfig()
20-
20+
const conf = new ConfigBuilder().build()
2121
t.true(conf.agentId.length == 16)
2222
})
2323

0 commit comments

Comments
 (0)