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

Fix a TypeError of url.parse #640

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All notable changes to this project will be documented in this file.

## Unreleased
- Fix a TypeError of url.parse (#640)

## 0.0.17 - 2019-09-03
- fix: allow override global trace params limits (#643)
Expand Down
11 changes: 7 additions & 4 deletions packages/opencensus-instrumentation-http2/src/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Http2Plugin extends HttpPlugin {
return (original: ConnectFunction): Func<http2.ClientHttp2Session> => {
return function patchedConnect(
this: Http2Plugin,
authority: string
authority: string | url.URL
): http2.ClientHttp2Session {
const client = original.apply(this, arguments);
shimmer.wrap(client, 'request', original =>
Expand All @@ -94,7 +94,7 @@ export class Http2Plugin extends HttpPlugin {
const plugin = this;
return (
original: RequestFunction,
authority: string
authority: string | url.URL
): Func<http2.ClientHttp2Stream> => {
return function patchedRequest(
this: http2.Http2Session,
Expand Down Expand Up @@ -146,7 +146,7 @@ export class Http2Plugin extends HttpPlugin {
private getMakeHttp2RequestTraceFunction(
request: http2.ClientHttp2Stream,
headers: http2.OutgoingHttpHeaders,
authority: string,
authority: string | url.URL,
plugin: Http2Plugin
): Func<http2.ClientHttp2Stream> {
return (span: Span): http2.ClientHttp2Stream => {
Expand Down Expand Up @@ -176,7 +176,10 @@ export class Http2Plugin extends HttpPlugin {
const userAgent =
headers['user-agent'] || headers['User-Agent'] || null;

const host = url.parse(authority).host;
const host = (authority instanceof url.URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @StoneDot for the fix! Would you be willing to add a unit test for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, also update the CHANGELOG.md

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written the tests for this PR, but it hasn't worked correctly because of another bug (See #641).

Could I tweak the tests to pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I've updated CHANGELOG.md at #de29571

? authority
: url.parse(authority)
).host;
if (host) {
span.addAttribute(Http2Plugin.ATTRIBUTE_HTTP_HOST, host);
}
Expand Down
24 changes: 21 additions & 3 deletions packages/opencensus-instrumentation-http2/test/test-http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as semver from 'semver';

import { Http2Plugin, plugin } from '../src/';
import { IncomingHttpHeaders, ServerHttp2Stream } from 'http2';
import { URL } from 'url';

const VERSION = process.versions.node;

Expand Down Expand Up @@ -123,9 +124,8 @@ describe('Http2Plugin', () => {
const serverPort = 8080;
const serverPort2 = 8081;
const host = `localhost:${serverPort}`;
const host2 = `localhost:${serverPort2}`;
const authority = `http://${host}`;
const authority2 = `http://${host2}`;
const authorityUrlObject = new URL('/', `http://${host}/`);

const log = logger.logger();
const tracer = new CoreTracer();
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('Http2Plugin', () => {
server2.listen(serverPort2);

client = http2.connect(authority);
client2 = http2.connect(authority2);
client2 = http2.connect(authorityUrlObject);
});

beforeEach(() => {
Expand Down Expand Up @@ -200,6 +200,24 @@ describe('Http2Plugin', () => {
});
});

it('should succeed when the client is connected using the url.URL object (#640)', async () => {
const statusCode = 200;
const testPath = `/${statusCode}`;
const requestOptions = {
':method': 'GET',
':path': testPath,
};

assert.strictEqual(spanVerifier.endedSpans.length, 0);

await http2Request.get(client2, requestOptions).then(result => {
assert.strictEqual(result, statusCode.toString());
assert.strictEqual(spanVerifier.endedSpans.length, 2);
const span = spanVerifier.endedSpans[1];
assertSpanAttributes(span, statusCode, 'GET', host, testPath);
});
});

const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500];

httpErrorCodes.map(errorCode => {
Expand Down