Skip to content

Commit

Permalink
Port ignoreUrls tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Dec 25, 2024
1 parent 3a013bf commit c764f0d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1134,5 +1134,87 @@ describe('fetch', () => {
});
});
});

describe('ignoring requests', () => {
async function tracedFetch({
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,
}: {
handlers?: msw.RequestHandler[];
callback: () => Promise<Response>;
}): Promise<{ rootSpan: api.Span; response: Response }> {
let rootSpan: api.Span;
let response: Response | undefined;

await startWorker(...handlers);

rootSpan = await trace(
async () => {
response = await callback();
},
{ ignoreUrls: [/\/ignored\.json/] }
);

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'),
});

assert.strictEqual(exportedSpans.length, 0);
assertDebugMessage();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,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 @@ -965,11 +965,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

0 comments on commit c764f0d

Please sign in to comment.