Skip to content

Commit 67336d2

Browse files
committed
[#404] Configuration Reconstructing
1 parent 7c619e2 commit 67336d2

File tree

1 file changed

+78
-44
lines changed

1 file changed

+78
-44
lines changed

lib/config-builder.js

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ConfigBuilder {
4343
const config = Object.assign({},
4444
this.defaultJson,
4545
this.userDefinedJson,
46+
makeEnvironmentConfig(),
4647
this.agentStartupUserDefinedJson
4748
)
4849
return new Config(config)
@@ -80,66 +81,99 @@ class ConfigBuilder {
8081
}
8182
}
8283

83-
class EnvironmentConfig {
84-
constructor() {
85-
this.env = {
86-
agentId: valueOfString('PINPOINT_AGENT_ID'),
87-
agentName: valueOfString('PINPOINT_AGENT_NAME'),
88-
applicationName: valueOfString('PINPOINT_APPLICATION_NAME'),
89-
applicationServiceType: valueOfNumber('PINPOINT_SERVICE_TYPE'),
90-
collector: new CollectorEnvironmentConfig(),
91-
sampling: new SamplingEnvironmentConfig(),
92-
features: {
93-
container: valueOfBoolean('PINPOINT_CONTAINER'),
94-
logLevels: new LogLevelsEnvironmentConfig(),
95-
traceExclusionUrl: new TraceExclusionUrlEnvironmentConfig(),
96-
sqlStats: valueOfBoolean('PINPOINT_PROFILER_SQL_STAT')
97-
},
98-
plugins: new PluginsEnvironmentConfig()
99-
}
84+
function makeEnvironmentConfig() {
85+
return {
86+
agentId: valueOfString('PINPOINT_AGENT_ID'),
87+
agentName: valueOfString('PINPOINT_AGENT_NAME'),
88+
applicationName: valueOfString('PINPOINT_APPLICATION_NAME'),
89+
applicationServiceType: valueOfNumber('PINPOINT_SERVICE_TYPE'),
90+
collector: makeCollectorEnvironmentConfig(),
91+
sampling: makeSamplingEnvironmentConfig(),
92+
features: {
93+
container: valueOfBoolean('PINPOINT_CONTAINER'),
94+
logLevels: makeLogLevelsEnvironmentConfig(),
95+
traceExclusionUrl: makeTraceExclusionUrlEnvironmentConfig(),
96+
sqlStats: valueOfBoolean('PINPOINT_PROFILER_SQL_STAT')
97+
},
98+
plugins: new PluginsEnvironmentConfigBuilder().build()
10099
}
101100
}
102101

103-
class CollectorEnvironmentConfig {
104-
constructor() {
105-
this.env = {
106-
ip: valueOfString('PINPOINT_COLLECTOR_IP'),
107-
spanPort: valueOfNumber('PINPOINT_COLLECTOR_SPAN_PORT'),
108-
statPort: valueOfNumber('PINPOINT_COLLECTOR_STAT_PORT'),
109-
tcpPort: valueOfNumber('PINPOINT_COLLECTOR_TCP_PORT'),
110-
}
102+
function valueOfString(envName) {
103+
const value = process.env[envName]
104+
return value !== undefined ? value : undefined
105+
}
106+
107+
function valueOfNumber(envName) {
108+
const value = process.env[envName]
109+
return value !== undefined ? Number(value) : undefined
110+
}
111+
112+
function valueOfBoolean(envName) {
113+
const value = process.env[envName]
114+
return typeof value === 'string' ? value.toLowerCase() === 'true' : undefined
115+
}
116+
117+
function makeCollectorEnvironmentConfig() {
118+
return {
119+
ip: valueOfString('PINPOINT_COLLECTOR_IP'),
120+
spanPort: valueOfNumber('PINPOINT_COLLECTOR_SPAN_PORT'),
121+
statPort: valueOfNumber('PINPOINT_COLLECTOR_STAT_PORT'),
122+
tcpPort: valueOfNumber('PINPOINT_COLLECTOR_TCP_PORT'),
111123
}
112124
}
113125

114-
class SamplingEnvironmentConfig {
115-
constructor() {
116-
const samplingEnable = valueOfBoolean('PINPOINT_SAMPLING')
117-
if (!samplingEnable) {
118-
return
119-
}
126+
function makeSamplingEnvironmentConfig() {
127+
const samplingEnable = valueOfBoolean('PINPOINT_SAMPLING')
128+
if (!samplingEnable) {
129+
return
130+
}
120131

121-
this.env = {
122-
rate: valueOfNumber('PINPOINT_SAMPLING_RATE')
123-
}
132+
return {
133+
rate: valueOfNumber('PINPOINT_SAMPLING_RATE')
124134
}
125135
}
126136

127-
class LogLevelsEnvironmentConfig {
128-
constructor() {
129-
this.levels = valueOfLoggerLevels('PINPOINT_LOGGER_LEVELS')
137+
function makeLogLevelsEnvironmentConfig() {
138+
const value = process.env[envName]
139+
if (typeof value !== 'string' || value.trim().length === 0) {
140+
return undefined
141+
}
142+
143+
const loggerLevels = {}
144+
const pairs = value.split(',')
145+
146+
const validLevels = Object.keys(levels).map(level => level.toUpperCase())
147+
for (const pair of pairs) {
148+
const trimmedPair = pair.trim()
149+
if (trimmedPair.length === 0) continue
150+
151+
const equalIndex = trimmedPair.indexOf('=')
152+
if (equalIndex === -1) continue
153+
154+
const loggerName = trimmedPair.substring(0, equalIndex).trim()
155+
const logLevel = trimmedPair.substring(equalIndex + 1).trim().toUpperCase()
156+
157+
if (loggerName.length === 0 || logLevel.length === 0) continue
158+
159+
if (validLevels.includes(logLevel)) {
160+
loggerLevels[loggerName] = logLevel
161+
}
130162
}
163+
164+
return Object.keys(loggerLevels).length > 0 ? loggerLevels : undefined
131165
}
132166

133-
class TraceExclusionUrlEnvironmentConfig {
134-
constructor() {
135-
this.patterns = valueOfString('PINPOINT_TRACE_EXCLUSION_URL_PATTERNS')
136-
this.cacheSize = valueOfNumber('PINPOINT_TRACE_EXCLUSION_URL_CACHE_SIZE')
167+
function makeTraceExclusionUrlEnvironmentConfig() {
168+
return {
169+
patterns: valueOfString('PINPOINT_TRACE_EXCLUSION_URL_PATTERNS'),
170+
cacheSize: valueOfNumber('PINPOINT_TRACE_EXCLUSION_URL_CACHE_SIZE')
137171
}
138172
}
139173

140-
class PluginsEnvironmentConfig {
141-
constructor() {
142-
this.httpErrorStatusCodes = valueOfString('PINPOINT_HTTP_STATUS_CODE_ERRORS')
174+
function makePluginsEnvironmentConfig() {
175+
return {
176+
httpErrorStatusCodes: valueOfString('PINPOINT_HTTP_STATUS_CODE_ERRORS')
143177
}
144178
}
145179

0 commit comments

Comments
 (0)