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

Commit

Permalink
Fix a TypeError of url.parse (#640)
Browse files Browse the repository at this point in the history
* Fix a TypeError of url.parse

* Update changelog

* Add a test for http2
  • Loading branch information
StoneDot authored and mayurkale22 committed Sep 9, 2019
1 parent ceb3327 commit 77ecbf0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
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
? 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

0 comments on commit 77ecbf0

Please sign in to comment.