Skip to content

Commit 205181a

Browse files
committed
[#371] Add ConfigurationBuilder for flexible config management
1 parent fc7797c commit 205181a

File tree

2 files changed

+137
-77
lines changed

2 files changed

+137
-77
lines changed

lib/config.js

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,6 @@ const initializeConfig = (initOptions) => {
297297

298298
const clear = () => agentConfig && (agentConfig = null)
299299

300-
let configInstance
301-
const getConfig2 = () => {
302-
if (!configInstance) {
303-
configInstance = new ConfigBuilder().build()
304-
}
305-
return configInstance
306-
}
307-
308-
const setConfig = (config) => {
309-
if (!(config instanceof Config)) {
310-
throw new TypeError('config must be an instance of Config')
311-
}
312-
313-
configInstance = config
314-
}
315-
316300
//https://github.com/sindresorhus/is-docker
317301
const isContainerEnvironment = () => {
318302
return hasDockerEnv() || hasDockerCGroup() || (process.env['KUBERNETES_SERVICE_HOST'] && process.env['KUBERNETES_SERVICE_HOST'].length > 0)
@@ -388,8 +372,8 @@ class ConfigBuilder {
388372
config.container = true
389373
}
390374

391-
for (const [key, validation] of Object.entries(configurationValueValidations)) {
392-
validation()
375+
for (const [key, validation] of Object.entries(valueValidations)) {
376+
validation(config)
393377
}
394378

395379
if (typeof this.agentStartupUserDefinedJson['grpc.service_config'] === 'object') {
@@ -401,6 +385,82 @@ class ConfigBuilder {
401385
}
402386
}
403387

388+
const valueValidations = {
389+
validateTraceExclusionUrlCacheSize: (config) => {
390+
if (typeof config.traceExclusionUrlCacheSize !== 'undefined' && typeof config.traceExclusionUrlPatterns === 'undefined') {
391+
delete config.traceExclusionUrlCacheSize
392+
log.warn(`You have to set the PINPOINT_TRACE_EXCLUSION_URL_PATTERN, PINPOINT_TRACE_EXCLUSION_URL_CACHE_SIZE or trace-exclusion-url{ pattern: 'pattern', 'cache-size': 100} for using excludsion url cache.`)
393+
}
394+
395+
if (Array.isArray(config.traceExclusionUrlPatterns) && Number.isInteger(config.traceExclusionUrlCacheSize)) {
396+
if (config.traceExclusionUrlCacheSize < 100) {
397+
config.traceExclusionUrlCacheSize = 100
398+
}
399+
}
400+
},
401+
validateIds: (config) => {
402+
[{ id: config.agentId, name: 'Agent ID', maxLength: 24, required: true }
403+
, { id: config.applicationName, name: 'Application Name', maxLength: 24, required: true }
404+
// Java PinpointConstants.AGENT_NAME_MAX_LEN = 255
405+
, { id: config.agentName, name: 'Agent Name', maxLength: 255, required: false }
406+
].filter(id => id.id)
407+
.filter(id => {
408+
if (isNaN(id.maxLength)) {
409+
return false
410+
}
411+
412+
if (id.required && typeof id.id !== 'string') {
413+
config.enable = false
414+
log.warn(`You have to set ${id.name}`)
415+
return false
416+
}
417+
418+
if (id.required === false && typeof id.id !== 'string') {
419+
return false
420+
}
421+
422+
const maxLength = id.maxLength
423+
const idRegex = /^[a-zA-Z0-9\\._\\-]+$/
424+
425+
if (id.id.length < 1) {
426+
config.enable = false
427+
log.warn(`You have to set ${id.name}`)
428+
return false
429+
}
430+
431+
if (id.id.length > maxLength) {
432+
config.enable = false
433+
log.warn(`You have to set ${id.name} to less ${maxLength} characters.`)
434+
return false
435+
}
436+
437+
if (!idRegex.test(id.id)) {
438+
config.enable = false
439+
log.warn(`invalidate ${id.name} name with /[a-zA-Z0-9\\._\\-]+/ RegExp`)
440+
return false
441+
}
442+
})
443+
}
444+
}
445+
446+
let configInstance
447+
const getConfig2 = (json) => {
448+
if (!configInstance) {
449+
configInstance = new ConfigBuilder(json).build()
450+
}
451+
return configInstance
452+
}
453+
454+
const setConfig = (config) => {
455+
if (!(config instanceof Config)) {
456+
throw new TypeError('config must be an instance of Config')
457+
}
458+
459+
configInstance = config
460+
}
461+
462+
const clear2 = () => configInstance && (configInstance = null)
463+
404464
module.exports = {
405465
getConfig,
406466
clear,
@@ -412,4 +472,5 @@ module.exports = {
412472
ConfigBuilder,
413473
getConfig2,
414474
setConfig,
475+
clear2
415476
}

0 commit comments

Comments
 (0)