Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set log level for Fetch/XHR breadcrumbs based on status code #13711

Merged
merged 9 commits into from
Sep 23, 2024
3 changes: 2 additions & 1 deletion packages/core/src/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Breadcrumb, BreadcrumbHint } from '@sentry/types';
import { consoleSandbox, dateTimestampInSeconds } from '@sentry/utils';
import { getClient, getIsolationScope } from './currentScopes';
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';

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

const timestamp = dateTimestampInSeconds();
const mergedBreadcrumb = { timestamp, ...breadcrumb };
const mergedBreadcrumb = { timestamp, ...assignBreadcrumbLogLevel(breadcrumb) };
const finalBreadcrumb = beforeBreadcrumb
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null)
: mergedBreadcrumb;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
import { dateTimestampInSeconds, generatePropagationContext, isPlainObject, logger, uuid4 } from '@sentry/utils';

import { updateSession } from './session';
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';
import { _getSpanForScope, _setSpanForScope } from './utils/spanOnScope';

/**
Expand Down Expand Up @@ -412,7 +413,7 @@ class ScopeClass implements ScopeInterface {

const mergedBreadcrumb = {
timestamp: dateTimestampInSeconds(),
...breadcrumb,
...assignBreadcrumbLogLevel(breadcrumb),
};

const breadcrumbs = this._breadcrumbs;
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/utils/breadcrumbsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Breadcrumb } from '@sentry/types';

/**
* Set a Fetch/XHR breadcrumb's log level based on the returned status code
* @param breadcrumb
*/
export function assignBreadcrumbLogLevel(breadcrumb: Breadcrumb): Breadcrumb {
const statusCode = breadcrumb.data?.status_code;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FetchBreadcrumbData and XhrBreadcrumbData are sent as the data property, which optionally contains status_code.

if (typeof statusCode !== 'number') {
return breadcrumb;
}

if (statusCode >= 400 && statusCode < 500) {
breadcrumb.level = 'warning';
} else if (statusCode >= 500) {
breadcrumb.level = 'error';
}

return breadcrumb;
}
4 changes: 2 additions & 2 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export interface Scope {
clear(): this;

/**
* Sets the breadcrumbs in the scope
* @param breadcrumbs Breadcrumb
* Sets the breadcrumb in the scope
* @param breadcrumb Breadcrumb
* @param maxBreadcrumbs number of max breadcrumbs to merged into event.
*/
addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this;
Expand Down
Loading