Skip to content

Commit 2528d85

Browse files
committed
feat(cloudFoundry): serviceName can be read from VCAP_APPLICATION
1 parent 4d52533 commit 2528d85

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/utils/configReader.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ ConfigReader.prototype._getSystemConfig = function () {
2929
}
3030
}
3131

32+
ConfigReader.prototype._getCloudFoundryConfig = function () {
33+
var VCAP_APPLICATION = process.env.VCAP_APPLICATION
34+
35+
try {
36+
VCAP_APPLICATION = JSON.parse(VCAP_APPLICATION)
37+
} catch (ex) {
38+
return {}
39+
}
40+
41+
return {
42+
serviceName: VCAP_APPLICATION.name
43+
}
44+
}
45+
3246
ConfigReader.prototype._getEnvVarConfig = function () {
3347
var envVarConfig = {
3448
collectInterval: process.env.TRACE_COLLECT_INTERVAL,
@@ -82,6 +96,7 @@ ConfigReader.prototype.getConfig = function () {
8296
var systemConfig = this._getSystemConfig()
8397
var envVarConfig = this._getEnvVarConfig()
8498
var defaultConfig = this._getDefaultConfig()
99+
var cloudFoundryConfig = this._getCloudFoundryConfig()
85100

86101
var config = defaults({}, parameterConfig, systemConfig, envVarConfig)
87102

@@ -92,6 +107,7 @@ ConfigReader.prototype.getConfig = function () {
92107
config = defaults(
93108
config,
94109
fileConfig,
110+
cloudFoundryConfig,
95111
defaultConfig
96112
)
97113

lib/utils/configReader.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ describe('Config Reader module', function () {
159159
expect(config.test).to.eql('param')
160160
})
161161

162+
it('works with Cloud Foundry', function () {
163+
process.env.VCAP_APPLICATION = JSON.stringify({
164+
name: 'test-app'
165+
})
166+
167+
var configReader = ConfigReader.create({
168+
reporter: 'dummy',
169+
collectorApiUrl: 'http://c.a.b',
170+
apiKey: 'api-key'
171+
})
172+
173+
var cfg = configReader.getConfig()
174+
175+
expect(cfg.serviceName).to.eql('test-app')
176+
delete process.env.VCAP_APPLICATION
177+
})
178+
162179
it('can find config file by default config', function () {
163180
var configReader = ConfigReader.create({
164181
serviceName: 'test',

0 commit comments

Comments
 (0)