Skip to content
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
43 changes: 39 additions & 4 deletions packages/core/src/integrations/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import type { EventItem, IntegrationFn } from '@sentry/types';
import type { EventItem, Exception, IntegrationFn } from '@sentry/types';
import { forEachEnvelopeItem } from '@sentry/utils';
import { defineIntegration } from '../integration';

import { addMetadataToStackFrames, stripMetadataFromStackFrames } from '../metadata';

const INTEGRATION_NAME = 'ModuleMetadata';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ModuleMetadata = any;

const _moduleMetadataIntegration = (() => {
function getAllModuleMetadata(exceptions: Exception[]): ModuleMetadata[] {
return exceptions.reduce(
(acc, exception) => {
if (exception.stacktrace && exception.stacktrace.frames) {
acc.push(...exception.stacktrace.frames.map(frame => frame.module_metadata));
}
return acc;
},
[] as ModuleMetadata[],
);
}

interface Options {
dropEvent?: {
/**
* Drop event if no stack frames have matching metadata
*/
ifNoStackFrameMetadataMatches?: (metadata: ModuleMetadata | undefined) => boolean;
};
}

const _moduleMetadataIntegration = ((options: Options = {}) => {
return {
name: INTEGRATION_NAME,
name: 'ModuleMetadata',
setup(client) {
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only.
client.on('beforeEnvelope', envelope => {
Expand All @@ -28,6 +50,19 @@ const _moduleMetadataIntegration = (() => {
processEvent(event, _hint, client) {
const stackParser = client.getOptions().stackParser;
addMetadataToStackFrames(stackParser, event);

if (
event.exception &&
event.exception.values &&
options.dropEvent &&
options.dropEvent.ifNoStackFrameMetadataMatches
) {
const metadata = getAllModuleMetadata(event.exception.values);
if (!metadata.some(options.dropEvent.ifNoStackFrameMetadataMatches)) {
return null;
}
}

return event;
},
};
Expand Down
63 changes: 63 additions & 0 deletions packages/core/test/lib/integrations/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,67 @@ describe('ModuleMetadata integration', () => {

captureException(new Error('Some error'));
});

test('Drops event if no stack frames have matching metadata', done => {
expect.assertions(0);

const options = getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
stackParser,
integrations: [
moduleMetadataIntegration({ dropEvent: { ifNoStackFrameMetadataMatches: m => m?.team === 'backend' } }),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
moduleMetadataIntegration({ dropEvent: { ifNoStackFrameMetadataMatches: m => m?.team === 'backend' } }),
moduleMetadataIntegration({ dropEvent: { ifNoStackFrameMetadataMatches: m => m.team === 'backend' } }),

or can m be undefined here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've changed the types to reflect that the parameter can be undefined. This allows users to filter on whether the metadata for a frame is defined!

],
transport: () =>
createTransport({ recordDroppedEvent: () => undefined }, async req => {
expect(req.body).toBeUndefined();
done();
return {};
}),
});

const client = new TestClient(options);
setCurrentClient(client);
client.init();

captureException(new Error('Some error'));

setTimeout(() => {
done();
}, 2000);
});

test('Sends event if stack frames have matching metadata', done => {
expect.assertions(1);

let callbackCalled = false;

const options = getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
stackParser,
integrations: [
moduleMetadataIntegration({
dropEvent: {
ifNoStackFrameMetadataMatches: m => {
callbackCalled = true;
return m?.team === 'frontend';
},
},
}),
],
transport: () =>
createTransport({ recordDroppedEvent: () => undefined }, async _ => {
expect(callbackCalled).toBe(true);
done();
return {};
}),
});

const client = new TestClient(options);
setCurrentClient(client);
client.init();

captureException(new Error('Some error'));
});
});