Skip to content

Commit 466b20a

Browse files
committed
feat(collector): add hostname and pid to requests
1 parent d1770ff commit 466b20a

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

lib/agent/api/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var requestSync = require('sync-request')
55
var isNumber = require('lodash.isnumber')
66
var debug = require('debug')('risingstack/trace')
77
var format = require('util').format
8+
var assign = require('lodash.assign')
89

910
var bl = require('bl')
1011
var libPackage = require('../../../package')
@@ -16,6 +17,8 @@ function CollectorApi (options) {
1617
this.COLLECTOR_API_RPM_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiRpmMetricsEndpoint)
1718

1819
this.apiKey = options.apiKey
20+
this.processId = options.processId
21+
this.hostname = options.hostname
1922
this.serviceName = options.serviceName
2023
this.baseRetryInterval = 100
2124
this.retryCount = 0
@@ -76,7 +79,7 @@ CollectorApi.prototype.sendRpmMetrics = function (data) {
7679
}
7780

7881
var url = util.format(this.COLLECTOR_API_RPM_METRICS, this.serviceKey)
79-
this._send(url, data)
82+
this._send(url, assign({ hostname: this.hostname, pid: this.processId }, data))
8083
}
8184

8285
CollectorApi.prototype.sendApmMetrics = function (data) {
@@ -86,12 +89,12 @@ CollectorApi.prototype.sendApmMetrics = function (data) {
8689
}
8790

8891
var url = util.format(this.COLLECTOR_API_METRICS, this.serviceKey)
89-
this._send(url, data)
92+
this._send(url, assign({ hostname: this.hostname, pid: this.processId }, data))
9093
}
9194

9295
CollectorApi.prototype.sendSamples = function (data) {
9396
var url = this.COLLECTOR_API_SAMPLE
94-
this._send(url, data)
97+
this._send(url, assign({ hostname: this.hostname, pid: this.processId }, data))
9598
}
9699

97100
CollectorApi.prototype._getRetryInterval = function () {

lib/agent/api/index.spec.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe('The Trace CollectorApi module', function () {
1515
collectorApiSampleEndpoint: '/service/sample',
1616
collectorApiServiceEndpoint: '/service',
1717
collectorApiApmMetricsEndpoint: '/service/%s/apm-metrics',
18-
collectorApiRpmMetricsEndpoint: '/service/%s/rpm-metrics'
18+
collectorApiRpmMetricsEndpoint: '/service/%s/rpm-metrics',
19+
hostname: 'test.org',
20+
processId: '7777'
1921
}
2022

2123
it('can be instantiated w/ serviceName and apiKey', function () {
@@ -27,7 +29,9 @@ describe('The Trace CollectorApi module', function () {
2729
var serviceKey = 12
2830

2931
var data = {
30-
trace: 'very data'
32+
trace: 'very data',
33+
hostname: 'test.org',
34+
pid: 7777
3135
}
3236

3337
var path = util.format(defaultConfig.collectorApiRpmMetricsEndpoint, serviceKey)
@@ -40,14 +44,15 @@ describe('The Trace CollectorApi module', function () {
4044

4145
collectorApi.sendRpmMetrics(data)
4246

43-
expect(sendStub).to.have.been.calledOnce
4447
expect(sendStub).to.have.been.calledWith(sendUrl, data)
4548
})
4649

4750
it('sends samples to the collector server', function (done) {
4851
var collectorApi = CollectorApi.create(defaultConfig)
4952
var data = {
50-
trace: 'very data'
53+
trace: 'very data',
54+
hostname: 'test.org',
55+
pid: 7777
5156
}
5257

5358
nock(defaultConfig.collectorApiUrl, {

lib/utils/configReader.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
var path = require('path')
4+
var os = require('os')
45
var url = require('url')
56
var debug = require('debug')('risingstack/trace')
67
var defaults = require('lodash.defaults')
@@ -10,6 +11,22 @@ function ConfigReader (config) {
1011
this.parameterConfig = config || { }
1112
}
1213

14+
ConfigReader.prototype._getSystemConfig = function () {
15+
return {
16+
osArch: process.arch,
17+
osPlatform: process.platform,
18+
osRelease: os.release(),
19+
hostname: os.hostname(),
20+
cpus: os.cpus().map(function (cpu) {
21+
delete cpu.times
22+
return cpu
23+
}),
24+
processName: process.title,
25+
processId: process.pid,
26+
processVersion: process.version
27+
}
28+
}
29+
1330
ConfigReader.prototype._getEnvVarConfig = function () {
1431
var envVarConfig = {
1532
collectInterval: process.env.TRACE_COLLECT_INTERVAL,
@@ -59,12 +76,11 @@ ConfigReader.prototype._getFileConfig = function (file) {
5976

6077
ConfigReader.prototype.getConfig = function () {
6178
var parameterConfig = this.parameterConfig
79+
var systemConfig = this._getSystemConfig()
6280
var envVarConfig = this._getEnvVarConfig()
6381
var defaultConfig = this._getDefaultConfig()
6482

65-
var config = {}
66-
67-
config = defaults(config, parameterConfig, envVarConfig)
83+
var config = defaults({}, parameterConfig, systemConfig, envVarConfig)
6884

6985
var configFilePath = parameterConfig.configPath || envVarConfig.configPath || defaultConfig.configPath
7086

lib/utils/configReader.spec.js

+48-31
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ describe('Config Reader module', function () {
1111
expect(configReader).to.exist
1212
})
1313

14-
it('does not override reporter if it is given', function () {
15-
var config = { serviceName: 'test', reporter: 'dummy', apiKey: 'api-key' }
16-
17-
var configReader = ConfigReader.create(config)
18-
19-
configReader.getConfig()
20-
21-
expect(config.reporter).to.eql('dummy')
22-
})
23-
2414
it('default config should be checked', function () {
2515
var configReader = ConfigReader.create({
2616
serviceName: 'test',
@@ -75,18 +65,34 @@ describe('Config Reader module', function () {
7565
expect(config.test).to.eql('env')
7666
})
7767

78-
it('file config should override default config', function () {
68+
it('system configuration should be checked', function () {
7969
var configReader = ConfigReader.create({
8070
serviceName: 'test',
8171
reporter: 'dummy',
8272
collectorApiUrl: 'http://c.a.b',
8373
apiKey: 'api-key'
8474
})
8575

86-
this.sandbox.stub(configReader, '_getEnvVarConfig', function () {
87-
return { }
76+
var getSystemConfigStub = this.sandbox.stub(configReader, '_getSystemConfig', function () {
77+
return { test: 'system' }
8878
})
8979

80+
var config = configReader.getConfig()
81+
82+
expect(getSystemConfigStub).to.have.been.calledOnce
83+
expect(config.test).to.eql('system')
84+
})
85+
86+
it('file config should override default config', function () {
87+
var configReader = ConfigReader.create({
88+
serviceName: 'test',
89+
reporter: 'dummy',
90+
collectorApiUrl: 'http://c.a.b',
91+
apiKey: 'api-key'
92+
})
93+
94+
this.sandbox.stub(configReader, '_getEnvVarConfig').returns({})
95+
9096
this.sandbox.stub(configReader, '_getFileConfig', function () {
9197
return { test: 'file' }
9298
})
@@ -116,21 +122,37 @@ describe('Config Reader module', function () {
116122
expect(config.test).to.eql('env')
117123
})
118124

119-
it('parameter config should override environment variable config', function () {
125+
it('system config should override all env var config', function () {
126+
var configReader = ConfigReader.create({ serviceName: 'test', reporter: 'dummy', apiKey: 'api-key' })
127+
128+
this.sandbox.stub(configReader, '_getSystemConfig', function () {
129+
return { test: 'system' }
130+
})
131+
132+
this.sandbox.stub(configReader, '_getEnvVarConfig', function () {
133+
return { test: 'env' }
134+
})
135+
136+
this.sandbox.stub(configReader, '_getFileConfig').returns({})
137+
138+
var config = configReader.getConfig()
139+
140+
expect(config.test).to.eql('system')
141+
})
142+
143+
it('parameter config should override system config', function () {
120144
var configReader = ConfigReader.create({
121145
serviceName: 'test',
122146
reporter: 'dummy',
123147
test: 'param',
124148
apiKey: 'api-key'
125149
})
126150

127-
this.sandbox.stub(configReader, '_getEnvVarConfig', function () {
128-
return { test: 'env' }
151+
this.sandbox.stub(configReader, '_getSystemConfig', function () {
152+
return { test: 'system' }
129153
})
130154

131-
this.sandbox.stub(configReader, '_getFileConfig', function () {
132-
return { }
133-
})
155+
this.sandbox.stub(configReader, '_getFileConfig').returns({})
134156

135157
var config = configReader.getConfig()
136158

@@ -149,9 +171,7 @@ describe('Config Reader module', function () {
149171
return { configPath: 'default' }
150172
})
151173

152-
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig', function () {
153-
return { }
154-
})
174+
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig').returns({})
155175

156176
configReader.getConfig()
157177

@@ -170,9 +190,7 @@ describe('Config Reader module', function () {
170190
return { configPath: 'env' }
171191
})
172192

173-
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig', function () {
174-
return { }
175-
})
193+
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig').returns({})
176194

177195
configReader.getConfig()
178196

@@ -188,9 +206,7 @@ describe('Config Reader module', function () {
188206
apiKey: 'api-key'
189207
})
190208

191-
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig', function () {
192-
return { }
193-
})
209+
var getFileConfigStub = this.sandbox.stub(configReader, '_getFileConfig').returns({})
194210

195211
configReader.getConfig()
196212

@@ -236,15 +252,16 @@ describe('Config Reader module', function () {
236252
apiKey: 'api-key'
237253
})
238254
var log = this.sandbox.spy(console, 'error')
239-
var ignoreHeadersOption = {
255+
var wellformed = {
240256
'user-agent': ['007']
241257
}
258+
242259
this.sandbox.stub(configReader, '_readConfigFile', function () {
243-
return { ignoreHeaders: ignoreHeadersOption }
260+
return { ignoreHeaders: wellformed }
244261
})
245262
try {
246263
var config = configReader.getConfig()
247-
expect(config.ignoreHeaders).to.eql(ignoreHeadersOption)
264+
expect(config.ignoreHeaders).to.eql(wellformed)
248265
expect(log).to.have.been.calledOnce
249266
delete process.env.TRACE_IGNORE_HEADERS
250267
} catch (err) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"debug": "2.2.0",
4949
"event-loop-stats": "1.0.0",
5050
"gc-stats": "1.0.0",
51+
"lodash.assign": "4.0.3",
5152
"lodash.defaults": "4.0.1",
5253
"lodash.isnumber": "3.0.3",
5354
"microtime": "2.0.0",

0 commit comments

Comments
 (0)