Skip to content

Commit 5570be8

Browse files
committed
[#371] Add ConfigurationBuilder for flexible config management
1 parent ecc82e5 commit 5570be8

File tree

9 files changed

+48
-44
lines changed

9 files changed

+48
-44
lines changed

lib/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ class ConfigBuilder {
376376
validation(config)
377377
}
378378

379-
if (typeof this.agentStartupUserDefinedJson['grpc.service_config'] === 'object') {
380-
config.grpcServiceConfig = new ServiceConfigBuilder().setJSON(this.agentStartupUserDefinedJson['grpc.service_config']).build()
379+
if (typeof this.agentStartupUserDefinedJson?.grpc?.service_config === 'object') {
380+
config.grpcServiceConfig = new ServiceConfigBuilder().setJSON(this.agentStartupUserDefinedJson.grpc.service_config).build()
381381
} else {
382382
config.grpcServiceConfig = ServiceConfigBuilder.nullObject.build()
383383
}

lib/context/method-descriptor-builder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class MethodDescriptorBuilder {
8383
if (this.apiDescriptor) {
8484
return this.apiDescriptor
8585
}
86-
86+
8787
const cacheIds = [this.formattedStringOfCall()]
8888

8989
if (typeof this.fileName === 'string') {
@@ -177,7 +177,7 @@ class MethodDescriptorBuilder {
177177
}
178178

179179
shouldCallSiteFileNameAndLineNumber() {
180-
const conf = config.getConfig()
180+
const conf = config.getConfig2()
181181
if (!conf || !conf.traceLocationAndFileNameOfCallSite) {
182182
return false
183183
}

lib/instrumentation/sql/parsing-result.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const UidParsingResult = require('./uid-parsing-result')
1212

1313
class ParsingResult {
1414
constructor(originalSql, normalizedSql) {
15-
const conf = config.getConfig()
15+
const conf = config.getConfig2()
1616
if (conf && conf.profilerSqlStat) {
1717
this.result = new UidParsingResult(originalSql, normalizedSql)
1818
} else {

test/agent/env-config.test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
*/
66

77
const test = require('tape')
8-
const config = require('../../lib/config')
8+
const { getConfig2, clear2, isContainerEnvironment } = require('../../lib/config')
99

1010
function isRunGithubAction() {
11-
return config.isContainerEnvironment()
11+
return isContainerEnvironment
1212
}
1313

1414

1515
test('should return the string value when the env value is string type', function(t) {
16-
config.clear()
16+
clear2()
1717

1818
process.env['PINPOINT_AGENT_ID'] = "agentId"
1919
process.env['PINPOINT_APPLICATION_NAME'] = "appication name"
2020
process.env['PINPOINT_COLLECTOR_IP'] = "192.168.78.79"
2121
process.env['PINPOINT_LOG_LEVEL'] = "Debug"
2222

23-
const given = config.getConfig()
23+
const given = getConfig2()
2424
t.equal(given.agentId, "agentId", "given PINPOINT_AGENT_ID env, should equal config")
2525
t.equal(given.applicationName, "appication name", "given PINPOINT_APPLICATION_NAME env, should equal config")
2626
t.equal(given.collectorIp, "192.168.78.79", "given PINPOINT_COLLECTOR_IP env, should equal config")
@@ -31,15 +31,15 @@ test('should return the string value when the env value is string type', functio
3131
delete process.env.PINPOINT_COLLECTOR_IP
3232
delete process.env.PINPOINT_LOG_LEVEL
3333

34-
config.clear()
34+
clear2()
3535
t.end()
3636
})
3737

3838
const givenDefaultIdAndName = () => {
3939
process.env['PINPOINT_AGENT_ID'] = "agentId"
4040
process.env['PINPOINT_APPLICATION_NAME'] = "appication name"
4141

42-
const given = config.getConfig()
42+
const given = getConfig2()
4343

4444
delete process.env.PINPOINT_AGENT_ID
4545
delete process.env.PINPOINT_APPLICATION_NAME
@@ -48,7 +48,7 @@ const givenDefaultIdAndName = () => {
4848
}
4949

5050
test('should return the number value when the env value is number type', function(t) {
51-
config.clear()
51+
clear2()
5252

5353
process.env['PINPOINT_SERVICE_TYPE'] = "1400"
5454
process.env['PINPOINT_COLLECTOR_TCP_PORT'] = "9894"
@@ -69,12 +69,12 @@ test('should return the number value when the env value is number type', functio
6969
delete process.env.PINPOINT_COLLECTOR_SPAN_PORT
7070
delete process.env.PINPOINT_SAMPLING_RATE
7171

72-
config.clear()
72+
clear2()
7373
t.end()
7474
})
7575

7676
test('should return the true value when the env value is boolean type', function(t) {
77-
config.clear()
77+
clear2()
7878

7979
process.env['PINPOINT_SAMPLING'] = "true"
8080
process.env['PINPOINT_ENABLE'] = "true"
@@ -89,18 +89,18 @@ test('should return the true value when the env value is boolean type', function
8989
delete process.env.PINPOINT_ENABLE
9090
delete process.env.PINPOINT_CONTAINER
9191

92-
config.clear()
92+
clear2()
9393
t.end()
9494
})
9595

9696
test('should return the false value when the env value is boolean type', function(t) {
97-
config.clear()
97+
clear2()
9898

9999
process.env['PINPOINT_SAMPLING'] = "false"
100100
process.env['PINPOINT_ENABLE'] = "false"
101101
process.env['PINPOINT_CONTAINER'] = "false"
102102

103-
const given = config.getConfig()
103+
const given = getConfig2()
104104
t.equal(given.sampling, false, 'given PINPOINT_SAMPLING env, should equal config')
105105
t.equal(given.enable, false, 'given PINPOINT_ENABLE env, should equal config')
106106
if (!isRunGithubAction()) {
@@ -111,12 +111,12 @@ test('should return the false value when the env value is boolean type', functio
111111
delete process.env.PINPOINT_ENABLE
112112
delete process.env.PINPOINT_CONTAINER
113113

114-
config.clear()
114+
clear2()
115115
t.end()
116116
})
117117

118118
test('should not exist in the process.env property when you do not set an environment variable', function(t) {
119-
config.clear()
119+
clear2()
120120

121121
delete process.env.PINPOINT_COLLECTOR_IP
122122
delete process.env.PINPOINT_LOG_LEVEL
@@ -149,12 +149,12 @@ test('should not exist in the process.env property when you do not set an enviro
149149
t.equal(given.collectorSpanPort, 9993, 'No set PINPOINT_COLLECTOR_SPAN_PORT env, should equal default config')
150150
t.equal(given.sampleRate, 10, 'No set PINPOINT_SAMPLING_RATE env, should equal default config')
151151

152-
config.clear()
152+
clear2()
153153
t.end()
154154
})
155155

156156
test(`default config`, (t) => {
157-
config.clear()
157+
clear2()
158158

159159
const given = givenDefaultIdAndName()
160160
t.equal(given.enabledActiveThreadCount, false, 'No set activeThreadCount')
@@ -163,10 +163,10 @@ test(`default config`, (t) => {
163163
})
164164

165165
test(`detect container`, (t) => {
166-
config.clear()
166+
clear2()
167167

168168
process.env['KUBERNETES_SERVICE_HOST'] = "aaa"
169-
const given = config.getConfig()
169+
const given = getConfig2()
170170

171171
t.plan(1)
172172
t.equal(given.container, true, 'container detect')
@@ -175,10 +175,10 @@ test(`detect container`, (t) => {
175175
})
176176

177177
test(`detect container2`, (t) => {
178-
config.clear()
178+
clear2()
179179

180180
if (!isRunGithubAction()) {
181-
const given = config.getConfig()
181+
const given = getConfig2()
182182
t.equal(given.container, false, 'container detect')
183183
}
184184

test/client/grpc-fixture.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'use strict'
88

99
const agent = require('../support/agent-singleton-mock')
10-
const config = require('../../lib/config')
10+
const { clear2, getConfig2 } = require('../../lib/config')
1111
const GrpcDataSender = require('../../lib/client/grpc-data-sender')
1212
const SpanBuilder = require('../../lib/context/span-builder')
1313
const RemoteTraceRootBuilder = require('../../lib/context/remote-trace-root-builder')
@@ -20,15 +20,19 @@ let callMetadata = []
2020
function beforeSpecificOne(port, one, serviceConfig) {
2121
callCount = 0
2222
afterCount = 0
23-
config.clear()
23+
clear2()
2424
callRequests = []
2525
callMetadata = []
26-
const actualConfig = config.getConfig({ 'grpc.service_config': serviceConfig })
27-
actualConfig.collectorIp = '127.0.0.1'
28-
actualConfig.collectorTcpPort = port
29-
actualConfig.collectorStatPort = port
30-
actualConfig.collectorSpanPort = port
31-
actualConfig.enabledDataSending = true
26+
const actualConfig = getConfig2({
27+
'grpc': { 'service_config': serviceConfig },
28+
'collector': {
29+
'ip': '127.0.0.1',
30+
'span-port': port,
31+
'stat-port': port,
32+
'tcp-port': port
33+
},
34+
'enabled-data-sending': true,
35+
})
3236
const dataSource = new one(
3337
actualConfig.collectorIp,
3438
actualConfig.collectorTcpPort,

test/client/grpc-unary-rpc.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const AgentInfo = require('../../lib/data/dto/agent-info')
1414
const ApiMetaInfo = require('../../lib/data/dto/api-meta-info')
1515
const StringMetaInfo = require('../../lib/data/dto/string-meta-info')
1616
const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-builder')
17-
const config = require('../../lib/config')
17+
const { clear2, getConfig2 } = require('../../lib/config')
1818
const SqlMetaData = require('../../lib/client/sql-meta-data')
1919
const sqlMetadataService = require('../../lib/instrumentation/sql/sql-metadata-service')
2020
const SqlUidMetaData = require('../../lib/client/sql-uid-meta-data')
@@ -424,10 +424,10 @@ test('sendSqlMetaData retry', (t) => {
424424
})
425425

426426
test('sendSqlUidMetaData retry', (t) => {
427-
config.clear()
427+
clear2()
428428
process.env['PINPOINT_PROFILER_SQL_STAT'] = 'true'
429429
sqlMetadataService.cache.cache.clear()
430-
const conf = config.getConfig()
430+
const conf = getConfig2()
431431
t.true(conf.profilerSqlStat, 'profiler SQL Stat is false')
432432

433433
const server = new grpc.Server()
@@ -467,7 +467,7 @@ test('sendSqlUidMetaData retry', (t) => {
467467
dataSender.close()
468468
server.forceShutdown()
469469
delete process.env.PINPOINT_PROFILER_SQL_STAT
470-
config.clear()
470+
clear2()
471471
})
472472
})
473473
})

test/fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const TraceId = require('../lib/context/trace-id')
99
const SpanId = require('../lib/context/span-id')
1010
const testConfig= require('./pinpoint-config-test')
1111
require('../lib/config').clear()
12-
const config = require('../lib/config').getConfig(testConfig)
12+
const config = require('../lib/config').getConfig2(testConfig)
1313
const { namedGroupLocationFileName, namedGroupTypeMethod } = require('../lib/instrumentation/call-stack')
1414

1515
const getTransactionId = () => {

test/support/agent-singleton-mock.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const activeRequestRepository = require('../../lib/metric/active-request-reposit
2222
const GrpcDataSender = require('../../lib/client/grpc-data-sender')
2323
const { AgentBuilder } = require('../../lib/agent-builder')
2424
const AgentInfo = require('../../lib/data/dto/agent-info')
25-
const { getConfig } = require('../../lib/config')
25+
const { getConfig2, clear2 } = require('../../lib/config')
2626

2727
let traces = []
2828
const resetTraces = () => {
@@ -84,7 +84,7 @@ json.collector['span-port'] = -1
8484
json.collector['stat-port'] = -1
8585
json.collector['tcp-port'] = -1
8686
json = Object.assign({}, require('../pinpoint-config-test'), json)
87-
const config = getConfig(json)
87+
const config = getConfig2(json)
8888
const agentInfo = AgentInfo.make(config)
8989
const agentBuilder = new AgentBuilder(agentInfo)
9090
.setConfig(config)
@@ -106,8 +106,8 @@ class MockAgent {
106106
} else {
107107
json = Object.assign({}, require('../pinpoint-config-test.json'), json)
108108
}
109-
require('../../lib/config').clear()
110-
const config = require('../../lib/config').getConfig(json)
109+
clear2()
110+
const config = require('../../lib/config').getConfig2(json)
111111
this.config = config
112112

113113
this.agentInfo = AgentInfo.make(config)

test/utils/log/logger.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ test('per-appender loggingLevel configuration', (t) => {
371371
test('PINPOINT_LOGGER_LEVELS environment variable log levels', (t) => {
372372
t.plan(30)
373373
const config = require('../../../lib/config')
374-
config.clear()
374+
config.clear2()
375375
process.env['PINPOINT_LOGGER_LEVELS'] = 'default-logger=WARN,.debug-appender=DEBUG,grpc.info-appender=DEBUG,.noLevelAppender=INFO,grpc=INFO'
376376

377377
const infoAppenderMessages = []
@@ -412,7 +412,7 @@ test('PINPOINT_LOGGER_LEVELS environment variable log levels', (t) => {
412412
error: (msg) => debugAppenderMessages.push(`DEBUG-APPENDER ERROR: ${msg}`)
413413
}
414414

415-
const confWithAppender = config.getConfig()
415+
const confWithAppender = config.getConfig2()
416416
log.setRootLogger(LogBuilder.createDefaultLogBuilder()
417417
.setConfig(confWithAppender)
418418
.addAppender(infoAppender)

0 commit comments

Comments
 (0)