@@ -19,11 +19,14 @@ import { ExportResultCode } from '@opentelemetry/core';
1919import {
2020 CollectorExporterNodeConfigBase ,
2121 collectorTypes ,
22+ CompressionAlgorithm ,
2223} from '@opentelemetry/exporter-collector' ;
2324import { ReadableSpan } from '@opentelemetry/tracing' ;
2425import * as assert from 'assert' ;
2526import * as http from 'http' ;
2627import * as sinon from 'sinon' ;
28+ import { Stream } from 'stream' ;
29+ import * as zlib from 'zlib' ;
2730import { CollectorTraceExporter } from '../src' ;
2831import { getExportRequestProto } from '../src/util' ;
2932import {
@@ -34,9 +37,9 @@ import {
3437} from './helper' ;
3538
3639const fakeRequest = {
37- end : function ( ) { } ,
38- on : function ( ) { } ,
39- write : function ( ) { } ,
40+ end : function ( ) { } ,
41+ on : function ( ) { } ,
42+ write : function ( ) { } ,
4043} ;
4144
4245describe ( 'CollectorTraceExporter - node with proto over http' , ( ) => {
@@ -104,7 +107,7 @@ describe('CollectorTraceExporter - node with proto over http', () => {
104107 } ) ;
105108
106109 it ( 'should open the connection' , done => {
107- collectorExporter . export ( spans , ( ) => { } ) ;
110+ collectorExporter . export ( spans , ( ) => { } ) ;
108111
109112 sinon . stub ( http , 'request' ) . callsFake ( ( options : any ) => {
110113 assert . strictEqual ( options . hostname , 'foo.bar.com' ) ;
@@ -116,7 +119,7 @@ describe('CollectorTraceExporter - node with proto over http', () => {
116119 } ) ;
117120
118121 it ( 'should set custom headers' , done => {
119- collectorExporter . export ( spans , ( ) => { } ) ;
122+ collectorExporter . export ( spans , ( ) => { } ) ;
120123
121124 sinon . stub ( http , 'request' ) . callsFake ( ( options : any ) => {
122125 assert . strictEqual ( options . headers [ 'foo' ] , 'bar' ) ;
@@ -126,7 +129,7 @@ describe('CollectorTraceExporter - node with proto over http', () => {
126129 } ) ;
127130
128131 it ( 'should have keep alive and keepAliveMsecs option set' , done => {
129- collectorExporter . export ( spans , ( ) => { } ) ;
132+ collectorExporter . export ( spans , ( ) => { } ) ;
130133
131134 sinon . stub ( http , 'request' ) . callsFake ( ( options : any ) => {
132135 assert . strictEqual ( options . agent . keepAlive , true ) ;
@@ -137,27 +140,31 @@ describe('CollectorTraceExporter - node with proto over http', () => {
137140 } ) ;
138141
139142 it ( 'should successfully send the spans' , done => {
140- collectorExporter . export ( spans , ( ) => { } ) ;
141-
142- sinon . stub ( http , 'request' ) . returns ( {
143- end : ( ) => { } ,
144- on : ( ) => { } ,
145- write : ( ...args : any [ ] ) => {
146- const ExportTraceServiceRequestProto = getExportRequestProto ( ) ;
147- const data = ExportTraceServiceRequestProto ?. decode ( args [ 0 ] ) ;
148- const json = data ?. toJSON ( ) as collectorTypes . opentelemetryProto . collector . trace . v1 . ExportTraceServiceRequest ;
149- const span1 =
150- json . resourceSpans [ 0 ] . instrumentationLibrarySpans [ 0 ] . spans [ 0 ] ;
151- assert . ok ( typeof span1 !== 'undefined' , "span doesn't exist" ) ;
152- if ( span1 ) {
153- ensureProtoSpanIsCorrect ( span1 ) ;
154- }
155-
156- ensureExportTraceServiceRequestIsSet ( json ) ;
157-
158- done ( ) ;
159- } ,
160- } as any ) ;
143+ const fakeRequest = new Stream . PassThrough ( ) ;
144+ sinon . stub ( http , 'request' ) . returns ( fakeRequest as any ) ;
145+
146+ let buff = Buffer . from ( '' ) ;
147+ fakeRequest . on ( 'end' , ( ) => {
148+ const ExportTraceServiceRequestProto = getExportRequestProto ( ) ;
149+ const data = ExportTraceServiceRequestProto ?. decode ( buff ) ;
150+ const json = data ?. toJSON ( ) as collectorTypes . opentelemetryProto . collector . trace . v1 . ExportTraceServiceRequest ;
151+ const span1 =
152+ json . resourceSpans [ 0 ] . instrumentationLibrarySpans [ 0 ] . spans [ 0 ] ;
153+ assert . ok ( typeof span1 !== 'undefined' , "span doesn't exist" ) ;
154+ if ( span1 ) {
155+ ensureProtoSpanIsCorrect ( span1 ) ;
156+ }
157+
158+ ensureExportTraceServiceRequestIsSet ( json ) ;
159+
160+ done ( ) ;
161+ } ) ;
162+
163+ fakeRequest . on ( 'data' , chunk => {
164+ buff = Buffer . concat ( [ buff , chunk ] ) ;
165+ } ) ;
166+
167+ collectorExporter . export ( spans , ( ) => { } ) ;
161168 } ) ;
162169
163170 it ( 'should log the successful message' , done => {
@@ -195,4 +202,57 @@ describe('CollectorTraceExporter - node with proto over http', () => {
195202 } ) ;
196203 } ) ;
197204 } ) ;
205+ describe ( 'export - with compression' , ( ) => {
206+ beforeEach ( ( ) => {
207+ collectorExporterConfig = {
208+ headers : {
209+ foo : 'bar' ,
210+ } ,
211+ hostname : 'foo' ,
212+ attributes : { } ,
213+ url : 'http://foo.bar.com' ,
214+ keepAlive : true ,
215+ compression : CompressionAlgorithm . GZIP ,
216+ httpAgentOptions : { keepAliveMsecs : 2000 } ,
217+ } ;
218+ collectorExporter = new CollectorTraceExporter ( collectorExporterConfig ) ;
219+ spans = [ ] ;
220+ spans . push ( Object . assign ( { } , mockedReadableSpan ) ) ;
221+ } ) ;
222+ afterEach ( ( ) => {
223+ sinon . restore ( ) ;
224+ } ) ;
225+
226+ it ( 'should successfully send the spans' , done => {
227+ const fakeRequest = new Stream . PassThrough ( ) ;
228+ sinon . stub ( http , 'request' ) . returns ( fakeRequest as any ) ;
229+ const spySetHeader = sinon . spy ( ) ;
230+ ( fakeRequest as any ) . setHeader = spySetHeader ;
231+
232+ let buff = Buffer . from ( '' ) ;
233+ fakeRequest . on ( 'end' , ( ) => {
234+ const unzippedBuff = zlib . gunzipSync ( buff ) ;
235+ const ExportTraceServiceRequestProto = getExportRequestProto ( ) ;
236+ const data = ExportTraceServiceRequestProto ?. decode ( unzippedBuff ) ;
237+ const json = data ?. toJSON ( ) as collectorTypes . opentelemetryProto . collector . trace . v1 . ExportTraceServiceRequest ;
238+ const span1 =
239+ json . resourceSpans [ 0 ] . instrumentationLibrarySpans [ 0 ] . spans [ 0 ] ;
240+ assert . ok ( typeof span1 !== 'undefined' , "span doesn't exist" ) ;
241+ if ( span1 ) {
242+ ensureProtoSpanIsCorrect ( span1 ) ;
243+ }
244+
245+ ensureExportTraceServiceRequestIsSet ( json ) ;
246+ assert . ok ( spySetHeader . calledWith ( 'Content-Encoding' , 'gzip' ) ) ;
247+
248+ done ( ) ;
249+ } ) ;
250+
251+ fakeRequest . on ( 'data' , chunk => {
252+ buff = Buffer . concat ( [ buff , chunk ] ) ;
253+ } ) ;
254+
255+ collectorExporter . export ( spans , ( ) => { } ) ;
256+ } ) ;
257+ } ) ;
198258} ) ;
0 commit comments