@@ -24,7 +24,6 @@ import * as url from 'url';
2424import * as uuid from 'uuid' ;
2525
2626
27- // TODO: maybe we should have a setup as a Client or as Server.
2827
2928export type HttpModule = typeof httpModule ;
3029export type RequestFunction = typeof httpModule . request ;
@@ -53,15 +52,14 @@ export class HttpPlugin extends classes.BasePlugin {
5352 }
5453
5554
56- // TODO: moduleExports should use type HttpModule instead of any
5755 /**
5856 * Patches HTTP incoming and outcoming request functions.
5957 * @param moduleExports The http module exports
6058 * @param tracer A tracer instance to create spans on.
6159 * @param version The package version.
6260 */
6361 // tslint:disable-next-line:no-any
64- applyPatch ( moduleExports : any , tracer : types . Tracer , version : string ) {
62+ applyPatch ( moduleExports : HttpModule , tracer : types . Tracer , version : string ) {
6563 this . setPluginContext ( moduleExports , tracer , version ) ;
6664 this . logger = tracer . logger || logger . logger ( 'debug' ) ;
6765
@@ -80,7 +78,7 @@ export class HttpPlugin extends classes.BasePlugin {
8078 shimmer . wrap (
8179 moduleExports && moduleExports . Server &&
8280 moduleExports . Server . prototype ,
83- 'emit' , this . patchIncomingRequest ( ) ) ;
81+ 'emit' as never , this . patchIncomingRequest ( ) ) ;
8482 } else {
8583 this . logger . error (
8684 'Could not apply patch to %s.emit. Interface is not as expected.' ,
@@ -110,10 +108,8 @@ export class HttpPlugin extends classes.BasePlugin {
110108 /**
111109 * Creates spans for incoming requests, restoring spans' context if applied.
112110 */
113- patchIncomingRequest ( ) {
114- // TODO: evaluate if this function should return RequestFunction
115- return ( original : RequestFunction ) :
116- types . Func < httpModule . ClientRequest > => {
111+ protected patchIncomingRequest ( ) {
112+ return ( original : RequestFunction ) => {
117113 const plugin = this ;
118114 return function incomingRequest (
119115 event : string , request : httpModule . IncomingMessage ,
@@ -198,7 +194,7 @@ export class HttpPlugin extends classes.BasePlugin {
198194 * Creates spans for outgoing requests, sending spans' context for distributed
199195 * tracing.
200196 */
201- patchOutgoingRequest ( ) {
197+ protected patchOutgoingRequest ( ) {
202198 return ( original : types . Func < httpModule . ClientRequest > ) :
203199 types . Func < httpModule . ClientRequest > => {
204200 const plugin = this ;
@@ -252,16 +248,15 @@ export class HttpPlugin extends classes.BasePlugin {
252248 } ;
253249 }
254250
255- // TODO: type of options shold be better define
256251 /**
257252 * Injects span's context to header for distributed tracing and finshes the
258253 * span when the response is finished.
259254 * @param original The original patched function.
260255 * @param options The arguments to the original function.
261256 */
262- makeRequestTrace (
257+ private makeRequestTrace (
263258 // tslint:disable-next-line:no-any
264- request : httpModule . ClientRequest , options : any ,
259+ request : httpModule . ClientRequest , options : httpModule . RequestOptions ,
265260 plugin : HttpPlugin ) : types . Func < httpModule . ClientRequest > {
266261 return ( span : types . Span ) : httpModule . ClientRequest => {
267262 plugin . logger . debug ( 'makeRequestTrace' ) ;
@@ -290,15 +285,17 @@ export class HttpPlugin extends classes.BasePlugin {
290285 response . on ( 'end' , ( ) => {
291286 plugin . logger . debug ( 'outgoingRequest on end()' ) ;
292287 const method = response . method ? response . method : 'GET' ;
293- const reqUrl = url . parse ( options ) ;
294288 const userAgent =
295289 headers ? ( headers [ 'user-agent' ] || headers [ 'User-Agent' ] ) : null ;
296290
297- span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_HOST , reqUrl . hostname ) ;
291+ span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_HOST , options . hostname ) ;
298292 span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_METHOD , method ) ;
299- span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_PATH , reqUrl . pathname ) ;
300- span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_ROUTE , reqUrl . path ) ;
301- span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_USER_AGENT , userAgent ) ;
293+ span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_PATH , options . path ) ;
294+ span . addAttribute ( HttpPlugin . ATTRIBUTE_HTTP_ROUTE , options . path ) ;
295+ if ( userAgent ) {
296+ span . addAttribute (
297+ HttpPlugin . ATTRIBUTE_HTTP_USER_AGENT , userAgent . toString ( ) ) ;
298+ }
302299 span . addAttribute (
303300 HttpPlugin . ATTRIBUTE_HTTP_STATUS_CODE ,
304301 response . statusCode . toString ( ) ) ;
0 commit comments