Skip to content

Commit

Permalink
Port ignoreUrls tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jan 13, 2025
1 parent 01c07b9 commit 549841b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ describe('fetch', () => {

const trace = async (
callback: () => Promise<void>,
config: FetchInstrumentationConfig = {}
config: FetchInstrumentationConfig = {},
expectExport = true
): Promise<api.Span> => {
try {
const contextManager = new ZoneContextManager().enable();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Response>;
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();
});
});
});
});

0 comments on commit 549841b

Please sign in to comment.