Skip to content

Commit 6a17592

Browse files
committed
feat(api): gzip encoding
1 parent 0581a1f commit 6a17592

13 files changed

Lines changed: 110 additions & 48 deletions

File tree

lib/agent/api/bufferStream.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var stream = require('stream')
2+
var util = require('util')
3+
4+
function BufferStream (source) {
5+
stream.Readable.call(this)
6+
this._source = source
7+
this._offset = 0
8+
this._length = source.length
9+
this.on('end', this._destroy)
10+
}
11+
12+
util.inherits(BufferStream, stream.Readable)
13+
14+
BufferStream.prototype._destroy = function () {
15+
this._source = null
16+
}
17+
18+
BufferStream.prototype._read = function (size) {
19+
if (this._offset < this._length) {
20+
this.push(this._source.slice(this._offset, (this._offset + size)))
21+
this._offset += size
22+
}
23+
if (this._offset >= this._length) {
24+
this.push(null)
25+
}
26+
}
27+
28+
module.exports = BufferStream

lib/agent/api/index.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ 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')
10+
var BufferStream = require('./bufferStream')
1011

1112
var bl = require('bl')
1213
var libPackage = require('../../../package')
14+
var zlib = require('zlib')
1315

1416
function CollectorApi (options) {
1517
this.COLLECTOR_API_SERVICE = url.resolve(options.collectorApiUrl, options.collectorApiServiceEndpoint)
@@ -66,22 +68,34 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback, options
6668
var opts = url.parse(destinationUrl)
6769
var payload = stringify(data)
6870

71+
options = options || { }
72+
6973
callback = callback || function () {}
7074

75+
var headers = {
76+
'Authorization': 'Bearer ' + this.apiKey,
77+
'Content-Type': 'application/json',
78+
'X-Reporter-Version': libPackage.version,
79+
'X-Reporter-Language': this.collectorLanguage
80+
}
81+
82+
assign(headers, options.headers)
83+
84+
if (options.compress) {
85+
headers['Content-Encoding'] = 'gzip'
86+
// headers['Content-Type'] = 'application/octet-stream'
87+
} else {
88+
headers['Content-Length'] = Buffer.byteLength(payload)
89+
}
90+
7191
var requestOptions = {
7292
hostname: opts.hostname,
7393
port: opts.port,
7494
path: opts.path,
7595
method: 'POST',
7696
// if the proxy is not set, it will fallback to the default agent
7797
agent: this.proxyAgent,
78-
headers: assign({
79-
'Authorization': 'Bearer ' + this.apiKey,
80-
'Content-Type': 'application/json',
81-
'X-Reporter-Version': libPackage.version,
82-
'X-Reporter-Language': this.collectorLanguage,
83-
'Content-Length': Buffer.byteLength(payload)
84-
}, options && options.headers)
98+
headers: headers
8599
}
86100

87101
var req = https.request(requestOptions, function (res) {
@@ -102,8 +116,14 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback, options
102116
debug('#_send', '[Error] Connection error', error)
103117
callback(error)
104118
})
105-
req.write(payload)
106-
req.end()
119+
120+
if (options.compress) {
121+
var stream = new BufferStream(new Buffer(payload))
122+
stream.pipe(zlib.createGzip()).pipe(req)
123+
} else {
124+
req.write(payload)
125+
req.end()
126+
}
107127
}
108128

109129
CollectorApi.prototype.sendRpmMetrics = function (data) {
@@ -281,8 +301,6 @@ CollectorApi.prototype.getService = function (cb) {
281301
}
282302
if (res.statusCode > 399) {
283303
debug('#getService', '[Error] Service responded with', res.statusCode)
284-
console.log(setTimeout)
285-
console.log(retryInterval)
286304
return setTimeout(function () {
287305
debug('#getService', 'Retrying with %d ms', retryInterval)
288306
console.log('adsfsadf')

lib/agent/control/control.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var debug = require('debug')('risingstack/trace')
1+
var debug = require('debug')('risingstack/trace:agent:control')
22

33
var inherits = require('util').inherits
44
var Agent = require('../agent')
@@ -24,7 +24,7 @@ Control.prototype.getUpdates = function () {
2424
latestCommandId: _this.latestCommandId
2525
}, function (err, result) {
2626
if (err) {
27-
return debug(err)
27+
return debug('#getUpdates', '[Error]', err)
2828
}
2929

3030
_this.latestCommandId = result.latestCommandId

lib/agent/tracer/collector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ Collector.prototype.end = function (briefcase, options) {
412412
}
413413

414414
Collector.prototype.getTransactionId = function (briefcase) {
415-
return briefcase.communication && briefcase.communication.transactionId
415+
return briefcase && briefcase.communication && briefcase.communication.transactionId
416416
}
417417

418418
module.exports = Collector

lib/agent/tracer/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ Tracer.prototype.send = function (isSync) {
4141
s: { n: this.serviceName, k: this.serviceKey },
4242
e: events
4343
}
44+
data
4445
if (isSync === true) {
4546
this._api._sendSync(this.sampleUrl, data, {
47+
compress: true,
4648
headers: {
4749
'x-document-schema': 'CollectorTransactionRaw:1.0.0'
4850
}
4951
})
5052
} else {
5153
this._api._send(this.sampleUrl, data, null, {
54+
compress: true,
5255
headers: {
5356
'x-document-schema': 'CollectorTransactionRaw:1.0.0'
5457
}

lib/instrumentations/core/http/request.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var debug = require('debug')('risingstack/trace')
1+
var debug = require('debug')('risingstack/trace:agent:instrumentations')
22
var url = require('url')
33
var microtime = require('../../../optionalDependencies/microtime')
44

55
var util = require('./util')
6+
var format = require('util').format
67

7-
function wrapRequest (originalHttpRequest, agent) {
8+
function httpClient (originalHttpRequest, agent) {
89
var whiteListHosts = agent.getConfig().whiteListHosts
910

1011
return function wrappedRequest (requestParams) {
@@ -41,22 +42,16 @@ function wrapRequest (originalHttpRequest, agent) {
4142

4243
var csCtx = cs.briefcase.csCtx
4344

44-
debug('trace event (cs); reqId: %s, child-id: %s',
45-
csCtx.transactionId,
46-
csCtx.communicationId
47-
)
48-
4945
// decorate headers
5046
requestParams.headers = requestParams.headers || {}
5147

5248
if (cs.duffelBag.transactionId) {
5349
requestParams.headers['request-id'] = cs.duffelBag.transactionId
5450
}
5551

56-
if (collector.LEVELS.gte(cs.duffelBag.severity, collector.mustCollectSeverity)) {
57-
debug('trace event (cs); reqId: %s, child commId: %s must collect',
58-
csCtx.transactionId,
59-
csCtx.communicationId)
52+
var mustCollect = collector.LEVELS.gte(cs.duffelBag.severity, collector.mustCollectSeverity)
53+
54+
if (mustCollect) {
6055
requestParams.headers['x-must-collect'] = '1'
6156
}
6257

@@ -68,13 +63,13 @@ function wrapRequest (originalHttpRequest, agent) {
6863
requestParams.headers['x-client-send'] = String(cs.duffelBag.timestamp)
6964
requestParams.headers['x-span-id'] = cs.duffelBag.communicationId
7065

66+
debug('#httpClient', format('CS(%s) [must-collect: %s]', csCtx.communicationId, mustCollect))
67+
7168
var returned = originalHttpRequest.apply(this, arguments)
7269

7370
// returns with error
7471
returned.on('error', function (err) {
75-
debug('trace event (cr) on error; reqId: %s, child commId: %s must collect',
76-
csCtx.transactionId,
77-
csCtx.communicationId)
72+
debug('#httpClient', format('CS(%s) network error: %s', csCtx.communicationId, err.code))
7873
collector.networkError(cs.briefcase, err)
7974
})
8075

@@ -95,6 +90,8 @@ function wrapRequest (originalHttpRequest, agent) {
9590
targetServiceKey: serviceKey
9691
}
9792

93+
debug('#httpClient', format('CR(%s) [severity: %s]', csCtx.communicationId, severity))
94+
9895
collector.clientRecv({
9996
protocol: 'http',
10097
status: incomingMessage.statusCode > 399 ? 'bad' : 'ok',
@@ -118,10 +115,10 @@ function wrapRequest (originalHttpRequest, agent) {
118115
}
119116
})
120117

121-
agent.storage.bindNew(returned)
118+
agent.storage.bind(returned)
122119

123120
return returned
124121
}
125122
}
126123

127-
module.exports = wrapRequest
124+
module.exports = httpClient

lib/instrumentations/core/http/request.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('The http.request wrapper module', function () {
6060
get: this.sandbox.stub().returns({
6161
communication: {}
6262
}),
63-
bindNew: this.sandbox.spy()
63+
bind: this.sandbox.spy()
6464
}
6565
}
6666
})

lib/instrumentations/core/http/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var microtime = require('../../../optionalDependencies/microtime')
2-
var debug = require('debug')('risingstack/trace')
2+
var debug = require('debug')('risingstack/trace:agent:instrumentations')
33
var Shimmer = require('../../../utils/shimmer')
44

55
var util = require('./util')
@@ -32,7 +32,7 @@ function isStatusCodeIgnored (ignoreStatusCodes, statusCode) {
3232
return false
3333
}
3434

35-
function wrapListener (listener, agent) {
35+
function httpServer (listener, agent) {
3636
var config = agent.getConfig()
3737
var collector = agent.tracer.collector
3838
var incomingEdgeMetrics = agent.incomingEdgeMetrics
@@ -50,7 +50,6 @@ function wrapListener (listener, agent) {
5050
})
5151

5252
if (isSkippedHeader || isPathIgnored(ignorePaths, requestUrl)) {
53-
debug('trace event (sr); request skipped because of ignore options', headers)
5453
return listener.apply(this, arguments)
5554
}
5655

@@ -119,6 +118,8 @@ function wrapListener (listener, agent) {
119118
if (collector.LEVELS.gte(ss.duffelBag.severity, collector.mustCollectSeverity)) {
120119
response.setHeader('x-must-collect', '1')
121120
}
121+
} else {
122+
debug('#httpServer', '[Warning] failed to create SS, cannot intrument headers')
122123
}
123124
agent.rpmMetrics.addResponseTime(ssTime - srTime)
124125
agent.rpmMetrics.addStatusCode(response.statusCode)
@@ -137,4 +138,4 @@ function wrapListener (listener, agent) {
137138
}
138139
}
139140

140-
module.exports = wrapListener
141+
module.exports = httpServer

lib/instrumentations/express.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var debug = require('debug')('risingstack/trace')
1+
var debug = require('debug')('risingstack/trace:agent:instrumentations')
22
var Shimmer = require('../utils/shimmer')
33

44
module.exports = function (express, agent) {
@@ -10,7 +10,7 @@ module.exports = function (express, agent) {
1010

1111
// for now support only express@4
1212
if (!isVersion4) {
13-
debug('express version is not supported, not wrapping error handler')
13+
debug('#express', 'express version is not supported, not wrapping error handler')
1414
return express
1515
}
1616

lib/instrumentations/pg.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var semver = require('semver')
2-
var debug = require('debug')('risingstack/trace')
2+
var debug = require('debug')('risingstack/trace:agent:instrumentations')
33
var consts = require('../consts')
44
var Shimmer = require('../utils/shimmer')
55
var utils = require('./utils')
66

77
function wrapNative (native, agent, version) {
88
// We support it only versions gte 4.0.0
99
if (!version) {
10-
debug('trace: warning: Cannot determine postgres version. ' +
10+
debug('#postgres', 'cannot determine postgres version. ' +
1111
'(No package.json?) Native queries won\'t be instrumented.'
1212
)
1313
} else if (!semver.satisfies(version, '>= 4.0.0')) {
14-
debug('trace: warning: You are using node-postgres version ' +
14+
debug('#postgres', 'you are using node-postgres version ' +
1515
version + ', (<4.0.0) so native queries won\'t be instrumented.'
1616
)
1717
} else {

0 commit comments

Comments
 (0)