diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts index 66e15755b4..ce3e9096f3 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.old.ts @@ -961,7 +961,7 @@ describe('fetch', () => { }); }); - describe('when url is ignored', () => { + xdescribe('when url is ignored', () => { beforeEach(async () => { const propagateTraceHeaderCorsUrls = url; await prepareData(url, () => getData(url), { @@ -974,11 +974,11 @@ describe('fetch', () => { clearData(); }); - it('should NOT create any span', () => { + xit('should NOT create any span', () => { assert.strictEqual(exportSpy.args.length, 0, "span shouldn't b exported"); }); - it('should accept Request objects as argument (#2411)', async () => { + xit('should accept Request objects as argument (#2411)', async () => { const response = await window.fetch(new Request(url)); assert.ok(response instanceof Response); diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 28b5a623fe..370dd416ae 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -147,7 +147,8 @@ describe('fetch', () => { const trace = async ( callback: () => Promise, - config: FetchInstrumentationConfig = {} + config: FetchInstrumentationConfig = {}, + expectExport = true ): Promise => { try { const contextManager = new ZoneContextManager().enable(); @@ -177,12 +178,16 @@ describe('fetch', () => { setTimeout(resolve, 500); }); - // This isn't intended to be an invariant, but in the current setup we - // don't expect multiple exports, it's easier to assert and unwrap the - // array of arrays here, than have every single test deal with that - // downstream. - assert.strictEqual(dummySpanExporter.exported.length, 1); - exportedSpans = dummySpanExporter.exported[0]; + if (expectExport) { + // This isn't intended to be an invariant, but in the current setup we + // don't expect multiple exports, it's easier to assert and unwrap the + // array of arrays here, than have every single test deal with that + // downstream. + assert.strictEqual(dummySpanExporter.exported.length, 1); + exportedSpans = dummySpanExporter.exported[0]; + } else { + assert.strictEqual(dummySpanExporter.exported.length, 0); + } return rootSpan; } finally { @@ -1265,5 +1270,89 @@ describe('fetch', () => { }); }); }); + + describe('`ignoreUrls` config', () => { + const tracedFetch = async ({ + handlers = [ + msw.http.get('/api/ignored.json', () => { + return msw.HttpResponse.json({ ok: true }); + }), + msw.http.get('/api/not-ignored.json', () => { + return msw.HttpResponse.json({ ok: true }); + }), + ], + callback, + expectExport = true, + }: { + handlers?: msw.RequestHandler[]; + callback: () => Promise; + expectExport?: boolean; + }): Promise<{ rootSpan: api.Span; response: Response }> => { + let response: Response | undefined; + + await startWorker(...handlers); + + const rootSpan = await trace( + async () => { + response = await callback(); + }, + { ignoreUrls: [/\/ignored\.json/] }, + expectExport + ); + + assert.ok(response instanceof Response); + + return { rootSpan, response }; + }; + + let spyDebug: sinon.SinonSpy | undefined; + + beforeEach(async () => { + const logger = new api.DiagConsoleLogger(); + spyDebug = sinon.stub(logger, 'debug'); + api.diag.setLogger(logger, api.DiagLogLevel.ALL); + }); + + afterEach(() => { + api.diag.disable(); + spyDebug = undefined; + }); + + const assertNoDebugMessages = () => { + assert.ok(spyDebug); + sinon.assert.neverCalledWith( + spyDebug, + '@opentelemetry/instrumentation-fetch', + 'ignoring span as url matches ignored url' + ); + }; + + const assertDebugMessage = () => { + assert.ok(spyDebug); + sinon.assert.calledWith( + spyDebug, + '@opentelemetry/instrumentation-fetch', + 'ignoring span as url matches ignored url' + ); + }; + + it('should create spans for normal request', async () => { + await tracedFetch({ + callback: () => fetch('/api/not-ignored.json'), + }); + + assert.strictEqual(exportedSpans.length, 1); + assertNoDebugMessages(); + }); + + it('should not create any spans for ignored request', async () => { + await tracedFetch({ + callback: () => fetch('/api/ignored.json'), + expectExport: false, + }); + + assertDebugMessage(); + }); + }); }); });