diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts index ce76e1bed4..f1b6466e97 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts @@ -1194,7 +1194,7 @@ describe('fetch', () => { }); }); - describe('when network events are ignored', () => { + xdescribe('when network events are ignored', () => { beforeEach(async () => { await prepareData(url, () => getData(url), { ignoreNetworkEvents: true, @@ -1203,13 +1203,13 @@ describe('fetch', () => { afterEach(() => { clearData(); }); - it('should NOT add network events', () => { + xit('should NOT add network events', () => { const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; const events = span.events; assert.strictEqual(events.length, 0, 'number of events is wrong'); }); - it('should still add the CONTENT_LENGTH attribute', () => { + xit('should still add the CONTENT_LENGTH attribute', () => { const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; const attributes = span.attributes; const responseContentLength = attributes[ diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index f3dd3c3db5..e0b62329b5 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -1715,5 +1715,145 @@ describe('fetch', () => { }); }); }); + + describe('`ignoreNetworkEvents` config', () => { + const tracedFetch = async ({ + handlers = [ + msw.http.get('/api/status.json', () => { + return msw.HttpResponse.json({ ok: true }); + }), + ], + callback = () => fetch('/api/status.json'), + config = {}, + }: { + handlers?: msw.RequestHandler[]; + callback?: () => Promise; + config?: FetchInstrumentationConfig; + } = {}): Promise<{ rootSpan: api.Span; response: Response }> => { + let response: Response | undefined; + + await startWorker(...handlers); + + const rootSpan = await trace(async () => { + response = await callback(); + }, config); + + assert.ok(response instanceof Response); + assert.strictEqual(exportedSpans.length, 1); + + return { rootSpan, response }; + }; + + describe('when `ignoreNetworkEvents` is not set', function () { + let response: Response | undefined; + + beforeEach(async () => { + const result = await tracedFetch(); + response = result.response; + }); + + afterEach(() => { + response = undefined; + }); + + it('span should have correct events', async () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + const events = span.events; + 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, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); + }); + + it('span should have http.response_content_length attribute', () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + assert.strictEqual( + span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH], + parseInt(response!.headers.get('content-length')!), + `attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0` + ); + }); + }); + + describe('when `ignoreNetworkEvents` is `false`', function () { + let response: Response | undefined; + + beforeEach(async () => { + const result = await tracedFetch({ + config: { ignoreNetworkEvents: false }, + }); + response = result.response; + }); + + afterEach(() => { + response = undefined; + }); + + it('span should have correct events', async () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + const events = span.events; + 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, + PTN.RESPONSE_START, + PTN.RESPONSE_END, + ]); + }); + + it('span should have http.response_content_length attribute', () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + assert.strictEqual( + span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH], + parseInt(response!.headers.get('content-length')!), + `attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0` + ); + }); + }); + + describe('when `ignoreNetworkEvents` is `true`', function () { + let response: Response | undefined; + + beforeEach(async () => { + const result = await tracedFetch({ + config: { ignoreNetworkEvents: true }, + }); + response = result.response; + }); + + afterEach(() => { + response = undefined; + }); + + it('span should have no events', async () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + assert.strictEqual( + span.events.length, + 0, + 'should not have any events' + ); + }); + + it('span should have http.response_content_length attribute', () => { + const span: tracing.ReadableSpan = exportedSpans[0]; + assert.strictEqual( + span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH], + parseInt(response!.headers.get('content-length')!), + `attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0` + ); + }); + }); + }); }); });