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

Commit 77ecbf0

Browse files
StoneDotmayurkale22
authored andcommitted
Fix a TypeError of url.parse (#640)
* Fix a TypeError of url.parse * Update changelog * Add a test for http2
1 parent ceb3327 commit 77ecbf0

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All notable changes to this project will be documented in this file.
44

55
## Unreleased
6+
- Fix a TypeError of url.parse (#640)
67

78
## 0.0.17 - 2019-09-03
89
- fix: allow override global trace params limits (#643)

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Http2Plugin extends HttpPlugin {
7878
return (original: ConnectFunction): Func<http2.ClientHttp2Session> => {
7979
return function patchedConnect(
8080
this: Http2Plugin,
81-
authority: string
81+
authority: string | url.URL
8282
): http2.ClientHttp2Session {
8383
const client = original.apply(this, arguments);
8484
shimmer.wrap(client, 'request', original =>
@@ -94,7 +94,7 @@ export class Http2Plugin extends HttpPlugin {
9494
const plugin = this;
9595
return (
9696
original: RequestFunction,
97-
authority: string
97+
authority: string | url.URL
9898
): Func<http2.ClientHttp2Stream> => {
9999
return function patchedRequest(
100100
this: http2.Http2Session,
@@ -146,7 +146,7 @@ export class Http2Plugin extends HttpPlugin {
146146
private getMakeHttp2RequestTraceFunction(
147147
request: http2.ClientHttp2Stream,
148148
headers: http2.OutgoingHttpHeaders,
149-
authority: string,
149+
authority: string | url.URL,
150150
plugin: Http2Plugin
151151
): Func<http2.ClientHttp2Stream> {
152152
return (span: Span): http2.ClientHttp2Stream => {
@@ -176,7 +176,10 @@ export class Http2Plugin extends HttpPlugin {
176176
const userAgent =
177177
headers['user-agent'] || headers['User-Agent'] || null;
178178

179-
const host = url.parse(authority).host;
179+
const host = (authority instanceof url.URL
180+
? authority
181+
: url.parse(authority)
182+
).host;
180183
if (host) {
181184
span.addAttribute(Http2Plugin.ATTRIBUTE_HTTP_HOST, host);
182185
}

packages/opencensus-instrumentation-http2/test/test-http2.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as semver from 'semver';
2727

2828
import { Http2Plugin, plugin } from '../src/';
2929
import { IncomingHttpHeaders, ServerHttp2Stream } from 'http2';
30+
import { URL } from 'url';
3031

3132
const VERSION = process.versions.node;
3233

@@ -123,9 +124,8 @@ describe('Http2Plugin', () => {
123124
const serverPort = 8080;
124125
const serverPort2 = 8081;
125126
const host = `localhost:${serverPort}`;
126-
const host2 = `localhost:${serverPort2}`;
127127
const authority = `http://${host}`;
128-
const authority2 = `http://${host2}`;
128+
const authorityUrlObject = new URL('/', `http://${host}/`);
129129

130130
const log = logger.logger();
131131
const tracer = new CoreTracer();
@@ -161,7 +161,7 @@ describe('Http2Plugin', () => {
161161
server2.listen(serverPort2);
162162

163163
client = http2.connect(authority);
164-
client2 = http2.connect(authority2);
164+
client2 = http2.connect(authorityUrlObject);
165165
});
166166

167167
beforeEach(() => {
@@ -200,6 +200,24 @@ describe('Http2Plugin', () => {
200200
});
201201
});
202202

203+
it('should succeed when the client is connected using the url.URL object (#640)', async () => {
204+
const statusCode = 200;
205+
const testPath = `/${statusCode}`;
206+
const requestOptions = {
207+
':method': 'GET',
208+
':path': testPath,
209+
};
210+
211+
assert.strictEqual(spanVerifier.endedSpans.length, 0);
212+
213+
await http2Request.get(client2, requestOptions).then(result => {
214+
assert.strictEqual(result, statusCode.toString());
215+
assert.strictEqual(spanVerifier.endedSpans.length, 2);
216+
const span = spanVerifier.endedSpans[1];
217+
assertSpanAttributes(span, statusCode, 'GET', host, testPath);
218+
});
219+
});
220+
203221
const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500];
204222

205223
httpErrorCodes.map(errorCode => {

0 commit comments

Comments
 (0)