Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 17dca7d

Browse files
eduardoemerykjin
authored andcommitted
refactor: address instrumentation-https TODOs (#46)
* refactor: address instrumentation-https TODOs * refactor(fix): changes to address review comments
1 parent 0af72d0 commit 17dca7d

2 files changed

Lines changed: 171 additions & 130 deletions

File tree

packages/opencensus-instrumentation-https/src/https.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import {HttpPlugin} from '@opencensus/instrumentation-http';
1818
import {types} from '@opencensus/opencensus-core';
1919
import {classes} from '@opencensus/opencensus-core';
2020
import {logger} from '@opencensus/opencensus-core';
21+
import * as http from 'http';
22+
import * as https from 'https';
23+
import * as semver from 'semver';
2124
import * as shimmer from 'shimmer';
2225
import * 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

Comments
 (0)