Skip to content

Commit bedc385

Browse files
authored
feat(solidstart): Filter out low quality transactions for build assets (#13222)
1 parent e6642a7 commit bedc385

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

packages/solidstart/src/server/sdk.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { applySdkMetadata } from '@sentry/core';
22
import type { NodeClient, NodeOptions } from '@sentry/node';
33
import { init as initNodeSdk } from '@sentry/node';
4+
import { filterLowQualityTransactions } from './utils';
45

56
/**
67
* Initializes the server side of the Solid Start SDK
@@ -11,6 +12,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
1112
};
1213

1314
applySdkMetadata(opts, 'solidstart', ['solidstart', 'node']);
15+
filterLowQualityTransactions(opts);
1416

1517
return initNodeSdk(opts);
1618
}

packages/solidstart/src/server/utils.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { flush } from '@sentry/node';
1+
import { flush, getGlobalScope } from '@sentry/node';
2+
import type { EventProcessor, Options } from '@sentry/types';
23
import { logger } from '@sentry/utils';
34
import { DEBUG_BUILD } from '../common/debug-build';
45

@@ -31,3 +32,26 @@ export function isRedirect(error: unknown): boolean {
3132
const hasValidStatus = error.status >= 300 && error.status <= 308;
3233
return hasValidLocation && hasValidStatus;
3334
}
35+
36+
/**
37+
* Adds an event processor to filter out low quality transactions,
38+
* e.g. to filter out transactions for build assets
39+
*/
40+
export function filterLowQualityTransactions(options: Options): void {
41+
getGlobalScope().addEventProcessor(
42+
Object.assign(
43+
(event => {
44+
if (event.type !== 'transaction') {
45+
return event;
46+
}
47+
// Filter out transactions for build assets
48+
if (event.transaction?.match(/^GET \/_build\//)) {
49+
options.debug && logger.log('SolidStartLowQualityTransactionsFilter filtered transaction', event.transaction);
50+
return null;
51+
}
52+
return event;
53+
}) satisfies EventProcessor,
54+
{ id: 'SolidStartLowQualityTransactionsFilter' },
55+
),
56+
);
57+
}

packages/solidstart/test/server/sdk.test.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import type { NodeClient } from '@sentry/node';
12
import { SDK_VERSION } from '@sentry/node';
23
import * as SentryNode from '@sentry/node';
3-
4-
import { vi } from 'vitest';
4+
import { beforeEach, describe, expect, it, vi } from 'vitest';
55
import { init as solidStartInit } from '../../src/server';
66

77
const browserInit = vi.spyOn(SentryNode, 'init');
@@ -33,4 +33,38 @@ describe('Initialize Solid Start SDK', () => {
3333
expect(browserInit).toHaveBeenCalledTimes(1);
3434
expect(browserInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
3535
});
36+
37+
it('filters out low quality transactions', async () => {
38+
const beforeSendEvent = vi.fn(event => event);
39+
const client = solidStartInit({
40+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
41+
}) as NodeClient;
42+
client.on('beforeSendEvent', beforeSendEvent);
43+
44+
client.captureEvent({ type: 'transaction', transaction: 'GET /' });
45+
client.captureEvent({ type: 'transaction', transaction: 'GET /_build/some_asset.js' });
46+
client.captureEvent({ type: 'transaction', transaction: 'POST /_server' });
47+
48+
await client!.flush();
49+
50+
expect(beforeSendEvent).toHaveBeenCalledTimes(2);
51+
expect(beforeSendEvent).toHaveBeenCalledWith(
52+
expect.objectContaining({
53+
transaction: 'GET /',
54+
}),
55+
expect.any(Object),
56+
);
57+
expect(beforeSendEvent).not.toHaveBeenCalledWith(
58+
expect.objectContaining({
59+
transaction: 'GET /_build/some_asset.js',
60+
}),
61+
expect.any(Object),
62+
);
63+
expect(beforeSendEvent).toHaveBeenCalledWith(
64+
expect.objectContaining({
65+
transaction: 'POST /_server',
66+
}),
67+
expect.any(Object),
68+
);
69+
});
3670
});

packages/types/src/eventprocessor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Event, EventHint } from './event';
22

33
/**
4-
* Event processors are used to change the event before it will be send.
4+
* Event processors are used to change the event before it will be sent.
55
* We strongly advise to make this function sync.
66
* Returning a PromiseLike<Event | null> will work just fine, but better be sure that you know what you are doing.
77
* Event processing will be deferred until your Promise is resolved.

0 commit comments

Comments
 (0)