-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Set log level for Fetch/XHR breadcrumbs based on status code (#…
…13711) Fixes #13359 - [x] If you've added code that should be tested, please add tests. - [x] Ensure your code lints and the test suite passes (`yarn lint`) & (`yarn test`). --------- Signed-off-by: Kaung Zin Hein <kaungzinhein113@gmail.com> Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
- Loading branch information
1 parent
c0a5a3e
commit 1f898b6
Showing
14 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
...ges/browser-integration-tests/suites/integrations/Breadcrumbs/fetch/statusCode/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fetch('http://sentry-test.io/foo').then(() => { | ||
Sentry.captureException('test error'); | ||
}); |
71 changes: 71 additions & 0 deletions
71
...ckages/browser-integration-tests/suites/integrations/Breadcrumbs/fetch/statusCode/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('captures Breadcrumb with log level for 4xx response code', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 404, | ||
contentType: 'text/plain', | ||
body: 'Not Found!', | ||
}); | ||
}); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
|
||
expect(eventData?.breadcrumbs?.length).toBe(1); | ||
expect(eventData!.breadcrumbs![0]).toEqual({ | ||
timestamp: expect.any(Number), | ||
category: 'fetch', | ||
type: 'http', | ||
data: { | ||
method: 'GET', | ||
status_code: 404, | ||
url: 'http://sentry-test.io/foo', | ||
}, | ||
level: 'warning', | ||
}); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 500, | ||
contentType: 'text/plain', | ||
body: 'Internal Server Error', | ||
}); | ||
}); | ||
}); | ||
|
||
sentryTest('captures Breadcrumb with log level for 5xx response code', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 500, | ||
contentType: 'text/plain', | ||
body: 'Internal Server Error', | ||
}); | ||
}); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
|
||
expect(eventData?.breadcrumbs?.length).toBe(1); | ||
expect(eventData!.breadcrumbs![0]).toEqual({ | ||
timestamp: expect.any(Number), | ||
category: 'fetch', | ||
type: 'http', | ||
data: { | ||
method: 'GET', | ||
status_code: 500, | ||
url: 'http://sentry-test.io/foo', | ||
}, | ||
level: 'error', | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
...kages/browser-integration-tests/suites/integrations/Breadcrumbs/xhr/statusCode/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.open('GET', 'http://sentry-test.io/foo'); | ||
xhr.send(); | ||
|
||
xhr.addEventListener('readystatechange', function () { | ||
if (xhr.readyState === 4) { | ||
Sentry.captureException('test error'); | ||
} | ||
}); |
71 changes: 71 additions & 0 deletions
71
...packages/browser-integration-tests/suites/integrations/Breadcrumbs/xhr/statusCode/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('captures Breadcrumb with log level for 4xx response code', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 404, | ||
contentType: 'text/plain', | ||
body: 'Not Found!', | ||
}); | ||
}); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
|
||
expect(eventData?.breadcrumbs?.length).toBe(1); | ||
expect(eventData!.breadcrumbs![0]).toEqual({ | ||
timestamp: expect.any(Number), | ||
category: 'xhr', | ||
type: 'http', | ||
data: { | ||
method: 'GET', | ||
status_code: 404, | ||
url: 'http://sentry-test.io/foo', | ||
}, | ||
level: 'warning', | ||
}); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 500, | ||
contentType: 'text/plain', | ||
body: 'Internal Server Error', | ||
}); | ||
}); | ||
}); | ||
|
||
sentryTest('captures Breadcrumb with log level for 5xx response code', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.route('**/foo', async route => { | ||
await route.fulfill({ | ||
status: 500, | ||
contentType: 'text/plain', | ||
body: 'Internal Server Error', | ||
}); | ||
}); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
|
||
expect(eventData?.breadcrumbs?.length).toBe(1); | ||
expect(eventData!.breadcrumbs![0]).toEqual({ | ||
timestamp: expect.any(Number), | ||
category: 'xhr', | ||
type: 'http', | ||
data: { | ||
method: 'GET', | ||
status_code: 500, | ||
url: 'http://sentry-test.io/foo', | ||
}, | ||
level: 'error', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { SeverityLevel } from '@sentry/types'; | ||
|
||
/** | ||
* Determine a breadcrumb's log level (only `warning` or `error`) based on an HTTP status code. | ||
*/ | ||
export function getBreadcrumbLogLevelFromHttpStatusCode(statusCode: number | undefined): SeverityLevel | undefined { | ||
// NOTE: undefined defaults to 'info' in Sentry | ||
if (statusCode === undefined) { | ||
return undefined; | ||
} else if (statusCode >= 400 && statusCode < 500) { | ||
return 'warning'; | ||
} else if (statusCode >= 500) { | ||
return 'error'; | ||
} else { | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getBreadcrumbLogLevelFromHttpStatusCode } from '../src/breadcrumb-log-level'; | ||
|
||
describe('getBreadcrumbLogLevelFromHttpStatusCode()', () => { | ||
it.each([ | ||
['warning', '4xx', 403], | ||
['error', '5xx', 500], | ||
[undefined, '3xx', 307], | ||
[undefined, '2xx', 200], | ||
[undefined, '1xx', 103], | ||
[undefined, '0', 0], | ||
[undefined, 'undefined', undefined], | ||
])('should return `%s` for %s', (output, _codeRange, input) => { | ||
expect(getBreadcrumbLogLevelFromHttpStatusCode(input)).toEqual(output); | ||
}); | ||
}); |
Oops, something went wrong.