Skip to content

Commit 54879ab

Browse files
FlarnaOlivierAlbertini
authored andcommitted
chore(plugin-https): sync https tests with http (open-telemetry#597)
* chore(plugin-https): sync https tests with http * chore: use Http instead typeof http * chore: review finding, improve https detection * chore: fix node 8 * chore: fix path to test files
1 parent daff102 commit 54879ab

File tree

13 files changed

+664
-439
lines changed

13 files changed

+664
-439
lines changed

packages/opentelemetry-plugin-http/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"types": "build/src/index.d.ts",
77
"repository": "open-telemetry/opentelemetry-js",
88
"scripts": {
9-
"test": "nyc ts-mocha -p tsconfig.json test/**/*/*.test.ts",
9+
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
1010
"tdd": "yarn test -- --watch-extensions ts --watch",
1111
"clean": "rimraf build/*",
1212
"check": "gts check",

packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { HttpPlugin, plugin } from '../../src/http';
2929
import { assertSpan } from '../utils/assertSpan';
3030
import { DummyPropagation } from '../utils/DummyPropagation';
3131
import { httpRequest } from '../utils/httpRequest';
32-
import * as utils from '../../src/utils';
32+
import { OT_REQUEST_HEADER } from '../../src/utils';
3333
import { HttpPluginConfig, Http } from '../../src/types';
3434
import { AttributeNames } from '../../src/enums/AttributeNames';
3535

@@ -77,8 +77,8 @@ describe('HttpPlugin', () => {
7777
assert.strictEqual(process.versions.node, plugin.version);
7878
});
7979

80-
it('moduleName should be http', () => {
81-
assert.strictEqual('http', plugin.moduleName);
80+
it(`moduleName should be ${protocol}`, () => {
81+
assert.strictEqual(protocol, plugin.moduleName);
8282
});
8383

8484
describe('enable()', () => {
@@ -123,7 +123,7 @@ describe('HttpPlugin', () => {
123123

124124
it('should generate valid spans (client side and server side)', async () => {
125125
const result = await httpRequest.get(
126-
`http://${hostname}:${serverPort}${pathname}`
126+
`${protocol}://${hostname}:${serverPort}${pathname}`
127127
);
128128
const spans = memoryExporter.getFinishedSpans();
129129
const [incomingSpan, outgoingSpan] = spans;
@@ -142,14 +142,14 @@ describe('HttpPlugin', () => {
142142
assertSpan(outgoingSpan, SpanKind.CLIENT, validations);
143143
});
144144

145-
it(`should not trace requests with '${utils.OT_REQUEST_HEADER}' header`, async () => {
145+
it(`should not trace requests with '${OT_REQUEST_HEADER}' header`, async () => {
146146
const testPath = '/outgoing/do-not-trace';
147147
doNock(hostname, testPath, 200, 'Ok');
148148

149149
const options = {
150150
host: hostname,
151151
path: testPath,
152-
headers: { [utils.OT_REQUEST_HEADER]: 1 },
152+
headers: { [OT_REQUEST_HEADER]: 1 },
153153
};
154154

155155
const result = await httpRequest.get(options);
@@ -171,7 +171,7 @@ describe('HttpPlugin', () => {
171171
(url: string) => url.endsWith(`/ignored/function`),
172172
],
173173
ignoreOutgoingUrls: [
174-
`http://${hostname}:${serverPort}/ignored/string`,
174+
`${protocol}://${hostname}:${serverPort}/ignored/string`,
175175
/\/ignored\/regexp$/i,
176176
(url: string) => url.endsWith(`/ignored/function`),
177177
],
@@ -190,11 +190,11 @@ describe('HttpPlugin', () => {
190190
plugin.disable();
191191
});
192192

193-
it('http module should be patched', () => {
193+
it(`${protocol} module should be patched`, () => {
194194
assert.strictEqual(http.Server.prototype.emit.__wrapped, true);
195195
});
196196

197-
it("should not patch if it's not a http module", () => {
197+
it(`should not patch if it's not a ${protocol} module`, () => {
198198
const httpNotPatched = new HttpPlugin(
199199
plugin.component,
200200
process.versions.node
@@ -204,7 +204,7 @@ describe('HttpPlugin', () => {
204204

205205
it('should generate valid spans (client side and server side)', async () => {
206206
const result = await httpRequest.get(
207-
`http://${hostname}:${serverPort}${pathname}`
207+
`${protocol}://${hostname}:${serverPort}${pathname}`
208208
);
209209
const spans = memoryExporter.getFinishedSpans();
210210
const [incomingSpan, outgoingSpan] = spans;
@@ -223,14 +223,14 @@ describe('HttpPlugin', () => {
223223
assertSpan(outgoingSpan, SpanKind.CLIENT, validations);
224224
});
225225

226-
it(`should not trace requests with '${utils.OT_REQUEST_HEADER}' header`, async () => {
226+
it(`should not trace requests with '${OT_REQUEST_HEADER}' header`, async () => {
227227
const testPath = '/outgoing/do-not-trace';
228228
doNock(hostname, testPath, 200, 'Ok');
229229

230230
const options = {
231231
host: hostname,
232232
path: testPath,
233-
headers: { [utils.OT_REQUEST_HEADER]: 1 },
233+
headers: { [OT_REQUEST_HEADER]: 1 },
234234
};
235235

236236
const result = await httpRequest.get(options);
@@ -395,13 +395,13 @@ describe('HttpPlugin', () => {
395395
}
396396

397397
for (const arg of ['string', {}, new Date()]) {
398-
it(`should be tracable and not throw exception in http plugin when passing the following argument ${JSON.stringify(
398+
it(`should be tracable and not throw exception in ${protocol} plugin when passing the following argument ${JSON.stringify(
399399
arg
400400
)}`, async () => {
401401
try {
402402
await httpRequest.get(arg);
403403
} catch (error) {
404-
// http request has been made
404+
// request has been made
405405
// nock throw
406406
assert.ok(error.message.startsWith('Nock: No match for request'));
407407
}
@@ -411,14 +411,14 @@ describe('HttpPlugin', () => {
411411
}
412412

413413
for (const arg of [true, 1, false, 0, '']) {
414-
it(`should not throw exception in http plugin when passing the following argument ${JSON.stringify(
414+
it(`should not throw exception in ${protocol} plugin when passing the following argument ${JSON.stringify(
415415
arg
416416
)}`, async () => {
417417
try {
418418
// @ts-ignore
419419
await httpRequest.get(arg);
420420
} catch (error) {
421-
// http request has been made
421+
// request has been made
422422
// nock throw
423423
assert.ok(
424424
error.stack.indexOf(
@@ -447,7 +447,7 @@ describe('HttpPlugin', () => {
447447

448448
const promiseRequest = new Promise((resolve, reject) => {
449449
const req = http.request(
450-
`http://${hostname}${testPath}`,
450+
`${protocol}://${hostname}${testPath}`,
451451
(resp: http.IncomingMessage) => {
452452
let data = '';
453453
resp.on('data', chunk => {
@@ -488,7 +488,7 @@ describe('HttpPlugin', () => {
488488

489489
const promiseRequest = new Promise((resolve, reject) => {
490490
const req = http.request(
491-
`http://${hostname}${testPath}`,
491+
`${protocol}://${hostname}${testPath}`,
492492
(resp: http.IncomingMessage) => {
493493
let data = '';
494494
resp.on('data', chunk => {
@@ -512,14 +512,14 @@ describe('HttpPlugin', () => {
512512
});
513513

514514
it('should have 1 ended span when request is aborted', async () => {
515-
nock('http://my.server.com')
515+
nock(`${protocol}://my.server.com`)
516516
.get('/')
517517
.socketDelay(50)
518518
.reply(200, '<html></html>');
519519

520520
const promiseRequest = new Promise((resolve, reject) => {
521521
const req = http.request(
522-
'http://my.server.com',
522+
`${protocol}://my.server.com`,
523523
(resp: http.IncomingMessage) => {
524524
let data = '';
525525
resp.on('data', chunk => {

packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { HttpPluginConfig } from '../../src/types';
3838
import { customAttributeFunction } from './http-enable.test';
3939

4040
const memoryExporter = new InMemorySpanExporter();
41+
const protocol = 'http';
4142

4243
describe('Packages', () => {
4344
describe('get', () => {
@@ -93,8 +94,8 @@ describe('Packages', () => {
9394
// https://github.com/nock/nock/pull/1551
9495
// https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258
9596
// TODO: check if this is still the case when new version
96-
'http://info.cern.ch/'
97-
: `http://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8`
97+
`${protocol}://info.cern.ch/`
98+
: `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8`
9899
);
99100
const result = await httpPackage.get(urlparsed.href!);
100101
if (!resHeaders) {

packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
SimpleSpanProcessor,
3131
} from '@opentelemetry/tracing';
3232
import { HttpPluginConfig } from '../../src/types';
33-
33+
const protocol = 'http';
3434
const serverPort = 32345;
3535
const hostname = 'localhost';
3636
const memoryExporter = new InMemorySpanExporter();
@@ -70,7 +70,7 @@ describe('HttpPlugin Integration tests', () => {
7070

7171
before(() => {
7272
const ignoreConfig = [
73-
`http://${hostname}:${serverPort}/ignored/string`,
73+
`${protocol}://${hostname}:${serverPort}/ignored/string`,
7474
/\/ignored\/regexp$/i,
7575
(url: string) => url.endsWith(`/ignored/function`),
7676
];
@@ -93,7 +93,9 @@ describe('HttpPlugin Integration tests', () => {
9393
let spans = memoryExporter.getFinishedSpans();
9494
assert.strictEqual(spans.length, 0);
9595

96-
const result = await httpRequest.get(`http://google.fr/?query=test`);
96+
const result = await httpRequest.get(
97+
`${protocol}://google.fr/?query=test`
98+
);
9799

98100
spans = memoryExporter.getFinishedSpans();
99101
const span = spans[0];
@@ -118,7 +120,7 @@ describe('HttpPlugin Integration tests', () => {
118120
assert.strictEqual(spans.length, 0);
119121

120122
const result = await httpRequest.get(
121-
new url.URL('http://google.fr/?query=test')
123+
new url.URL(`${protocol}://google.fr/?query=test`)
122124
);
123125

124126
spans = memoryExporter.getFinishedSpans();
@@ -144,7 +146,7 @@ describe('HttpPlugin Integration tests', () => {
144146
assert.strictEqual(spans.length, 0);
145147

146148
const result = await httpRequest.get(
147-
new url.URL('http://google.fr/?query=test'),
149+
new url.URL(`${protocol}://google.fr/?query=test`),
148150
{ headers: { 'x-foo': 'foo' } }
149151
);
150152

@@ -168,7 +170,7 @@ describe('HttpPlugin Integration tests', () => {
168170
});
169171

170172
it('custom attributes should show up on client spans', async () => {
171-
const result = await httpRequest.get(`http://google.fr/`);
173+
const result = await httpRequest.get(`${protocol}://google.fr/`);
172174
const spans = memoryExporter.getFinishedSpans();
173175
const span = spans[0];
174176
const validations = {
@@ -192,7 +194,7 @@ describe('HttpPlugin Integration tests', () => {
192194
assert.strictEqual(spans.length, 0);
193195
const options = Object.assign(
194196
{ headers: { Expect: '100-continue' } },
195-
url.parse('http://google.fr/')
197+
url.parse(`${protocol}://google.fr/`)
196198
);
197199

198200
const result = await httpRequest.get(options);
@@ -223,7 +225,7 @@ describe('HttpPlugin Integration tests', () => {
223225
{ Expect: '100-continue', 'user-agent': 'http-plugin-test' },
224226
{ 'user-agent': 'http-plugin-test' },
225227
]) {
226-
it(`should create a span for GET requests and add propagation when using the following signature: http.get(url, options, callback) and following headers: ${JSON.stringify(
228+
it(`should create a span for GET requests and add propagation when using the following signature: get(url, options, callback) and following headers: ${JSON.stringify(
227229
headers
228230
)}`, done => {
229231
let validations: {
@@ -239,7 +241,7 @@ describe('HttpPlugin Integration tests', () => {
239241
assert.strictEqual(spans.length, 0);
240242
const options = { headers };
241243
const req = http.get(
242-
'http://google.fr/',
244+
`${protocol}://google.fr/`,
243245
options,
244246
(resp: http.IncomingMessage) => {
245247
const res = (resp as unknown) as http.IncomingMessage & {
@@ -266,7 +268,7 @@ describe('HttpPlugin Integration tests', () => {
266268
}
267269
);
268270

269-
req.once('close', () => {
271+
req.on('close', () => {
270272
const spans = memoryExporter.getFinishedSpans();
271273
assert.strictEqual(spans.length, 1);
272274
assert.ok(spans[0].name.indexOf('GET /') >= 0);

packages/opentelemetry-plugin-https/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![devDependencies][devDependencies-image]][devDependencies-url]
66
[![Apache License][license-image]][license-image]
77

8-
This module provides automatic instrumentation for [`https`](http://nodejs.org/dist/latest/docs/api/https.html).
8+
This module provides automatic instrumentation for [`https`](http://nodejs.org/api/https.html).
99

1010
For automatic instrumentation see the
1111
[@opentelemetry/node](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node) package.

packages/opentelemetry-plugin-https/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"types": "build/src/index.d.ts",
77
"repository": "open-telemetry/opentelemetry-js",
88
"scripts": {
9-
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.ts'",
9+
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
1010
"tdd": "yarn test -- --watch-extensions ts --watch",
1111
"clean": "rimraf build/*",
1212
"check": "gts check",
13+
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
1314
"precompile": "tsc --version",
1415
"compile": "tsc -p .",
1516
"fix": "gts fix",

packages/opentelemetry-plugin-https/src/https.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { HttpPlugin, Func, HttpRequestArgs } from '@opentelemetry/plugin-http';
1818
import * as http from 'http';
1919
import * as https from 'https';
20+
import { URL } from 'url';
2021
import * as semver from 'semver';
2122
import * as shimmer from 'shimmer';
2223
import * as utils from './utils';
@@ -81,12 +82,13 @@ export class HttpsPlugin extends HttpPlugin {
8182
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
8283
const plugin = this;
8384
return function httpsOutgoingRequest(
84-
options,
85+
options: https.RequestOptions | string | URL,
8586
...args: HttpRequestArgs
8687
): http.ClientRequest {
8788
// Makes sure options will have default HTTPS parameters
88-
if (typeof options === 'object') {
89-
utils.setDefaultOptions(options);
89+
if (typeof options === 'object' && !(options instanceof URL)) {
90+
options = Object.assign({}, options);
91+
utils.setDefaultOptions(options as https.RequestOptions);
9092
}
9193
return plugin._getPatchOutgoingRequestFunction()(original)(
9294
options,
@@ -105,17 +107,9 @@ export class HttpsPlugin extends HttpPlugin {
105107
) {
106108
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
107109
return function httpsOutgoingRequest(
108-
options: https.RequestOptions | string,
110+
options: https.RequestOptions | string | URL,
109111
...args: HttpRequestArgs
110112
): http.ClientRequest {
111-
const optionsType = typeof options;
112-
// Makes sure options will have default HTTPS parameters
113-
if (optionsType === 'object') {
114-
utils.setDefaultOptions(options as https.RequestOptions);
115-
} else if (typeof args[0] === 'object' && optionsType === 'string') {
116-
utils.setDefaultOptions(args[0] as https.RequestOptions);
117-
}
118-
119113
return plugin._getPatchOutgoingGetFunction(clientRequest)(original)(
120114
options,
121115
...args

packages/opentelemetry-plugin-https/test/functionals/https-disable.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { NoopLogger } from '@opentelemetry/core';
18-
import { NodeTracer } from '@opentelemetry/node';
19-
import { Http } from '@opentelemetry/plugin-http';
2017
import * as assert from 'assert';
2118
import * as fs from 'fs';
2219
import * as https from 'https';
23-
import { AddressInfo } from 'net';
2420
import * as nock from 'nock';
2521
import * as sinon from 'sinon';
22+
2623
import { plugin } from '../../src/https';
24+
import { NodeTracer } from '@opentelemetry/node';
25+
import { NoopLogger } from '@opentelemetry/core';
26+
import { Http } from '@opentelemetry/plugin-http';
27+
import { AddressInfo } from 'net';
2728
import { DummyPropagation } from '../utils/DummyPropagation';
2829
import { httpsRequest } from '../utils/httpsRequest';
2930

0 commit comments

Comments
 (0)