Skip to content

Commit 0581a1f

Browse files
committed
chore(): better debug logs
1 parent 2531a6f commit 0581a1f

15 files changed

Lines changed: 82 additions & 98 deletions

File tree

lib/agent/api/index.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var url = require('url')
33
var util = require('util')
44
var requestSync = require('sync-request')
55
var isNumber = require('lodash.isnumber')
6-
var debug = require('debug')('risingstack/trace')
6+
var debug = require('debug')('risingstack/trace:agent:api')
77
var assign = require('lodash.assign')
88
var HttpsProxyAgent = require('https-proxy-agent')
99
var stringify = require('json-stringify-safe')
@@ -38,7 +38,7 @@ function CollectorApi (options) {
3838

3939
// USE THIS WITH CAUTION, IT WILL BE BLOCKING
4040
CollectorApi.prototype._sendSync = function (destinationUrl, data, options) {
41-
debug('sending data to trace servers sync: ', stringify(data))
41+
debug('#_sendSync', 'Sending data to ' + destinationUrl)
4242
try {
4343
requestSync('POST', destinationUrl, {
4444
json: data,
@@ -51,7 +51,7 @@ CollectorApi.prototype._sendSync = function (destinationUrl, data, options) {
5151
timeout: 1000
5252
})
5353
} catch (ex) {
54-
debug('error sending data to trace servers sync: ', ex)
54+
debug('#_sendSync', '[Error] Server responded with error', ex)
5555
}
5656
}
5757

@@ -88,20 +88,18 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback, options
8888
res.setEncoding('utf8')
8989
res.pipe(bl(function (err, result) {
9090
if (err) {
91-
debug('There was an error when connecting to the Trace API', err)
91+
debug('#_send', '[Error]', err)
9292
return
9393
}
94-
9594
callback(null, result)
96-
debug('HTTP Traces sent successfully')
9795
}))
9896
})
9997

100-
debug('sending data to trace servers: ', destinationUrl, payload)
98+
debug('#_send', 'Sending data to ', destinationUrl)
10199

102100
req.on('error', function (error) {
103101
console.error('error: [trace]', 'There was an error connecting to the Trace servers when sending data. Make sure your servers can reach', opts.hostname)
104-
debug('error connecting to the Trace servers', error)
102+
debug('#_send', '[Error] Connection error', error)
105103
callback(error)
106104
})
107105
req.write(payload)
@@ -110,7 +108,7 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback, options
110108

111109
CollectorApi.prototype.sendRpmMetrics = function (data) {
112110
if (!isNumber(this.serviceKey)) {
113-
debug('Service id not present, cannot send rpm metrics')
111+
debug('#sendRpmMetrics', '[Error] Service key not present, cannot send rpm metrics')
114112
return
115113
}
116114

@@ -120,7 +118,7 @@ CollectorApi.prototype.sendRpmMetrics = function (data) {
120118

121119
CollectorApi.prototype.ping = function () {
122120
if (!isNumber(this.serviceKey)) {
123-
debug('Service id not present, cannot do healthcheck')
121+
debug('#ping', '[Error] Service key not present, cannot do healthcheck')
124122
return
125123
}
126124

@@ -130,7 +128,7 @@ CollectorApi.prototype.ping = function () {
130128

131129
CollectorApi.prototype.sendApmMetrics = function (data) {
132130
if (!isNumber(this.serviceKey)) {
133-
debug('Service id not present, cannot send metrics')
131+
debug('#sendApmMetrics', '[Error] Service key not present, cannot send metrics')
134132
return
135133
}
136134

@@ -140,7 +138,7 @@ CollectorApi.prototype.sendApmMetrics = function (data) {
140138

141139
CollectorApi.prototype.sendMemorySnapshot = function (data, callback) {
142140
if (!isNumber(this.serviceKey)) {
143-
debug('Service id not present, cannot send heapdump')
141+
debug('#sendMemorySnapshot', '[Error] Service key not present, cannot send heapdump')
144142
return callback(new Error('serviceKey is missing'))
145143
}
146144

@@ -150,7 +148,7 @@ CollectorApi.prototype.sendMemorySnapshot = function (data, callback) {
150148

151149
CollectorApi.prototype.sendCpuProfile = function (data, callback) {
152150
if (!isNumber(this.serviceKey)) {
153-
debug('Service id not present, cannot send cpu profile')
151+
debug('#sendCpuProfile', '[Error] Service key not present, cannot send cpu profile')
154152
return callback(new Error('serviceKey is missing'))
155153
}
156154

@@ -160,7 +158,7 @@ CollectorApi.prototype.sendCpuProfile = function (data, callback) {
160158

161159
CollectorApi.prototype.sendExternalEdgeMetrics = function (data) {
162160
if (!isNumber(this.serviceKey)) {
163-
debug('Service id not present, cannot send metrics')
161+
debug('#sendExternalEdgeMetrics', '[Error] Service key not present, cannot send metrics')
164162
return
165163
}
166164

@@ -170,7 +168,7 @@ CollectorApi.prototype.sendExternalEdgeMetrics = function (data) {
170168

171169
CollectorApi.prototype.sendIncomingEdgeMetrics = function (data) {
172170
if (!isNumber(this.serviceKey)) {
173-
debug('Service id not present, cannot send metrics')
171+
debug('#sendIncomingEdgeMetrics', '[Error] Service key not present, cannot send metrics')
174172
return
175173
}
176174

@@ -183,7 +181,7 @@ CollectorApi.prototype.sendIncomingEdgeMetrics = function (data) {
183181

184182
CollectorApi.prototype.getUpdates = function (data, callback) {
185183
if (!isNumber(this.serviceKey)) {
186-
debug('Service id not present, cannot get updates')
184+
debug('#getUpdates', '[Error] Service key not present, cannot get updates')
187185
return
188186
}
189187

@@ -206,7 +204,7 @@ CollectorApi.prototype.getUpdates = function (data, callback) {
206204

207205
CollectorApi.prototype.sendCustomMetrics = function (data) {
208206
if (!isNumber(this.serviceKey)) {
209-
debug('Service id not present, cannot send metrics')
207+
debug('#sendCustomMetrics', '[Error] Service key not present, cannot send metrics')
210208
return
211209
}
212210

@@ -222,14 +220,7 @@ CollectorApi.prototype.sendDependencies = function (dependencies) {
222220
this._send(url, dependencies)
223221
}
224222

225-
CollectorApi.prototype._getRetryInterval = function () {
226-
debug('retrying with %d ms', this.baseRetryInterval)
227-
return this.baseRetryInterval
228-
}
229-
230223
CollectorApi.prototype.getService = function (cb) {
231-
debug('getting service id from the trace servers')
232-
233224
var opts = url.parse(this.COLLECTOR_API_SERVICE)
234225
var _this = this
235226
cb = cb || function () {}
@@ -272,42 +263,49 @@ CollectorApi.prototype.getService = function (cb) {
272263
res.pipe(bl(function (err, resBuffer) {
273264
var response
274265

266+
var retryInterval = _this.baseRetryInterval
267+
275268
if (err) {
276-
debug('There was an error when connecting to the Trace API, retrying', err)
269+
debug('#getService', '[Error]', err)
277270
return setTimeout(function () {
271+
debug('#getService', 'Retrying with %d ms', retryInterval)
278272
_this.getService()
279-
}, _this._getRetryInterval())
273+
}, retryInterval)
280274
}
281275

282276
var resText = resBuffer.toString('utf8')
283277

284-
debug('raw response from trace servers: ', resText)
285278
if (res.statusCode === 401) {
279+
debug('#getService', '[Error] Api key rejected')
286280
return console.error('error: [trace]', 'Trace API key is rejected - are you sure you are using the right one?')
287281
}
288282
if (res.statusCode > 399) {
283+
debug('#getService', '[Error] Service responded with', res.statusCode)
284+
console.log(setTimeout)
285+
console.log(retryInterval)
289286
return setTimeout(function () {
287+
debug('#getService', 'Retrying with %d ms', retryInterval)
288+
console.log('adsfsadf')
290289
_this.getService(cb)
291-
}, _this._getRetryInterval())
290+
}, retryInterval)
292291
}
293292

294293
try {
295294
response = JSON.parse(resText)
296295
} catch (ex) {
297-
debug('Error parsing JSON:', ex)
298-
return debug(ex)
296+
return
299297
}
300298

301299
_this.serviceKey = response.key
302300
cb(null, response.key)
303301
}))
304302
})
305303

306-
debug('getting serviceKey with payload:', payload)
304+
debug('#getService', 'Sending request')
307305

308306
req.on('error', function (error) {
309307
console.error('error: [trace]', 'There was an error connecting to the Trace servers to get the service key. Make sure your servers can reach', opts.hostname)
310-
debug('error connecting to the Trace servers', error)
308+
debug('#getService', '[Error] error', error)
311309
})
312310
req.write(payload)
313311
req.end()

lib/agent/api/index.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ describe('The Trace CollectorApi module', function () {
198198

199199
it('retries on 409 error', function (done) {
200200
var collectorApi = CollectorApi.create(defaultConfig)
201-
var _getRetryIntervalSpy = this.sandbox.spy(collectorApi, '_getRetryInterval')
202201

203202
var data = {
204203
name: 'testName',
@@ -235,10 +234,12 @@ describe('The Trace CollectorApi module', function () {
235234

236235
collectorApi.getService()
237236

238-
// FIXME: this is not nice, who can I test it better?
239-
setTimeout(function () {
240-
expect(_getRetryIntervalSpy).to.be.called
237+
collectorApi.baseRetryInterval = 1
238+
239+
this.timeout(500)
240+
241+
this.sandbox.stub(collectorApi, 'getService', function () {
241242
done()
242-
}, 100)
243+
})
243244
})
244245
})

lib/agent/index.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventEmitter = require('events').EventEmitter
22

3-
var debug = require('debug')('risingstack/trace')
3+
var debug = require('debug')('risingstack/trace:agent')
44

55
var CollectorApi = require('./api')
66
var Tracer = require('./tracer')
@@ -15,56 +15,56 @@ var Storage = require('./storage')
1515
var controlBus = new EventEmitter()
1616

1717
function Agent (options) {
18-
debug('Agent is initializing...')
19-
this._collectorApi = CollectorApi.create(options.config)
18+
debug('#[constructor]', 'Initializing api client...')
19+
this._api = CollectorApi.create(options.config)
2020
// config
2121
this.config = options.config
2222

2323
this.apmMetrics = Metrics.apm.create({
24-
collectorApi: this._collectorApi,
24+
collectorApi: this._api,
2525
config: this.config
2626
})
2727

2828
this.healthcheck = Healthcheck.create({
29-
collectorApi: this._collectorApi,
29+
collectorApi: this._api,
3030
config: this.config
3131
})
3232

3333
this.rpmMetrics = Metrics.rpm.create({
34-
collectorApi: this._collectorApi,
34+
collectorApi: this._api,
3535
config: this.config
3636
})
3737

3838
this.externalEdgeMetrics = Metrics.externalEdge.create({
39-
collectorApi: this._collectorApi,
39+
collectorApi: this._api,
4040
config: this.config
4141
})
4242

4343
this.incomingEdgeMetrics = Metrics.incomingEdge.create({
44-
collectorApi: this._collectorApi,
44+
collectorApi: this._api,
4545
config: this.config
4646
})
4747

4848
this.memoryProfiler = Profiler.memory.create({
49-
collectorApi: this._collectorApi,
49+
collectorApi: this._api,
5050
config: this.config,
5151
controlBus: controlBus
5252
})
5353

5454
this.cpuProfiler = Profiler.cpu.create({
55-
collectorApi: this._collectorApi,
55+
collectorApi: this._api,
5656
config: this.config,
5757
controlBus: controlBus
5858
})
5959

6060
this.control = Control.create({
61-
collectorApi: this._collectorApi,
61+
collectorApi: this._api,
6262
config: this.config,
6363
controlBus: controlBus
6464
})
6565

6666
this.customMetrics = Metrics.customMetrics.create({
67-
collectorApi: this._collectorApi,
67+
collectorApi: this._api,
6868
config: this.config
6969
})
7070

@@ -74,7 +74,7 @@ function Agent (options) {
7474
})
7575

7676
this.tracer = Tracer.create({
77-
collectorApi: this._collectorApi,
77+
collectorApi: this._api,
7878
config: this.config
7979
})
8080

@@ -96,35 +96,26 @@ function Agent (options) {
9696

9797
Agent.prototype.start = function () {
9898
var self = this
99-
this._collectorApi.getService(function (err, serviceKey) {
99+
this._api.getService(function (err, serviceKey) {
100100
if (err) {
101101
return debug(err.message)
102102
}
103-
debug('Agent serviceKey is set to: ', serviceKey)
103+
debug('#start', 'ServiceKey is set to: ', serviceKey)
104104
self.serviceKey = serviceKey
105+
debug('#start', 'Starting agents...')
105106
self.agents.forEach(function (agent) {
106-
debug(agent.name + ' initialized')
107107
agent.initialize({ serviceKey: serviceKey })
108+
agent.start()
109+
debug('#start', agent.name + ' started')
108110
})
109-
self._startAll()
110111
})
111112
}
112113

113114
Agent.prototype.stop = function () {
114-
this._stopAll()
115-
}
116-
117-
Agent.prototype._startAll = function () {
118-
this.agents.forEach(function (agent) {
119-
debug(agent.name + ' started')
120-
agent.start()
121-
})
122-
}
123-
124-
Agent.prototype._stopAll = function () {
115+
debug('#stop', 'Stopping agents...')
125116
this.agents.forEach(function (agent) {
126-
debug(agent.name + ' stopped')
127117
agent.stop()
118+
debug('#stop', agent.name + ' stopped')
128119
})
129120
}
130121

lib/agent/metrics/externalEdge/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
var debug = require('debug')('risingstack/trace')
1+
var debug = require('debug')('risingstack/trace:agent:metrics:externalEdge')
22
var flatMap = require('lodash.flatmap')
3-
var consts = require('../../../consts')
43

54
var inherits = require('util').inherits
65
var Agent = require('../../agent')
@@ -42,17 +41,17 @@ ExternalEdgeMetrics.prototype.report = function (data) {
4241
// protect against bad use of the report function: responseTime is required
4342
// TODO: check all instrumentations to be sure they send a responseTime
4443
if (!data.responseTime) {
45-
debug('ExternalEdgeMetrics.report responseTime is undefined, discarding')
44+
debug('#report', '[Error] ExternalEdgeMetrics.report responseTime is undefined, discarding')
4645
return
4746
}
4847
this.initProtocol(data)
4948
var edge = this.initHost(data)
5049

5150
edge.responseTime.push(data.responseTime)
5251

53-
if (data.status === consts.EDGE_STATUS.OK) {
52+
if (data.status === this.EDGE_STATUS.OK) {
5453
edge.status.ok += 1
55-
} else if (data.status === consts.EDGE_STATUS.NOT_OK) {
54+
} else if (data.status === this.EDGE_STATUS.NOT_OK) {
5655
edge.status.notOk += 1
5756
}
5857
}

0 commit comments

Comments
 (0)