Skip to content

Commit 83baf7c

Browse files
committed
feat(ignore): add ignoreOutgoingHosts configuration
1 parent 8b5ae61 commit 83baf7c

5 files changed

Lines changed: 137 additions & 29 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ module.exports = {
6060
ignoreStatusCodes: [
6161
401,
6262
404
63+
],
64+
ignoreOutgoingHosts: [
65+
'google.com'
6366
]
6467
}
6568
```
6669

67-
**Here please pay special attention to the `ignoreHeaders` option. With this, you can specify which requests should not be accounted. This can be extremely useful if you want to filter out the noise generated by your health checks for example.**
68-
6970
*Note: Custom reporters are no longer supported in trace 2.x*
7071

7172
*Note: If you are running your app with NODE_ENV=test, Trace won't start*

lib/config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var url = require('url')
21
var config = {}
32

43
config.collectInterval = 2 * 60 * 1000
@@ -17,10 +16,6 @@ config.collectorApiHealthcheckEndpoint = '/service/%s/healthcheck'
1716

1817
config.configPath = 'trace.config'
1918

20-
var collectorApiHost = url.parse(config.collectorApiUrl).host
21-
22-
config.whiteListHosts = [
23-
collectorApiHost
24-
]
19+
config.whiteListHosts = []
2520

2621
module.exports = config

lib/config.spec.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/utils/configReader.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,24 @@ ConfigReader.prototype._getEnvVarConfig = function () {
6060
apiKey: process.env.TRACE_API_KEY
6161
}
6262

63+
var ignoreHeaders
64+
var ignoreOutgoingHosts
65+
6366
if (process.env.TRACE_IGNORE_HEADERS) {
6467
try {
65-
var ignoreHeaders = JSON.parse(process.env.TRACE_IGNORE_HEADERS)
68+
ignoreHeaders = JSON.parse(process.env.TRACE_IGNORE_HEADERS)
6669
defaults(envVarConfig, { ignoreHeaders: ignoreHeaders })
6770
} catch (err) {
68-
console.error(format('%s trace: warning: Cannot parse TRACE_IGNORE_HEADERS. Error: %s', new Date(), err.message))
71+
console.error(format('%s trace: warning: Cannot parse TRACE_IGNORE_HEADERS. Error: %s', err.message))
72+
}
73+
}
74+
75+
if (process.env.TRACE_IGNORE_OUTGOING_HOSTS) {
76+
try {
77+
ignoreOutgoingHosts = JSON.parse(process.env.TRACE_IGNORE_OUTGOING_HOSTS)
78+
defaults(envVarConfig, { ignoreOutgoingHosts: ignoreOutgoingHosts })
79+
} catch (err) {
80+
console.error(format('%s trace: warning: Cannot parse TRACE_IGNORE_OUTGOING_HOSTS. Error: %s', err.message))
6981
}
7082
}
7183

@@ -80,15 +92,30 @@ ConfigReader.prototype._readConfigFile = function (file) {
8092
return require(file)
8193
}
8294

83-
ConfigReader.prototype._getFileConfig = function (file) {
84-
if (file) {
85-
try {
86-
fs.statSync(path.resolve(file + '.js'))
87-
} catch (ex) {
88-
debug('Configuration file not found')
89-
return { }
90-
}
95+
ConfigReader.prototype._isConfigFileExists = function (file) {
96+
var failCounter = 0
97+
try {
98+
fs.statSync(path.resolve(file + '.js'))
99+
} catch (ex) {
100+
failCounter += 1
101+
}
102+
103+
try {
104+
fs.statSync(path.resolve(file))
105+
} catch (ex) {
106+
failCounter += 1
107+
}
91108

109+
if (failCounter < 2) {
110+
return true
111+
} else {
112+
debug('Configuration file not found')
113+
return false
114+
}
115+
}
116+
117+
ConfigReader.prototype._getFileConfig = function (file) {
118+
if (file && this._isConfigFileExists(file)) {
92119
try {
93120
return this._readConfigFile(path.resolve(file))
94121
} catch (ex) {
@@ -183,7 +210,9 @@ ConfigReader.prototype.getConfig = function () {
183210
defaultConfig
184211
)
185212

186-
config.whiteListHosts = [url.parse(config.collectorApiUrl).host]
213+
config.whiteListHosts = [
214+
url.parse(config.collectorApiUrl).host
215+
].concat(config.ignoreOutgoingHosts || [])
187216

188217
this.checkApiToken(config.apiKey)
189218

lib/utils/configReader.spec.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,99 @@ describe('Config Reader module', function () {
8787
expect(config.test).to.eql('system')
8888
})
8989

90+
it('whitelists default collectorApiUrl host', function () {
91+
var configReader = ConfigReader.create({
92+
apiKey: testApiToken,
93+
serviceName: 'test'
94+
})
95+
96+
var config = configReader.getConfig()
97+
98+
expect(config.whiteListHosts).to.be.eql([ 'trace-collector-api.risingstack.com' ])
99+
})
100+
101+
it('whitelists collectorApiUrl host from config', function () {
102+
var configReader = ConfigReader.create({
103+
collectorApiUrl: 'http://c.a.b',
104+
apiKey: testApiToken,
105+
serviceName: 'test'
106+
})
107+
108+
var config = configReader.getConfig()
109+
110+
expect(config.whiteListHosts).to.be.eql([ 'c.a.b' ])
111+
})
112+
113+
it('appends whitelist hosts from parameter config', function () {
114+
var configReader = ConfigReader.create({
115+
collectorApiUrl: 'http://c.a.b',
116+
ignoreOutgoingHosts: [ 'fake.host1.com', 'fake.host2.com' ],
117+
apiKey: testApiToken,
118+
serviceName: 'test'
119+
})
120+
121+
var config = configReader.getConfig()
122+
123+
expect(config.whiteListHosts).to.be.eql([ 'c.a.b', 'fake.host1.com', 'fake.host2.com' ])
124+
})
125+
126+
it('appends whitelist hosts from environment config', function () {
127+
process.env.TRACE_IGNORE_OUTGOING_HOSTS = JSON.stringify(
128+
[ 'fake.host1.com', 'fake.host2.com' ]
129+
)
130+
131+
var configReader = ConfigReader.create({
132+
collectorApiUrl: 'http://c.a.b',
133+
apiKey: testApiToken,
134+
serviceName: 'test'
135+
})
136+
137+
try {
138+
var config = configReader.getConfig()
139+
expect(config.whiteListHosts).to.be.eql([ 'c.a.b', 'fake.host1.com', 'fake.host2.com' ])
140+
141+
delete process.env.TRACE_IGNORE_OUTGOING_HOSTS
142+
} catch (err) {
143+
delete process.env.TRACE_IGNORE_OUTGOING_HOSTS
144+
throw err
145+
}
146+
})
147+
148+
it('does not throw error if TRACE_IGNORE_OUTGOING_HOSTS is malformed, and uses the next highest priority source for the ignoreHeaders config', function () {
149+
process.env.TRACE_IGNORE_OUTGOING_HOSTS = 'This is not a valid JSON'
150+
var configReader = ConfigReader.create({
151+
apiKey: testApiToken,
152+
serviceName: 'test',
153+
configPath: 'test'
154+
})
155+
var log = this.sandbox.spy(console, 'error')
156+
var expected = [ 'c.a.b', 'fake.host1.com', 'fake.host2.com' ]
157+
158+
this.sandbox.stub(configReader, '_readConfigFile', function () {
159+
return { ignoreOutgoingHosts: expected.slice(1) }
160+
})
161+
162+
this.sandbox.stub(configReader, '_getDefaultConfig', function () {
163+
return {
164+
collectorApiUrl: 'http://c.a.b'
165+
}
166+
})
167+
168+
this.sandbox.stub(fs, 'statSync', function () {
169+
return { }
170+
})
171+
172+
try {
173+
var config = configReader.getConfig()
174+
expect(config.whiteListHosts).to.eql(expected)
175+
expect(log).to.have.been.calledOnce
176+
delete process.env.TRACE_IGNORE_OUTGOING_HOSTS
177+
} catch (err) {
178+
delete process.env.TRACE_IGNORE_OUTGOING_HOSTS
179+
throw err
180+
}
181+
})
182+
90183
it('file config should override default config', function () {
91184
var configReader = ConfigReader.create({
92185
serviceName: 'test',

0 commit comments

Comments
 (0)