Skip to content

Commit d6e9654

Browse files
Nir Hadassivmarchaud
andauthored
fix(http-instrumentation): crash fix (#2009)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent cc9f1d2 commit d6e9654

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/opentelemetry-instrumentation-http/src/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
226226
if (
227227
component === 'https' &&
228228
typeof options === 'object' &&
229-
options?.constructor.name !== 'URL'
229+
options?.constructor?.name !== 'URL'
230230
) {
231231
options = Object.assign({}, options);
232232
instrumentation._setDefaultOptions(options);

packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,32 @@ describe('HttpsInstrumentation', () => {
515515
}
516516
});
517517

518+
it('should have 2 ended spans when provided "options" are an object without a constructor', async () => {
519+
// Related issue: https://github.com/open-telemetry/opentelemetry-js/issues/2008
520+
const testPath = '/outgoing/test';
521+
const options = Object.create(null);
522+
options.hostname = hostname;
523+
options.port = serverPort;
524+
options.path = pathname;
525+
options.method = 'GET';
526+
527+
doNock(hostname, testPath, 200, 'Ok');
528+
529+
const promiseRequest = new Promise((resolve, _reject) => {
530+
const req = https.request(options, (resp: http.IncomingMessage) => {
531+
resp.on('data', () => {});
532+
resp.on('end', () => {
533+
resolve({});
534+
});
535+
});
536+
return req.end();
537+
});
538+
539+
await promiseRequest;
540+
const spans = memoryExporter.getFinishedSpans();
541+
assert.strictEqual(spans.length, 2);
542+
});
543+
518544
it('should have 1 ended span when response.end throw an exception', async () => {
519545
const testPath = '/outgoing/rootSpan/childs/1';
520546
doNock(hostname, testPath, 400, 'Not Ok');

0 commit comments

Comments
 (0)