From 40be61ac803df96efb32dc6639be8ea8f2fd703a Mon Sep 17 00:00:00 2001 From: Abinet Date: Fri, 9 Jun 2023 10:57:10 -0700 Subject: [PATCH 1/5] fix: add secureConnectionStart to https only --- .../opentelemetry-sdk-trace-web/src/utils.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index 2027abc42f..7b38c886d6 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -85,7 +85,12 @@ export function addSpanNetworkEvents( addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_START, resource); addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_END, resource); addSpanNetworkEvent(span, PTN.CONNECT_START, resource); - addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource); + if ( + hasKey(resource as PerformanceResourceTiming, 'name') && + getProtocol((resource as PerformanceResourceTiming)['name']) === 'https' + ) { + addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource); + } addSpanNetworkEvent(span, PTN.CONNECT_END, resource); addSpanNetworkEvent(span, PTN.REQUEST_START, resource); addSpanNetworkEvent(span, PTN.RESPONSE_START, resource); @@ -441,3 +446,18 @@ export function shouldPropagateTraceHeaders( ); } } + +export function getURL(urlString: string | undefined) { + if (urlString) { + try { + return new URL(urlString); + } catch (e) { + return null; + } + } + return null; +} + +export function getProtocol(urlString: string | undefined) { + return getURL(urlString)?.protocol; +} From c83aaf9bd067443544a8efe54359d865483720c5 Mon Sep 17 00:00:00 2001 From: Abinet Date: Fri, 9 Jun 2023 11:35:10 -0700 Subject: [PATCH 2/5] fix: fix test --- .../opentelemetry-sdk-trace-web/test/utils.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-sdk-trace-web/test/utils.test.ts b/packages/opentelemetry-sdk-trace-web/test/utils.test.ts index 06e550c1f0..4db5fa55f1 100644 --- a/packages/opentelemetry-sdk-trace-web/test/utils.test.ts +++ b/packages/opentelemetry-sdk-trace-web/test/utils.test.ts @@ -116,9 +116,15 @@ describe('utils', () => { assert.strictEqual(addEventSpy.callCount, 0); addSpanNetworkEvents(span, entries); - - assert.strictEqual(addEventSpy.callCount, 9); assert.strictEqual(setAttributeSpy.callCount, 2); + //secure connect start should not be added to non-https resource + assert.strictEqual(addEventSpy.callCount, 8); + //secure connect start should be added to https resource + addSpanNetworkEvents(span, { + ...entries, + name: 'https://foo', + } as PerformanceResourceTiming); + assert.strictEqual(addEventSpy.callCount, 9); }); it('should only include encoded size when content encoding is being used', () => { const addEventSpy = sinon.spy(); From 611cc4d74503791c6020a3821c81df2879a7c375 Mon Sep 17 00:00:00 2001 From: Abinet Date: Fri, 9 Jun 2023 12:00:29 -0700 Subject: [PATCH 3/5] fix: typo and test fixes --- packages/opentelemetry-sdk-trace-web/src/utils.ts | 2 +- packages/opentelemetry-sdk-trace-web/test/utils.test.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index 7b38c886d6..6f2ba32660 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -87,7 +87,7 @@ export function addSpanNetworkEvents( addSpanNetworkEvent(span, PTN.CONNECT_START, resource); if ( hasKey(resource as PerformanceResourceTiming, 'name') && - getProtocol((resource as PerformanceResourceTiming)['name']) === 'https' + getProtocol((resource as PerformanceResourceTiming)['name']) === 'https:' ) { addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource); } diff --git a/packages/opentelemetry-sdk-trace-web/test/utils.test.ts b/packages/opentelemetry-sdk-trace-web/test/utils.test.ts index 4db5fa55f1..dd00e6b061 100644 --- a/packages/opentelemetry-sdk-trace-web/test/utils.test.ts +++ b/packages/opentelemetry-sdk-trace-web/test/utils.test.ts @@ -119,7 +119,8 @@ describe('utils', () => { assert.strictEqual(setAttributeSpy.callCount, 2); //secure connect start should not be added to non-https resource assert.strictEqual(addEventSpy.callCount, 8); - //secure connect start should be added to https resource + //secure connect start should be added to an https resource + addEventSpy.resetHistory(); addSpanNetworkEvents(span, { ...entries, name: 'https://foo', From 29f29a74d680171fcbd6e17ef71b783a15581eb5 Mon Sep 17 00:00:00 2001 From: Abinet18 <35442169+Abinet18@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:41:32 -0700 Subject: [PATCH 4/5] Update packages/opentelemetry-sdk-trace-web/src/utils.ts Co-authored-by: t2t2 --- packages/opentelemetry-sdk-trace-web/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index 6f2ba32660..87098c0f58 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -87,7 +87,7 @@ export function addSpanNetworkEvents( addSpanNetworkEvent(span, PTN.CONNECT_START, resource); if ( hasKey(resource as PerformanceResourceTiming, 'name') && - getProtocol((resource as PerformanceResourceTiming)['name']) === 'https:' + (resource as PerformanceResourceTiming)['name'].startsWith('https:') ) { addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource); } From 03abf7a339dd1ef628c3f53ecebd9debc2cf5167 Mon Sep 17 00:00:00 2001 From: Abinet Date: Wed, 14 Jun 2023 13:13:42 -0700 Subject: [PATCH 5/5] fix: fix xhr and fetch tests --- CHANGELOG.md | 1 + .../test/fetch.test.ts | 188 ++++++------- .../test/xhr.test.ts | 260 ++++++------------ .../opentelemetry-sdk-trace-web/src/utils.ts | 15 - 4 files changed, 173 insertions(+), 291 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f827e31585..bc1fa62cd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-trace-web): add secureConnectionStart to https only [#3879](https://github.com/open-telemetry/opentelemetry-js/pull/3879) @Abinet18 * fix(http-instrumentation): stop listening to `request`'s `close` event once it has emitted `response` [#3625](https://github.com/open-telemetry/opentelemetry-js/pull/3625) @SimenB * fix(sdk-node): fix initialization in bundled environments by not loading @opentelemetry/exporter-jaeger [#3739](https://github.com/open-telemetry/opentelemetry-js/pull/3739) @pichlermarc diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 318e0f98c3..14dcec9978 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -139,6 +139,19 @@ function createFakePerformanceObs(url: string) { return FakePerfObs; } +function testForCorrectEvents( + events: tracing.TimedEvent[], + eventNames: string[] +) { + for (let i = 0; i < events.length; i++) { + assert.strictEqual( + events[i].name, + eventNames[i], + `event ${eventNames[i]} is not defined` + ); + } +} + describe('fetch', () => { let contextManager: ZoneContextManager; let lastResponse: any | undefined; @@ -152,6 +165,7 @@ describe('fetch', () => { let fetchInstrumentation: FetchInstrumentation; const url = 'http://localhost:8090/get'; + const secureUrl = 'https://localhost:8090/get'; const badUrl = 'http://foo.bar.com/get'; const clearData = () => { @@ -399,53 +413,17 @@ describe('fetch', () => { it('span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; const events = span.events; - assert.strictEqual(events.length, 9, 'number of events is wrong'); - - assert.strictEqual( - events[0].name, + assert.strictEqual(events.length, 8, 'number of events is wrong'); + testForCorrectEvents(events, [ PTN.FETCH_START, - `event ${PTN.FETCH_START} is not defined` - ); - assert.strictEqual( - events[1].name, PTN.DOMAIN_LOOKUP_START, - `event ${PTN.DOMAIN_LOOKUP_START} is not defined` - ); - assert.strictEqual( - events[2].name, PTN.DOMAIN_LOOKUP_END, - `event ${PTN.DOMAIN_LOOKUP_END} is not defined` - ); - assert.strictEqual( - events[3].name, PTN.CONNECT_START, - `event ${PTN.CONNECT_START} is not defined` - ); - assert.strictEqual( - events[4].name, - PTN.SECURE_CONNECTION_START, - `event ${PTN.SECURE_CONNECTION_START} is not defined` - ); - assert.strictEqual( - events[5].name, PTN.CONNECT_END, - `event ${PTN.CONNECT_END} is not defined` - ); - assert.strictEqual( - events[6].name, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); - assert.strictEqual( - events[7].name, PTN.RESPONSE_START, - `event ${PTN.RESPONSE_START} is not defined` - ); - assert.strictEqual( - events[8].name, PTN.RESPONSE_END, - `event ${PTN.RESPONSE_END} is not defined` - ); + ]); }); it('should create a span for preflight request', () => { @@ -479,53 +457,17 @@ describe('fetch', () => { it('preflight request span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - assert.strictEqual(events.length, 9, 'number of events is wrong'); - - assert.strictEqual( - events[0].name, + assert.strictEqual(events.length, 8, 'number of events is wrong'); + testForCorrectEvents(events, [ PTN.FETCH_START, - `event ${PTN.FETCH_START} is not defined` - ); - assert.strictEqual( - events[1].name, PTN.DOMAIN_LOOKUP_START, - `event ${PTN.DOMAIN_LOOKUP_START} is not defined` - ); - assert.strictEqual( - events[2].name, PTN.DOMAIN_LOOKUP_END, - `event ${PTN.DOMAIN_LOOKUP_END} is not defined` - ); - assert.strictEqual( - events[3].name, PTN.CONNECT_START, - `event ${PTN.CONNECT_START} is not defined` - ); - assert.strictEqual( - events[4].name, - PTN.SECURE_CONNECTION_START, - `event ${PTN.SECURE_CONNECTION_START} is not defined` - ); - assert.strictEqual( - events[5].name, PTN.CONNECT_END, - `event ${PTN.CONNECT_END} is not defined` - ); - assert.strictEqual( - events[6].name, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); - assert.strictEqual( - events[7].name, PTN.RESPONSE_START, - `event ${PTN.RESPONSE_START} is not defined` - ); - assert.strictEqual( - events[8].name, PTN.RESPONSE_END, - `event ${PTN.RESPONSE_END} is not defined` - ); + ]); }); it('should set trace headers', () => { @@ -639,6 +581,51 @@ describe('fetch', () => { }); }); + describe('when request is secure and successful', () => { + beforeEach(async () => { + const propagateTraceHeaderCorsUrls = [secureUrl]; + await prepareData(secureUrl, { propagateTraceHeaderCorsUrls }); + }); + + afterEach(() => { + clearData(); + }); + + it('span should have correct events', () => { + const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; + const events = span.events; + assert.strictEqual(events.length, 9, 'number of events is wrong'); + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.SECURE_CONNECTION_START, + PTN.CONNECT_END, + PTN.REQUEST_START, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); + }); + + it('preflight request span should have correct events', () => { + const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; + const events = span.events; + assert.strictEqual(events.length, 9, 'number of events is wrong'); + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.SECURE_CONNECTION_START, + PTN.CONNECT_END, + PTN.REQUEST_START, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); + }); + }); + describe('applyCustomAttributesOnSpan option', () => { const prepare = async ( url: string, @@ -815,13 +802,18 @@ describe('fetch', () => { `Wrong number of spans: ${exportSpy.args.length}` ); - assert.strictEqual(events.length, 9, 'number of events is wrong'); + assert.strictEqual(events.length, 8, 'number of events is wrong'); - assert.strictEqual( - events[6].name, + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.CONNECT_END, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); }); }); @@ -844,12 +836,17 @@ describe('fetch', () => { `Wrong number of spans: ${exportSpy.args.length}` ); - assert.strictEqual(events.length, 9, 'number of events is wrong'); - assert.strictEqual( - events[6].name, + assert.strictEqual(events.length, 8, 'number of events is wrong'); + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.CONNECT_END, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); }); it('should have an absolute http.url attribute', () => { @@ -882,12 +879,17 @@ describe('fetch', () => { 2, `Wrong number of spans: ${exportSpy.args.length}` ); - assert.strictEqual(events.length, 9, 'number of events is wrong'); - assert.strictEqual( - events[6].name, + assert.strictEqual(events.length, 8, 'number of events is wrong'); + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.CONNECT_END, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); }); }); diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index b304bb9c03..c3614a42e8 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -162,6 +162,19 @@ function createFakePerformanceObs(url: string) { return FakePerfObs; } +function testForCorrectEvents( + events: tracing.TimedEvent[], + eventNames: string[] +) { + for (let i = 0; i < events.length; i++) { + assert.strictEqual( + events[i].name, + eventNames[i], + `event ${eventNames[i]} is not defined` + ); + } +} + describe('xhr', () => { const asyncTests = [{ async: true }, { async: false }]; asyncTests.forEach(test => { @@ -200,6 +213,7 @@ describe('xhr', () => { let rootSpan: api.Span; let spyEntries: any; const url = 'http://localhost:8090/xml-http-request.js'; + const secureUrl = 'https://localhost:8090/xml-http-request.js'; let fakeNow = 0; let xmlHttpRequestInstrumentation: XMLHttpRequestInstrumentation; @@ -383,69 +397,20 @@ describe('xhr', () => { it('span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; const events = span.events; - - assert.strictEqual( - events[0].name, + testForCorrectEvents(events, [ EventNames.METHOD_OPEN, - `event ${EventNames.METHOD_OPEN} is not defined` - ); - assert.strictEqual( - events[1].name, EventNames.METHOD_SEND, - `event ${EventNames.METHOD_SEND} is not defined` - ); - assert.strictEqual( - events[2].name, PTN.FETCH_START, - `event ${PTN.FETCH_START} is not defined` - ); - assert.strictEqual( - events[3].name, PTN.DOMAIN_LOOKUP_START, - `event ${PTN.DOMAIN_LOOKUP_START} is not defined` - ); - assert.strictEqual( - events[4].name, PTN.DOMAIN_LOOKUP_END, - `event ${PTN.DOMAIN_LOOKUP_END} is not defined` - ); - assert.strictEqual( - events[5].name, PTN.CONNECT_START, - `event ${PTN.CONNECT_START} is not defined` - ); - assert.strictEqual( - events[6].name, - PTN.SECURE_CONNECTION_START, - `event ${PTN.SECURE_CONNECTION_START} is not defined` - ); - assert.strictEqual( - events[7].name, PTN.CONNECT_END, - `event ${PTN.CONNECT_END} is not defined` - ); - assert.strictEqual( - events[8].name, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); - assert.strictEqual( - events[9].name, PTN.RESPONSE_START, - `event ${PTN.RESPONSE_START} is not defined` - ); - assert.strictEqual( - events[10].name, PTN.RESPONSE_END, - `event ${PTN.RESPONSE_END} is not defined` - ); - assert.strictEqual( - events[11].name, EventNames.EVENT_LOAD, - `event ${EventNames.EVENT_LOAD} is not defined` - ); - - assert.strictEqual(events.length, 12, 'number of events is wrong'); + ]); + assert.strictEqual(events.length, 11, 'number of events is wrong'); }); it('should create a span for preflight request', () => { @@ -479,53 +444,17 @@ describe('xhr', () => { it('preflight request span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - assert.strictEqual(events.length, 9, 'number of events is wrong'); - - assert.strictEqual( - events[0].name, + assert.strictEqual(events.length, 8, 'number of events is wrong'); + testForCorrectEvents(events, [ PTN.FETCH_START, - `event ${PTN.FETCH_START} is not defined` - ); - assert.strictEqual( - events[1].name, PTN.DOMAIN_LOOKUP_START, - `event ${PTN.DOMAIN_LOOKUP_START} is not defined` - ); - assert.strictEqual( - events[2].name, PTN.DOMAIN_LOOKUP_END, - `event ${PTN.DOMAIN_LOOKUP_END} is not defined` - ); - assert.strictEqual( - events[3].name, PTN.CONNECT_START, - `event ${PTN.CONNECT_START} is not defined` - ); - assert.strictEqual( - events[4].name, - PTN.SECURE_CONNECTION_START, - `event ${PTN.SECURE_CONNECTION_START} is not defined` - ); - assert.strictEqual( - events[5].name, PTN.CONNECT_END, - `event ${PTN.CONNECT_END} is not defined` - ); - assert.strictEqual( - events[6].name, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); - assert.strictEqual( - events[7].name, PTN.RESPONSE_START, - `event ${PTN.RESPONSE_START} is not defined` - ); - assert.strictEqual( - events[8].name, PTN.RESPONSE_END, - `event ${PTN.RESPONSE_END} is not defined` - ); + ]); }); it('should NOT clear the resources', () => { @@ -534,6 +463,53 @@ describe('xhr', () => { 'resources have been cleared' ); }); + describe('When making https requests', () => { + beforeEach(done => { + clearData(); + // this won't generate a preflight span + const propagateTraceHeaderCorsUrls = [secureUrl]; + prepareData(done, secureUrl, { + propagateTraceHeaderCorsUrls, + }); + }); + + it('span should have correct events', () => { + const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; + const events = span.events; + testForCorrectEvents(events, [ + EventNames.METHOD_OPEN, + EventNames.METHOD_SEND, + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.SECURE_CONNECTION_START, + PTN.CONNECT_END, + PTN.REQUEST_START, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + EventNames.EVENT_LOAD, + ]); + assert.strictEqual(events.length, 12, 'number of events is wrong'); + }); + + it('preflight request span should have correct events', () => { + const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; + const events = span.events; + assert.strictEqual(events.length, 9, 'number of events is wrong'); + testForCorrectEvents(events, [ + PTN.FETCH_START, + PTN.DOMAIN_LOOKUP_START, + PTN.DOMAIN_LOOKUP_END, + PTN.CONNECT_START, + PTN.SECURE_CONNECTION_START, + PTN.CONNECT_END, + PTN.REQUEST_START, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); + }); + }); describe('AND origin match with window.location', () => { beforeEach(done => { @@ -786,11 +762,11 @@ describe('xhr', () => { assert.strictEqual( events.length, - 12, + 11, `number of events is wrong: ${events.length}` ); assert.strictEqual( - events[8].name, + events[7].name, PTN.REQUEST_START, `event ${PTN.REQUEST_START} is not defined` ); @@ -1008,67 +984,20 @@ describe('xhr', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - assert.strictEqual( - events[0].name, + testForCorrectEvents(events, [ EventNames.METHOD_OPEN, - `event ${EventNames.METHOD_OPEN} is not defined` - ); - assert.strictEqual( - events[1].name, EventNames.METHOD_SEND, - `event ${EventNames.METHOD_SEND} is not defined` - ); - assert.strictEqual( - events[2].name, PTN.FETCH_START, - `event ${PTN.FETCH_START} is not defined` - ); - assert.strictEqual( - events[3].name, PTN.DOMAIN_LOOKUP_START, - `event ${PTN.DOMAIN_LOOKUP_START} is not defined` - ); - assert.strictEqual( - events[4].name, PTN.DOMAIN_LOOKUP_END, - `event ${PTN.DOMAIN_LOOKUP_END} is not defined` - ); - assert.strictEqual( - events[5].name, PTN.CONNECT_START, - `event ${PTN.CONNECT_START} is not defined` - ); - assert.strictEqual( - events[6].name, PTN.SECURE_CONNECTION_START, - `event ${PTN.SECURE_CONNECTION_START} is not defined` - ); - assert.strictEqual( - events[7].name, PTN.CONNECT_END, - `event ${PTN.CONNECT_END} is not defined` - ); - assert.strictEqual( - events[8].name, PTN.REQUEST_START, - `event ${PTN.REQUEST_START} is not defined` - ); - assert.strictEqual( - events[9].name, PTN.RESPONSE_START, - `event ${PTN.RESPONSE_START} is not defined` - ); - assert.strictEqual( - events[10].name, PTN.RESPONSE_END, - `event ${PTN.RESPONSE_END} is not defined` - ); - assert.strictEqual( - events[11].name, EventNames.EVENT_ERROR, - `event ${EventNames.EVENT_ERROR} is not defined` - ); - + ]); assert.strictEqual(events.length, 12, 'number of events is wrong'); }); }); @@ -1123,23 +1052,11 @@ describe('xhr', () => { it('span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - - assert.strictEqual( - events[0].name, + testForCorrectEvents(events, [ EventNames.METHOD_OPEN, - `event ${EventNames.METHOD_OPEN} is not defined` - ); - assert.strictEqual( - events[1].name, EventNames.METHOD_SEND, - `event ${EventNames.METHOD_SEND} is not defined` - ); - assert.strictEqual( - events[2].name, EventNames.EVENT_ERROR, - `event ${EventNames.EVENT_ERROR} is not defined` - ); - + ]); assert.strictEqual(events.length, 3, 'number of events is wrong'); }); }); @@ -1201,23 +1118,11 @@ describe('xhr', () => { it('span should have correct events', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - - assert.strictEqual( - events[0].name, + testForCorrectEvents(events, [ EventNames.METHOD_OPEN, - `event ${EventNames.METHOD_OPEN} is not defined` - ); - assert.strictEqual( - events[1].name, EventNames.METHOD_SEND, - `event ${EventNames.METHOD_SEND} is not defined` - ); - assert.strictEqual( - events[2].name, EventNames.EVENT_ABORT, - `event ${EventNames.EVENT_ABORT} is not defined` - ); - + ]); assert.strictEqual(events.length, 3, 'number of events is wrong'); }); }); @@ -1280,22 +1185,11 @@ describe('xhr', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; const events = span.events; - assert.strictEqual( - events[0].name, + testForCorrectEvents(events, [ EventNames.METHOD_OPEN, - `event ${EventNames.METHOD_OPEN} is not defined` - ); - assert.strictEqual( - events[1].name, EventNames.METHOD_SEND, - `event ${EventNames.METHOD_SEND} is not defined` - ); - assert.strictEqual( - events[2].name, EventNames.EVENT_TIMEOUT, - `event ${EventNames.EVENT_TIMEOUT} is not defined` - ); - + ]); assert.strictEqual(events.length, 3, 'number of events is wrong'); }); }); diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index 87098c0f58..b3f583676f 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -446,18 +446,3 @@ export function shouldPropagateTraceHeaders( ); } } - -export function getURL(urlString: string | undefined) { - if (urlString) { - try { - return new URL(urlString); - } catch (e) { - return null; - } - } - return null; -} - -export function getProtocol(urlString: string | undefined) { - return getURL(urlString)?.protocol; -}