@@ -18,6 +18,9 @@ import {HttpPlugin} from '@opencensus/instrumentation-http';
1818import { types } from '@opencensus/opencensus-core' ;
1919import { classes } from '@opencensus/opencensus-core' ;
2020import { logger } from '@opencensus/opencensus-core' ;
21+ import * as http from 'http' ;
22+ import * as https from 'https' ;
23+ import * as semver from 'semver' ;
2124import * as shimmer from 'shimmer' ;
2225import * as url from 'url' ;
2326
@@ -46,14 +49,35 @@ export class HttpsPlugin extends HttpPlugin {
4649 this . moduleName ) ;
4750 }
4851
49- // TODO: review the need to patch 'request'
50-
5152 shimmer . wrap (
52- this . moduleExports , 'get' , this . getPatchOutgoingRequestFunction ( ) ) ;
53+ this . moduleExports , 'request' , this . getPatchHttpsOutgoingRequest ( ) ) ;
54+ if ( semver . satisfies ( this . version , '>=8.0.0' ) ) {
55+ shimmer . wrap (
56+ this . moduleExports , 'get' , this . getPatchHttpsOutgoingRequest ( ) ) ;
57+ }
5358
5459 return this . moduleExports ;
5560 }
5661
62+ /** Patches HTTPS outgoing requests */
63+ private getPatchHttpsOutgoingRequest ( ) {
64+ return ( original : types . Func < http . ClientRequest > ) :
65+ types . Func < http . ClientRequest > => {
66+ const plugin = this ;
67+ return function httpsOutgoingRequest (
68+ options , callback ) : http . ClientRequest {
69+ // Makes sure options will have default HTTPS parameters
70+ if ( typeof ( options ) !== 'string' ) {
71+ options . protocol = options . protocol || 'https:' ;
72+ options . port = options . port || 443 ;
73+ options . agent = options . agent || https . globalAgent ;
74+ }
75+ return ( plugin . getPatchOutgoingRequestFunction ( ) ) ( original ) (
76+ options , callback ) ;
77+ } ;
78+ } ;
79+ }
80+
5781 /** Unpatches all HTTPS patched function. */
5882 protected applyUnpatch ( ) : void {
5983 if ( this . moduleExports && this . moduleExports . Server &&
@@ -63,7 +87,10 @@ export class HttpsPlugin extends HttpPlugin {
6387 this . moduleExports . Server . prototype ,
6488 'emit' ) ;
6589 }
66- shimmer . unwrap ( this . moduleExports , 'get' ) ;
90+ shimmer . unwrap ( this . moduleExports , 'request' ) ;
91+ if ( semver . satisfies ( this . version , '>=8.0.0' ) ) {
92+ shimmer . unwrap ( this . moduleExports , 'get' ) ;
93+ }
6794 }
6895}
6996
0 commit comments