Skip to content

Commit 3759fa4

Browse files
committed
feat(proxy): adding https proxy
1 parent fb57943 commit 3759fa4

File tree

5 files changed

+88
-48
lines changed

5 files changed

+88
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ module.exports = {
6666
],
6767
disableInstrumentations: [
6868
'mongodb'
69-
]
69+
],
70+
proxy: 'http://168.63.76.32:3128'
7071
}
7172
```
7273

lib/agent/api/index.js

Lines changed: 11 additions & 2 deletions
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 assign = require('lodash.assign')
8+
var HttpsProxyAgent = require('https-proxy-agent')
89

910
var bl = require('bl')
1011
var libPackage = require('../../../package')
@@ -28,6 +29,10 @@ function CollectorApi (options) {
2829
this.serviceName = options.serviceName
2930
this.baseRetryInterval = 1000 * 60 * 30 // 30 minutes
3031
this.serviceKey = null
32+
33+
if (options.proxy) {
34+
this.proxyAgent = new HttpsProxyAgent(options.proxy)
35+
}
3136
}
3237

3338
// USE THIS WITH CAUTION, IT WILL BE BLOCKING
@@ -62,19 +67,23 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback) {
6267

6368
callback = callback || function () {}
6469

65-
var req = https.request({
70+
var requestOptions = {
6671
hostname: opts.hostname,
6772
port: opts.port,
6873
path: opts.path,
6974
method: 'POST',
75+
// if the proxy is not set, it will fallback to the default agent
76+
agent: this.proxyAgent,
7077
headers: {
7178
'Authorization': 'Bearer ' + this.apiKey,
7279
'Content-Type': 'application/json',
7380
'X-Reporter-Version': libPackage.version,
7481
'X-Reporter-Language': this.collectorLanguage,
7582
'Content-Length': payload.length
7683
}
77-
}, function (res) {
84+
}
85+
86+
var req = https.request(requestOptions, function (res) {
7887
res.setEncoding('utf8')
7988
res.pipe(bl(function (err, result) {
8089
if (err) {

lib/agent/api/index.spec.js

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var util = require('util')
22
var url = require('url')
3+
var https = require('https')
34

45
var CollectorApi = require('./')
56

@@ -9,54 +10,81 @@ var libPackage = require('../../../package')
910
var assign = require('lodash.assign')
1011

1112
describe('The Trace CollectorApi module', function () {
12-
var defaultConfig = {
13-
serviceName: 'testName',
14-
apiKey: 'testApiKey',
15-
collectorLanguage: 'nodejs',
16-
collectorApiUrl: 'https://collector.api.mock',
17-
collectorApiSampleEndpoint: '/service/sample',
18-
collectorApiServiceEndpoint: '/service',
19-
collectorApiApmMetricsEndpoint: '/service/%s/apm-metrics',
20-
collectorApiRpmMetricsEndpoint: '/service/%s/rpm-metrics',
21-
collectorApiEdgeMetricsEndpoint: '/service/%s/edge-metrics',
22-
collectorApiIncomingEdgeMetricsEndpoint: '/service/%s/edge-incoming',
23-
collectorApiExternalEdgeMetricsEndpoint: '/service/%s/edge-external',
24-
collectorApiHealthcheckEndpoint: '/service/%s/healthcheck',
25-
collectorApiProfilerMemoryHeapdumpEndpoint: '/service/%s/memory-heapdump',
26-
collectorApiProfilerCpuProfileEndpoint: '/service/%s/cpu-profile',
27-
collectorApiControlEndpoint: '/service/%s/control',
28-
collectorApiCustomMetrics: '/service/%s/custom-metrics',
29-
system: {
30-
hostname: 'test.org',
31-
processVersion: '4.3.1',
32-
processName: 'node',
33-
processId: 7777,
34-
osArch: 'x86',
35-
osPlatform: 'darwin',
36-
osRelease: '11',
37-
cpus: [
38-
{
39-
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
40-
speed: 2700
41-
},
42-
{
43-
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
44-
speed: 2700
45-
},
46-
{
47-
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
48-
speed: 2700
49-
},
50-
{ model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
51-
speed: 2700
52-
}
53-
]
13+
var defaultConfig
14+
beforeEach(function () {
15+
defaultConfig = {
16+
serviceName: 'testName',
17+
apiKey: 'testApiKey',
18+
collectorLanguage: 'nodejs',
19+
collectorApiUrl: 'https://collector.api.mock',
20+
collectorApiSampleEndpoint: '/service/sample',
21+
collectorApiServiceEndpoint: '/service',
22+
collectorApiApmMetricsEndpoint: '/service/%s/apm-metrics',
23+
collectorApiRpmMetricsEndpoint: '/service/%s/rpm-metrics',
24+
collectorApiEdgeMetricsEndpoint: '/service/%s/edge-metrics',
25+
collectorApiIncomingEdgeMetricsEndpoint: '/service/%s/edge-incoming',
26+
collectorApiExternalEdgeMetricsEndpoint: '/service/%s/edge-external',
27+
collectorApiHealthcheckEndpoint: '/service/%s/healthcheck',
28+
collectorApiProfilerMemoryHeapdumpEndpoint: '/service/%s/memory-heapdump',
29+
collectorApiProfilerCpuProfileEndpoint: '/service/%s/cpu-profile',
30+
collectorApiControlEndpoint: '/service/%s/control',
31+
collectorApiCustomMetrics: '/service/%s/custom-metrics',
32+
system: {
33+
hostname: 'test.org',
34+
processVersion: '4.3.1',
35+
processName: 'node',
36+
processId: 7777,
37+
osArch: 'x86',
38+
osPlatform: 'darwin',
39+
osRelease: '11',
40+
cpus: [
41+
{
42+
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
43+
speed: 2700
44+
},
45+
{
46+
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
47+
speed: 2700
48+
},
49+
{
50+
model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
51+
speed: 2700
52+
},
53+
{ model: 'Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz',
54+
speed: 2700
55+
}
56+
]
57+
}
5458
}
55-
}
59+
})
5660

5761
it('can be instantiated w/ serviceName and apiKey', function () {
62+
var httpsRequestSpy = this.sandbox.spy(https, 'request')
63+
defaultConfig.proxy = 'http://127.0.0.1'
64+
var collectorApi = CollectorApi.create(defaultConfig)
65+
66+
var serviceKey = 12
67+
68+
var data = {
69+
trace: 'very data',
70+
hostname: 'test.org',
71+
pid: 7777
72+
}
73+
74+
var path = util.format(defaultConfig.collectorApiRpmMetricsEndpoint, serviceKey)
75+
var sendUrl = url.resolve(defaultConfig.collectorApiUrl, path)
76+
77+
var collectorApi = CollectorApi.create(defaultConfig)
78+
collectorApi.serviceKey = serviceKey
79+
80+
collectorApi.sendRpmMetrics(data)
81+
82+
expect(httpsRequestSpy.firstCall.args[0].agent).to.eql(collectorApi.proxyAgent)
83+
expect(collectorApi.proxyAgent).to.be.ok
84+
})
85+
86+
it('can use a proxy', function () {
5887
var collectorApi = CollectorApi.create(defaultConfig)
59-
expect(collectorApi).to.be.ok
6088
})
6189

6290
it('sends rpm metrics to the collector server', function () {

lib/utils/configReader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ ConfigReader.prototype._getEnvVarConfig = function () {
6262
disableStackTrace: process.env.TRACE_DISABLE_STACK_TRACE == true, // eslint-disable-line
6363
disableInstrumentations: process.env.TRACE_DISABLE_INSTRUMENTATIONS
6464
? process.env.TRACE_DISABLE_INSTRUMENTATIONS.split(',')
65-
: []
65+
: [],
66+
proxy: process.env.TRACE_PROXY
6667
}
6768

6869
var ignoreHeaders

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"bl": "1.1.2",
4545
"continuation-local-storage": "3.1.7",
4646
"debug": "2.2.0",
47+
"https-proxy-agent": "1.0.0",
4748
"lodash.assign": "4.0.9",
4849
"lodash.defaults": "4.0.1",
4950
"lodash.flatmap": "4.3.0",

0 commit comments

Comments
 (0)