Skip to content

Commit e70d1b3

Browse files
committed
fix(core): Set log level for Fetch/XHR breadcrumbs based on status code
Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent e0015c5 commit e70d1b3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

packages/core/src/breadcrumbs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Breadcrumb, BreadcrumbHint } from '@sentry/types';
22
import { consoleSandbox, dateTimestampInSeconds } from '@sentry/utils';
33
import { getClient, getIsolationScope } from './currentScopes';
4+
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';
45

56
/**
67
* Default maximum number of breadcrumbs added to an event. Can be overwritten
@@ -25,7 +26,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): vo
2526
if (maxBreadcrumbs <= 0) return;
2627

2728
const timestamp = dateTimestampInSeconds();
28-
const mergedBreadcrumb = { timestamp, ...breadcrumb };
29+
const mergedBreadcrumb = { timestamp, ...assignBreadcrumbLogLevel(breadcrumb) };
2930
const finalBreadcrumb = beforeBreadcrumb
3031
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null)
3132
: mergedBreadcrumb;

packages/core/src/scope.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
import { dateTimestampInSeconds, generatePropagationContext, isPlainObject, logger, uuid4 } from '@sentry/utils';
2525

2626
import { updateSession } from './session';
27+
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';
2728
import { _getSpanForScope, _setSpanForScope } from './utils/spanOnScope';
2829

2930
/**
@@ -412,7 +413,7 @@ class ScopeClass implements ScopeInterface {
412413

413414
const mergedBreadcrumb = {
414415
timestamp: dateTimestampInSeconds(),
415-
...breadcrumb,
416+
...assignBreadcrumbLogLevel(breadcrumb),
416417
};
417418

418419
const breadcrumbs = this._breadcrumbs;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Breadcrumb } from '@sentry/types';
2+
3+
/**
4+
* Set a Fetch/XHR breadcrumb's log level based on the returned status code
5+
* @param breadcrumb
6+
*/
7+
export function assignBreadcrumbLogLevel(breadcrumb: Breadcrumb): Breadcrumb {
8+
const statusCode = breadcrumb?.data?.status_code;
9+
if (typeof statusCode !== 'number') {
10+
return breadcrumb;
11+
}
12+
13+
if (statusCode >= 400 && statusCode < 500) {
14+
breadcrumb.level = 'warning';
15+
} else if (statusCode >= 500) {
16+
breadcrumb.level = 'error';
17+
}
18+
19+
return breadcrumb;
20+
}

0 commit comments

Comments
 (0)