1717import { types } from '@opencensus/opencensus-core' ;
1818import { classes } from '@opencensus/opencensus-core' ;
1919import { logger } from '@opencensus/opencensus-core' ;
20- import { B3Format } from '@opencensus/opencensus-propagation-b3' ;
2120import * as semver from 'semver' ;
2221import * as shimmer from 'shimmer' ;
2322import * as url from 'url' ;
@@ -45,6 +44,8 @@ export class HttpPlugin extends classes.BasePlugin {
4544 this . setPluginContext ( moduleExporters , tracer , version ) ;
4645 this . logger = tracer . logger || logger . logger ( 'debug' ) ;
4746
47+ this . logger . debug ( 'applying pacth to %s@%s' , this . moduleName , this . version ) ;
48+
4849 shimmer . wrap ( moduleExporters , 'request' , this . patchOutgoingRequest ( ) ) ;
4950
5051 // In Node 8, http.get calls a private request method, therefore we patch it
@@ -64,7 +65,9 @@ export class HttpPlugin extends classes.BasePlugin {
6465 /** Unpatches all HTTP patched function. */
6566 applyUnpatch ( ) : void {
6667 shimmer . unwrap ( this . moduleExporters , 'request' ) ;
67- shimmer . unwrap ( this . moduleExporters , 'get' ) ;
68+ if ( semver . satisfies ( this . version , '>=8.0.0' ) ) {
69+ shimmer . unwrap ( this . moduleExporters , 'get' ) ;
70+ }
6871 shimmer . unwrap (
6972 this . moduleExporters && this . moduleExporters . Server &&
7073 this . moduleExporters . Server . prototype ,
@@ -86,10 +89,18 @@ export class HttpPlugin extends classes.BasePlugin {
8689 return original . apply ( this , arguments ) ;
8790 }
8891
92+ plugin . logger . debug ( '%s plugin incomingRequest' , plugin . moduleName ) ;
93+ const propagation = plugin . tracer . propagation ;
94+ const headers = arguments [ 1 ] . headers ;
95+ const getter = {
96+ getHeader ( name : string ) {
97+ return headers [ name ] ;
98+ }
99+ } as types . HeaderGetter ;
89100 const traceOptions = {
90101 name : arguments [ 1 ] . url ,
91102 type : 'SERVER' ,
92- spanContext : B3Format . extractFromHeader ( arguments [ 1 ] . headers )
103+ spanContext : propagation ? propagation . extract ( getter ) : null
93104 } ;
94105
95106 return plugin . tracer . startRootSpan ( traceOptions , rootSpan => {
@@ -151,27 +162,38 @@ export class HttpPlugin extends classes.BasePlugin {
151162 if ( arguments [ 0 ] . headers &&
152163 arguments [ 0 ] . headers [ 'x-opencensus-outgoing-request' ] ) {
153164 // tslint:disable:no-any
154- return original . apply ( this as any , arguments ) ;
165+ plugin . logger . debug (
166+ 'header with "x-opencensus-outgoing-request" - do not trace' ) ;
167+ return original . apply ( this , arguments ) ;
155168 }
156169
170+ const request = original . apply ( this , arguments ) ;
171+
172+ plugin . tracer . wrapEmitter ( request ) ;
173+
174+ plugin . logger . debug ( '%s plugin outgoingRequest' , plugin . moduleName ) ;
157175 const traceOptions = {
158- name : arguments [ 0 ] . pathname ,
176+ name : `${ request . method ? request . method : 'GET' } ${
177+ arguments [ 0 ] . pathname } `,
159178 type : 'CLIENT' ,
160179 } ;
161180
181+
162182 // Checks if this outgoing request is part of an operation by checking
163183 // if there is a current root span, if so, we create a child span. In
164184 // case there is no root span, this means that the outgoing request is
165185 // the first operation, therefore we create a root span.
166186 if ( ! plugin . tracer . currentRootSpan ) {
187+ plugin . logger . debug ( 'outgoingRequest starting a root span' ) ;
167188 return plugin . tracer . startRootSpan (
168189 traceOptions ,
169- plugin . makeRequestTrace ( original , arguments , plugin ) ) ;
190+ plugin . makeRequestTrace ( request , arguments , plugin ) ) ;
170191 } else {
192+ plugin . logger . debug ( 'outgoingRequest starting a child span' ) ;
171193 const span : types . Span = plugin . tracer . startChildSpan (
172194 traceOptions . name , traceOptions . type ) ;
173195 return ( span : types . Span ) =>
174- plugin . makeRequestTrace ( original , arguments , plugin ) ;
196+ plugin . makeRequestTrace ( request , arguments , plugin ) ;
175197 }
176198 } ;
177199 } ;
@@ -184,19 +206,34 @@ export class HttpPlugin extends classes.BasePlugin {
184206 * @param args The arguments to the original function.
185207 */
186208 // tslint:disable:no-any
187- makeRequestTrace ( original : Function , args : any , plugin : HttpPlugin ) : any {
209+ makeRequestTrace ( request : any , args : any , plugin : HttpPlugin ) : any {
188210 return ( span : types . Span ) => {
189- args [ 0 ] . headers = B3Format . injectToHeader ( args [ 0 ] . headers , span ) ;
211+ plugin . logger . debug ( 'makeRequestTrace' ) ;
190212
191- const request = original . apply ( this , args ) ;
213+ const headers = args [ 0 ] . headers ;
214+ const setter = {
215+ setHeader ( name : string , value : string ) {
216+ headers [ name ] = value ;
217+ }
218+ } ;
219+
220+ const propagation = plugin . tracer . propagation ;
221+ if ( propagation ) {
222+ propagation . inject ( setter , span . spanContext ) ;
223+ }
192224
193225 if ( ! span ) {
226+ plugin . logger . debug ( 'makeRequestTrace span is null' ) ;
194227 return request ;
195228 }
196229
197230 // tslint:disable:no-any
198- request . on ( 'response' , ( response : any ) => {
231+ const req = request . on ( 'response' , ( response : any ) => {
232+ plugin . tracer . wrapEmitter ( response ) ;
233+ plugin . logger . debug ( 'outgoingRequest on response()' ) ;
234+
199235 response . on ( 'end' , ( ) => {
236+ plugin . logger . debug ( 'outgoingRequest on end()' ) ;
200237 span . addAttribute ( 'http.host' , args [ 0 ] . host ) ;
201238 span . addAttribute ( 'http.method' , request . method ) ;
202239 span . addAttribute ( 'http.path' , args [ 0 ] . pathname ) ;
@@ -211,11 +248,12 @@ export class HttpPlugin extends classes.BasePlugin {
211248 span . addMessageEvent (
212249 'MessageEventTypeSent' , uuid . v4 ( ) . split ( '-' ) . join ( '' ) ) ;
213250
214- // debug('Ended span: ', span.name, span.id, span.traceId);
215251 span . end ( ) ;
216252 } ) ;
217253 } ) ;
254+ plugin . tracer . wrapEmitter ( req ) ;
218255
256+ plugin . logger . debug ( 'makeRequestTrace retun request' ) ;
219257 return request ;
220258 } ;
221259 }
0 commit comments