From 0970f41afc63480e49a50a9fa6e9bb40ae4f8b0a Mon Sep 17 00:00:00 2001 From: Hiroaki Goto <1642211+StoneDot@users.noreply.github.com> Date: Wed, 21 Aug 2019 14:53:19 +0900 Subject: [PATCH 1/3] Fix a TypeError of url.parse --- .../opencensus-instrumentation-http2/src/http2.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/opencensus-instrumentation-http2/src/http2.ts b/packages/opencensus-instrumentation-http2/src/http2.ts index 6af28a184..0420045fd 100644 --- a/packages/opencensus-instrumentation-http2/src/http2.ts +++ b/packages/opencensus-instrumentation-http2/src/http2.ts @@ -78,7 +78,7 @@ export class Http2Plugin extends HttpPlugin { return (original: ConnectFunction): Func => { return function patchedConnect( this: Http2Plugin, - authority: string + authority: string | url.URL ): http2.ClientHttp2Session { const client = original.apply(this, arguments); shimmer.wrap(client, 'request', original => @@ -94,7 +94,7 @@ export class Http2Plugin extends HttpPlugin { const plugin = this; return ( original: RequestFunction, - authority: string + authority: string | url.URL ): Func => { return function patchedRequest( this: http2.Http2Session, @@ -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 { return (span: Span): http2.ClientHttp2Stream => { @@ -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); } From ed7fcef3cbf6ca10b9c8b63026620a51d83446dc Mon Sep 17 00:00:00 2001 From: Hiroaki Goto <1642211+StoneDot@users.noreply.github.com> Date: Thu, 22 Aug 2019 10:53:20 +0900 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fccfdb5ff..00213b603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From 7c1134b25c64b82332d68c5d9242f1cad37307bc Mon Sep 17 00:00:00 2001 From: Hiroaki Goto <1642211+StoneDot@users.noreply.github.com> Date: Mon, 9 Sep 2019 11:22:51 +0900 Subject: [PATCH 3/3] Add a test for http2 --- .../test/test-http2.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/opencensus-instrumentation-http2/test/test-http2.ts b/packages/opencensus-instrumentation-http2/test/test-http2.ts index 84c2d2bc9..ec37d4cde 100644 --- a/packages/opencensus-instrumentation-http2/test/test-http2.ts +++ b/packages/opencensus-instrumentation-http2/test/test-http2.ts @@ -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; @@ -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(); @@ -161,7 +161,7 @@ describe('Http2Plugin', () => { server2.listen(serverPort2); client = http2.connect(authority); - client2 = http2.connect(authority2); + client2 = http2.connect(authorityUrlObject); }); beforeEach(() => { @@ -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 => {