Skip to content

Commit d715700

Browse files
committed
[#371] Add ConfigurationBuilder for flexible config management
1 parent b4497ec commit d715700

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

lib/config.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,70 @@ function registerLoadedConfig(propertyName, callback) {
337337
loadedConfigCallbacks.push({ propertyName, callback })
338338
}
339339

340+
class Config {
341+
constructor(config) {
342+
Object.assign(this, config)
343+
Object.freeze(this)
344+
}
345+
}
346+
347+
class ConfigBuilder {
348+
constructor(agentStartupUserDefinedJson = {}) {
349+
this.agentStartupUserDefinedJson = agentStartupUserDefinedJson
350+
}
351+
352+
setDefaultJson(json) {
353+
this.defaultJson = json
354+
return this
355+
}
356+
357+
setUserDefinedJson(json) {
358+
this.userDefinedJson = json
359+
return this
360+
}
361+
362+
build() {
363+
if (!this.defaultJson) {
364+
this.defaultJson = require('./pinpoint-config-default')
365+
}
366+
367+
if (!this.userDefinedJson) {
368+
this.userDefinedJson = readRootConfigFile()
369+
}
370+
371+
const config = Object.assign({},
372+
readConfigJson(this.defaultJson),
373+
readConfigJson(this.userDefinedJson),
374+
readFromEnv(),
375+
readConfigJson(this.agentStartupUserDefinedJson))
376+
377+
Object.entries(REQUIRE_CONFIG).forEach(([propertyName, description]) => {
378+
if (propertyName === 'agentId' && !config[propertyName]) {
379+
return config.agentId = randomBytes(8).toString('hex')
380+
}
381+
if (config.enable && !config[propertyName]) {
382+
config.enable = false
383+
log.warn(`You must set ${description}.The Pinpoint Node JS Agent has been shutdown.`)
384+
}
385+
})
386+
387+
if (!ENV_MAP.container() && isContainerEnvironment()) {
388+
config.container = true
389+
}
390+
391+
for (const [key, validation] of Object.entries(configurationValueValidations)) {
392+
validation()
393+
}
394+
395+
if (typeof this.agentStartupUserDefinedJson['grpc.service_config'] === 'object') {
396+
config.grpcServiceConfig = new ServiceConfigBuilder().setJSON(this.agentStartupUserDefinedJson['grpc.service_config']).build()
397+
} else {
398+
config.grpcServiceConfig = ServiceConfigBuilder.nullObject.build()
399+
}
400+
return new Config(config)
401+
}
402+
}
403+
340404
module.exports = {
341405
getConfig,
342406
clear,
@@ -346,4 +410,5 @@ module.exports = {
346410
isContainerEnvironment,
347411
initializeConfig,
348412
registerLoadedConfig,
413+
ConfigBuilder
349414
}

0 commit comments

Comments
 (0)