From d6e9654216e5d38c56e2e2c5ad2dec142a41d94e Mon Sep 17 00:00:00 2001 From: Nir Hadassi Date: Fri, 12 Mar 2021 10:58:51 +0200 Subject: [PATCH] fix(http-instrumentation): crash fix (#2009) Co-authored-by: Valentin Marchaud --- .../src/http.ts | 2 +- .../test/functionals/https-enable.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-instrumentation-http/src/http.ts b/packages/opentelemetry-instrumentation-http/src/http.ts index d022b38a01..69d581c78a 100644 --- a/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/packages/opentelemetry-instrumentation-http/src/http.ts @@ -226,7 +226,7 @@ export class HttpInstrumentation extends InstrumentationBase { if ( component === 'https' && typeof options === 'object' && - options?.constructor.name !== 'URL' + options?.constructor?.name !== 'URL' ) { options = Object.assign({}, options); instrumentation._setDefaultOptions(options); diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index 2dd34b2c44..86de0fbefa 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -515,6 +515,32 @@ describe('HttpsInstrumentation', () => { } }); + it('should have 2 ended spans when provided "options" are an object without a constructor', async () => { + // Related issue: https://github.com/open-telemetry/opentelemetry-js/issues/2008 + const testPath = '/outgoing/test'; + const options = Object.create(null); + options.hostname = hostname; + options.port = serverPort; + options.path = pathname; + options.method = 'GET'; + + doNock(hostname, testPath, 200, 'Ok'); + + const promiseRequest = new Promise((resolve, _reject) => { + const req = https.request(options, (resp: http.IncomingMessage) => { + resp.on('data', () => {}); + resp.on('end', () => { + resolve({}); + }); + }); + return req.end(); + }); + + await promiseRequest; + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 2); + }); + it('should have 1 ended span when response.end throw an exception', async () => { const testPath = '/outgoing/rootSpan/childs/1'; doNock(hostname, testPath, 400, 'Not Ok');