Skip to content

Commit e606a4c

Browse files
committed
refactor(instrumentation-http): fix eslint warnings
``` /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts 86:27 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts 86:27 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts 81:25 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api 156:43 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api 161:43 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api 213:35 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts 300:9 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts 274:9 warning 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead node/no-deprecated-api ``` Generally speaking, `new URL()` is not a direct replacement for the deprecated `url.parse()`, so this type of change requires careful considerations. However, in this instance, these are all found in test code, which cuts out a lot of the typically associated issues, and the tests passing after the change is a good indication for correctness. Ref open-telemetry#5365
1 parent 83ad899 commit e606a4c

File tree

5 files changed

+40
-36
lines changed

5 files changed

+40
-36
lines changed

experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
} from '@opentelemetry/sdk-trace-base';
2424
import * as assert from 'assert';
2525
import * as path from 'path';
26-
import * as url from 'url';
2726
import { HttpInstrumentation } from '../../src/http';
2827
import { assertSpan } from '../utils/assertSpan';
2928
import { DummyPropagation } from '../utils/DummyPropagation';
@@ -83,22 +82,22 @@ describe('Packages', () => {
8382
it(`should create a span for GET requests and add propagation headers by using ${name} package`, async () => {
8483
nock.load(path.join(__dirname, '../', '/fixtures/google-http.json'));
8584

86-
const urlparsed = url.parse(
85+
const url = new URL(
8786
`${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8`
8887
);
89-
const result = await httpPackage.get(urlparsed.href!);
88+
const result = await httpPackage.get(url.href);
9089
if (!resHeaders) {
9190
const res = result as axios.AxiosResponse<unknown>;
9291
resHeaders = res.headers as any;
9392
}
9493
const spans = memoryExporter.getFinishedSpans();
9594
const span = spans[0];
9695
const validations = {
97-
hostname: urlparsed.hostname!,
96+
hostname: 'www.google.com',
9897
httpStatusCode: 200,
9998
httpMethod: 'GET',
100-
pathname: urlparsed.pathname!,
101-
path: urlparsed.path,
99+
pathname: '/search',
100+
path: '/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8',
102101
resHeaders,
103102
component: 'http',
104103
};

experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
} from '@opentelemetry/sdk-trace-base';
2424
import * as assert from 'assert';
2525
import * as path from 'path';
26-
import * as url from 'url';
2726
import { HttpInstrumentation } from '../../src/http';
2827
import { assertSpan } from '../utils/assertSpan';
2928
import { DummyPropagation } from '../utils/DummyPropagation';
@@ -83,22 +82,22 @@ describe('Packages', () => {
8382
it(`should create a span for GET requests and add propagation headers by using ${name} package`, async () => {
8483
nock.load(path.join(__dirname, '../', '/fixtures/google-https.json'));
8584

86-
const urlparsed = url.parse(
85+
const url = new URL(
8786
'https://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8'
8887
);
89-
const result = await httpPackage.get(urlparsed.href!);
88+
const result = await httpPackage.get(url.href!);
9089
if (!resHeaders) {
9190
const res = result as axios.AxiosResponse<unknown>;
9291
resHeaders = res.headers as any;
9392
}
9493
const spans = memoryExporter.getFinishedSpans();
9594
const span = spans[0];
9695
const validations = {
97-
hostname: urlparsed.hostname!,
96+
hostname: 'www.google.com',
9897
httpStatusCode: 200,
9998
httpMethod: 'GET',
100-
pathname: urlparsed.pathname!,
101-
path: urlparsed.path,
99+
pathname: '/search',
100+
path: '/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8',
102101
resHeaders,
103102
component: 'https',
104103
};

experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ describe('Utility', () => {
7878
describe('getRequestInfo()', () => {
7979
it('should get options object', () => {
8080
const webUrl = 'http://u:p@google.fr/aPath?qu=ry';
81+
// This is explicitly testing interop with the legacy API
82+
// eslint-disable-next-line node/no-deprecated-api
8183
const urlParsed = url.parse(webUrl);
8284
const urlParsedWithoutPathname = {
8385
...urlParsed,
@@ -152,13 +154,15 @@ describe('Utility', () => {
152154

153155
describe('getAbsoluteUrl()', () => {
154156
it('should return absolute url with localhost', () => {
155-
const path = '/test/1';
156-
const result = utils.getAbsoluteUrl(url.parse(path), {});
157-
assert.strictEqual(result, `http://localhost${path}`);
157+
const result = utils.getAbsoluteUrl({ path: '/test/1' }, {});
158+
assert.strictEqual(result, 'http://localhost/test/1');
158159
});
159160
it('should return absolute url', () => {
160-
const absUrl = 'http://www.google/test/1?query=1';
161-
const result = utils.getAbsoluteUrl(url.parse(absUrl), {});
161+
const absUrl = 'http://www.google.com/test/1?query=1';
162+
const result = utils.getAbsoluteUrl(
163+
{ protocol: 'http:', host: 'www.google.com', path: '/test/1?query=1' },
164+
{}
165+
);
162166
assert.strictEqual(result, absUrl);
163167
});
164168
it('should return default url', () => {
@@ -210,7 +214,7 @@ describe('Utility', () => {
210214
assert.strictEqual(utils.isValidOptionsType(options), false);
211215
});
212216
});
213-
for (const options of ['url', url.parse('http://url.com'), {}]) {
217+
for (const options of ['url', new URL('http://url.com'), {}]) {
214218
it(`should return true with the following value: ${JSON.stringify(
215219
options
216220
)}`, () => {

experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
SEMATTRS_NET_TRANSPORT,
2424
} from '@opentelemetry/semantic-conventions';
2525
import * as assert from 'assert';
26-
import * as url from 'url';
2726
import { HttpInstrumentation } from '../../src/http';
2827
import { assertSpan } from '../utils/assertSpan';
2928
import * as utils from '../utils/utils';
@@ -180,7 +179,7 @@ describe('HttpInstrumentation Integration tests', () => {
180179
assert.strictEqual(spans.length, 0);
181180

182181
const result = await httpRequest.get(
183-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`)
182+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`)
184183
);
185184

186185
spans = memoryExporter.getFinishedSpans();
@@ -207,7 +206,7 @@ describe('HttpInstrumentation Integration tests', () => {
207206
assert.strictEqual(spans.length, 0);
208207

209208
const result = await httpRequest.get(
210-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
209+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
211210
{
212211
headers: { 'x-foo': 'foo' },
213212
}
@@ -270,7 +269,7 @@ describe('HttpInstrumentation Integration tests', () => {
270269

271270
const headers = { 'x-foo': 'foo' };
272271
const result = await httpRequest.get(
273-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
272+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
274273
{ headers }
275274
);
276275
assert.deepStrictEqual(headers, { 'x-foo': 'foo' });
@@ -284,7 +283,7 @@ describe('HttpInstrumentation Integration tests', () => {
284283

285284
const headers = { 'x-foo': 'foo', forwarded: 'malformed' };
286285
const result = await httpRequest.get(
287-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
286+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
288287
{ headers }
289288
);
290289

@@ -295,12 +294,14 @@ describe('HttpInstrumentation Integration tests', () => {
295294
it('should create a span for GET requests and add propagation headers with Expect headers', async () => {
296295
let spans = memoryExporter.getFinishedSpans();
297296
assert.strictEqual(spans.length, 0);
298-
const options = Object.assign(
299-
{ headers: { Expect: '100-continue' } },
300-
url.parse(`${protocol}://localhost:${mockServerPort}/`)
301-
);
302297

303-
const result = await httpRequest.get(options);
298+
const result = await httpRequest.get({
299+
protocol: `${protocol}:`,
300+
host: 'localhost',
301+
port: mockServerPort,
302+
path: '/',
303+
headers: { Expect: '100-continue' },
304+
});
304305
spans = memoryExporter.getFinishedSpans();
305306
const span = spans.find(s => s.kind === SpanKind.CLIENT);
306307
assert.ok(span);

experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import * as fs from 'fs';
2727
import * as path from 'path';
2828
import { Socket } from 'net';
2929
import { assertSpan } from '../utils/assertSpan';
30-
import * as url from 'url';
3130
import * as utils from '../utils/utils';
3231
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
3332
import {
@@ -182,7 +181,7 @@ describe('HttpsInstrumentation Integration tests', () => {
182181
assert.strictEqual(spans.length, 0);
183182

184183
const result = await httpsRequest.get(
185-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`)
184+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`)
186185
);
187186

188187
spans = memoryExporter.getFinishedSpans();
@@ -209,7 +208,7 @@ describe('HttpsInstrumentation Integration tests', () => {
209208
assert.strictEqual(spans.length, 0);
210209

211210
const result = await httpsRequest.get(
212-
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
211+
new URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
213212
{
214213
headers: { 'x-foo': 'foo' },
215214
}
@@ -269,12 +268,14 @@ describe('HttpsInstrumentation Integration tests', () => {
269268
it('should create a span for GET requests and add propagation headers with Expect headers', async () => {
270269
let spans = memoryExporter.getFinishedSpans();
271270
assert.strictEqual(spans.length, 0);
272-
const options = Object.assign(
273-
{ headers: { Expect: '100-continue' } },
274-
url.parse(`${protocol}://localhost:${mockServerPort}/`)
275-
);
276271

277-
const result = await httpsRequest.get(options);
272+
const result = await httpsRequest.get({
273+
protocol: `${protocol}:`,
274+
host: 'localhost',
275+
port: mockServerPort,
276+
path: '/',
277+
headers: { Expect: '100-continue' },
278+
});
278279
spans = memoryExporter.getFinishedSpans();
279280
const span = spans.find(s => s.kind === SpanKind.CLIENT);
280281
assert.ok(span);

0 commit comments

Comments
 (0)