@@ -7,9 +7,11 @@ var debug = require('debug')('risingstack/trace:agent:api')
77var assign = require ( 'lodash.assign' )
88var HttpsProxyAgent = require ( 'https-proxy-agent' )
99var stringify = require ( 'json-stringify-safe' )
10+ var BufferStream = require ( './bufferStream' )
1011
1112var bl = require ( 'bl' )
1213var libPackage = require ( '../../../package' )
14+ var zlib = require ( 'zlib' )
1315
1416function 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
109129CollectorApi . 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' )
0 commit comments