Skip to content

feat(core): Use the ignoreErrors logic for failed transactions #13084

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/core/src/tracing/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import {
addGlobalErrorInstrumentationHandler,
addGlobalUnhandledRejectionInstrumentationHandler,
logger,
stringMatchesSomePattern,
} from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { getActiveSpan, getRootSpan } from '../utils/spanUtils';
import { getClient } from '../currentScopes';
import { SPAN_STATUS_ERROR } from './spanstatus';
import { type HandlerDataError } from '@sentry/types';

let errorsInstrumented = false;

Expand All @@ -31,16 +36,45 @@ export function registerSpanErrorInstrumentation(): void {
/**
* If an error or unhandled promise occurs, we mark the active root span as failed
*/
function errorCallback(): void {
function errorCallback(error: any): void {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);
if (rootSpan) {
if (_isIgnoredError(error)) {
DEBUG_BUILD && logger.log('[Tracing] Root span: Global error occured then ignored');
return;
}

const message = 'internal_error';
DEBUG_BUILD && logger.log(`[Tracing] Root span: ${message} -> Global error occured`);
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message });
}
}

function _isIgnoredError(error: any): boolean {
if (!error) {
return false;
}

const errorMessage = _errorMessage(error);

const client = getClient();
if (!client) {
return false;
}

const options = client.getOptions();
if (!options) {
return false;
}

return stringMatchesSomePattern(errorMessage, options.ignoreErrors);
}

function _errorMessage(error: any): string {
return String((error as HandlerDataError).msg || (error as Error).message || error);
}

// The function name will be lost when bundling but we need to be able to identify this listener later to maintain the
// node.js default exit behaviour
errorCallback.tag = 'sentry_tracingErrorCallback';
23 changes: 22 additions & 1 deletion packages/core/test/lib/tracing/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('registerErrorHandlers()', () => {
beforeEach(() => {
mockAddGlobalErrorInstrumentationHandler.mockClear();
mockAddGlobalUnhandledRejectionInstrumentationHandler.mockClear();
const options = getDefaultTestClientOptions({ enableTracing: true });
const options = getDefaultTestClientOptions({
enableTracing: true,
ignoreErrors: ['entity not found', /some error that will not happen.*/],
});
const client = new TestClient(options);
setCurrentClient(client);
client.init();
Expand Down Expand Up @@ -76,4 +79,22 @@ describe('registerErrorHandlers()', () => {
expect(spanToJSON(span).status).toBe('internal_error');
});
});

it('does not set status for transaction on scope on an ignored error', () => {
registerSpanErrorInstrumentation();

startSpan({ name: 'test' }, span => {
mockErrorCallback({ msg: 'Database entity not found!' });
expect(spanToJSON(span).status).toBe(undefined);
});
});

it('does not set status for transaction on scope on unhandledrejection for an ignored error', () => {
registerSpanErrorInstrumentation();

startSpan({ name: 'test' }, span => {
mockUnhandledRejectionCallback('Database entity not found!');
expect(spanToJSON(span).status).toBe(undefined);
});
});
});