Skip to content

Commit 1d39581

Browse files
committed
fix(startup): verbose error message
1 parent 9b7d4ec commit 1d39581

File tree

5 files changed

+22
-31
lines changed

5 files changed

+22
-31
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ module.exports = {
6161

6262
*Note: Custom reporters are no longer supported in trace 2.x*
6363

64+
*Note: If you are running your app with NODE_ENV=test, Trace won't start*
65+
6466
## API
6567

6668
### trace.report(String, [Object])

lib/agent/api/index.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ function CollectorApi (options) {
2020
this.processId = options.processId
2121
this.hostname = options.hostname
2222
this.serviceName = options.serviceName
23-
this.baseRetryInterval = 100
24-
this.retryCount = 0
25-
this.retryLimit = 13
23+
this.baseRetryInterval = 1000 * 60 * 30 // 30 minutes
2624
this.serviceKey = null
2725
}
2826

@@ -98,9 +96,8 @@ CollectorApi.prototype.sendSamples = function (data) {
9896
}
9997

10098
CollectorApi.prototype._getRetryInterval = function () {
101-
var retryInterval = Math.pow(2, this.retryCount) * this.baseRetryInterval
102-
debug('retrying with %d ms', retryInterval)
103-
return retryInterval
99+
debug('retrying with %d ms', this.baseRetryInterval)
100+
return this.baseRetryInterval
104101
}
105102

106103
CollectorApi.prototype.getService = function (cb) {
@@ -113,7 +110,6 @@ CollectorApi.prototype.getService = function (cb) {
113110
var payload = JSON.stringify({
114111
name: _this.serviceName
115112
})
116-
117113
var req = https.request({
118114
hostname: opts.hostname,
119115
port: opts.port,
@@ -132,12 +128,9 @@ CollectorApi.prototype.getService = function (cb) {
132128

133129
if (err) {
134130
debug('There was an error when connecting to the Trace API, retrying', err)
135-
if (++_this.retryCount < _this.retryLimit) {
136-
return setTimeout(function () {
137-
_this.getService()
138-
}, _this._getRetryInterval())
139-
}
140-
return debug('The trace collector-api is currently unavailable', err)
131+
return setTimeout(function () {
132+
_this.getService()
133+
}, _this._getRetryInterval())
141134
}
142135

143136
var resText = resBuffer.toString('utf8')
@@ -147,13 +140,9 @@ CollectorApi.prototype.getService = function (cb) {
147140
return console.error(format('%s trace: error: %s', new Date(), 'TRACE_API_KEY got rejected - are you sure you are using the right one?'))
148141
}
149142
if (res.statusCode > 399) {
150-
if (++_this.retryCount < _this.retryLimit) {
151-
return setTimeout(function () {
152-
_this.getService(cb)
153-
}, _this._getRetryInterval())
154-
}
155-
156-
return cb(new Error('The trace collector-api is currently unavailable'))
143+
return setTimeout(function () {
144+
_this.getService(cb)
145+
}, _this._getRetryInterval())
157146
}
158147

159148
try {

lib/agent/api/index.spec.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ describe('The Trace CollectorApi module', function () {
9999

100100
it('retries on 409 error', function (done) {
101101
var collectorApi = CollectorApi.create(defaultConfig)
102+
var _getRetryIntervalSpy = this.sandbox.spy(collectorApi, '_getRetryInterval')
102103

103104
var data = {
104105
name: defaultConfig.serviceName
105106
}
106107

107-
collectorApi.retryLimit = 2
108-
collectorApi.baseRetryInterval = 0
109-
110108
nock(defaultConfig.collectorApiUrl, {
111109
reqheaders: {
112110
'Authorization': 'Bearer testApiKey',
@@ -115,12 +113,15 @@ describe('The Trace CollectorApi module', function () {
115113
}
116114
})
117115
.post(defaultConfig.collectorApiServiceEndpoint, JSON.stringify(data))
118-
.times(collectorApi.retryLimit)
116+
.times(2)
119117
.reply(409, {})
120118

121-
collectorApi.getService(function (err) {
122-
expect(err.message).to.eql('The trace collector-api is currently unavailable')
119+
collectorApi.getService()
120+
121+
// FIXME: this is not nice, who can I test it better?
122+
setTimeout(function () {
123+
expect(_getRetryIntervalSpy).to.be.called
123124
done()
124-
})
125+
}, 100)
125126
})
126127
})

lib/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var Agent = require('./agent')
22
var Instrumentation = require('./instrumentations')
33

4-
var format = require('util').format
54
var debug = require('debug')('risingstack/trace')
65

76
var ConfigReader = require('./utils/configReader')
@@ -22,7 +21,7 @@ function Trace () {
2221
try {
2322
this.config = this.configReader.getConfig()
2423
} catch (ex) {
25-
console.error(format('%s trace: error: %s', new Date(), ex.message))
24+
console.error('trace: error', ex.message)
2625
return traceNoop
2726
}
2827

lib/utils/configReader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ ConfigReader.prototype.getConfig = function () {
9595
config.whiteListHosts = [url.parse(config.collectorApiUrl).host]
9696

9797
if (!config.apiKey) {
98-
throw new Error('Missing apiKey')
98+
throw new Error('Missing apiKey, please set the TRACE_API_KEY environment variable')
9999
}
100100

101101
if (!config.serviceName) {
102-
throw new Error('Missing serviceName')
102+
throw new Error('Missing serviceName, please set the TRACE_SERVICE_NAME environment variable')
103103
}
104104

105105
return config

0 commit comments

Comments
 (0)