Skip to content

feat(core): Pass event as third argument to recordDroppedEvent #6289

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

Merged
merged 1 commit into from
Nov 25, 2022
Merged
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
10 changes: 6 additions & 4 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/**
* @inheritDoc
*/
public recordDroppedEvent(reason: EventDropReason, category: DataCategory): void {
public recordDroppedEvent(reason: EventDropReason, category: DataCategory, _event?: Event): void {
// Note: we use `event` in replay, where we overwrite this hook.

if (this._options.sendClientReports) {
// We want to track each category (error, transaction, session) separately
// but still keep the distinction between different type of outcomes.
Expand Down Expand Up @@ -632,7 +634,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
// 0.0 === 0% events are sent
// Sampling for transaction happens somewhere else
if (!isTransaction && typeof sampleRate === 'number' && Math.random() > sampleRate) {
this.recordDroppedEvent('sample_rate', 'error');
this.recordDroppedEvent('sample_rate', 'error', event);
return rejectedSyncPromise(
new SentryError(
`Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`,
Expand All @@ -644,7 +646,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
return this._prepareEvent(event, hint, scope)
.then(prepared => {
if (prepared === null) {
this.recordDroppedEvent('event_processor', event.type || 'error');
this.recordDroppedEvent('event_processor', event.type || 'error', event);
throw new SentryError('An event processor returned `null`, will not send event.', 'log');
}

Expand All @@ -658,7 +660,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
})
.then(processedEvent => {
if (processedEvent === null) {
this.recordDroppedEvent('before_send', event.type || 'error');
this.recordDroppedEvent('before_send', event.type || 'error', event);
throw new SentryError(`\`${beforeSendProcessorName}\` returned \`null\`, will not send event.`, 'log');
}

Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/transports/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
Envelope,
EnvelopeItem,
EnvelopeItemType,
Event,
EventDropReason,
EventItem,
InternalBaseTransportOptions,
Transport,
TransportRequestExecutor,
Expand Down Expand Up @@ -45,7 +48,8 @@ export function createTransport(
forEachEnvelopeItem(envelope, (item, type) => {
const envelopeItemDataCategory = envelopeItemTypeToDataCategory(type);
if (isRateLimited(rateLimits, envelopeItemDataCategory)) {
options.recordDroppedEvent('ratelimit_backoff', envelopeItemDataCategory);
const event: Event | undefined = getEventForEnvelopeItem(item, type);
options.recordDroppedEvent('ratelimit_backoff', envelopeItemDataCategory, event);
} else {
filteredEnvelopeItems.push(item);
}
Expand All @@ -61,8 +65,9 @@ export function createTransport(

// Creates client report for each item in an envelope
const recordEnvelopeLoss = (reason: EventDropReason): void => {
forEachEnvelopeItem(filteredEnvelope, (_, type) => {
options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type));
forEachEnvelopeItem(filteredEnvelope, (item, type) => {
const event: Event | undefined = getEventForEnvelopeItem(item, type);
options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type), event);
});
};

Expand Down Expand Up @@ -101,3 +106,11 @@ export function createTransport(
flush,
};
}

function getEventForEnvelopeItem(item: Envelope[1][number], type: EnvelopeItemType): Event | undefined {
if (type !== 'event' && type !== 'transaction') {
return undefined;
}

return Array.isArray(item) ? (item as EventItem)[1] : undefined;
}
22 changes: 17 additions & 5 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,9 @@ describe('BaseClient', () => {
client.captureEvent({ message: 'hello' }, {});

expect(beforeSend).toHaveBeenCalled();
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'error');
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'error', {
message: 'hello',
});
});

test('`beforeSendTransaction` records dropped events', () => {
Expand All @@ -1257,7 +1259,10 @@ describe('BaseClient', () => {
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });

expect(beforeSendTransaction).toHaveBeenCalled();
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'transaction');
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'transaction', {
transaction: '/dogs/are/great',
type: 'transaction',
});
});

test('event processor drops error event when it returns `null`', () => {
Expand Down Expand Up @@ -1309,7 +1314,9 @@ describe('BaseClient', () => {

client.captureEvent({ message: 'hello' }, {}, scope);

expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'error');
expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'error', {
message: 'hello',
});
});

test('event processor records dropped transaction events', () => {
Expand All @@ -1325,7 +1332,10 @@ describe('BaseClient', () => {

client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' }, {}, scope);

expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'transaction');
expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'transaction', {
transaction: '/dogs/are/great',
type: 'transaction',
});
});

test('mutating transaction name with event processors sets transaction-name-change metadata', () => {
Expand Down Expand Up @@ -1434,7 +1444,9 @@ describe('BaseClient', () => {
const recordLostEventSpy = jest.spyOn(client, 'recordDroppedEvent');

client.captureEvent({ message: 'hello' }, {});
expect(recordLostEventSpy).toHaveBeenCalledWith('sample_rate', 'error');
expect(recordLostEventSpy).toHaveBeenCalledWith('sample_rate', 'error', {
message: 'hello',
});
});
});

Expand Down
56 changes: 48 additions & 8 deletions packages/core/test/lib/transports/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEnvelope, EventItem, TransportMakeRequestResponse } from '@sentry/types';
import { AttachmentItem, EventEnvelope, EventItem, TransportMakeRequestResponse } from '@sentry/types';
import { createEnvelope, PromiseBuffer, resolvedSyncPromise, serializeEnvelope } from '@sentry/utils';
import { TextEncoder } from 'util';

Expand All @@ -13,6 +13,22 @@ const TRANSACTION_ENVELOPE = createEnvelope<EventEnvelope>(
[[{ type: 'transaction' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem],
);

const ATTACHMENT_ENVELOPE = createEnvelope<EventEnvelope>(
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
[
[
{
type: 'attachment',
length: 20,
filename: 'test-file.txt',
content_type: 'text/plain',
attachment_type: 'text',
},
'attachment content',
] as AttachmentItem,
],
);

const transportOptions = {
recordDroppedEvent: () => undefined, // noop
textEncoder: new TextEncoder(),
Expand Down Expand Up @@ -122,7 +138,9 @@ describe('createTransport', () => {

await transport.send(ERROR_ENVELOPE);
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

Expand Down Expand Up @@ -164,7 +182,9 @@ describe('createTransport', () => {

await transport.send(ERROR_ENVELOPE); // Error envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

Expand All @@ -186,7 +206,7 @@ describe('createTransport', () => {
const { retryAfterSeconds, beforeLimit, withinLimit, afterLimit } = setRateLimitTimes();
const [transport, setTransportResponse, requestExecutor, recordDroppedEventCallback] = createTestTransport({
headers: {
'x-sentry-rate-limits': `${retryAfterSeconds}:error;transaction:scope`,
'x-sentry-rate-limits': `${retryAfterSeconds}:error;transaction;attachment:scope`,
'retry-after': null,
},
});
Expand All @@ -206,13 +226,23 @@ describe('createTransport', () => {

await transport.send(TRANSACTION_ENVELOPE); // Transaction envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'transaction');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'transaction', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

await transport.send(ERROR_ENVELOPE); // Error envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

await transport.send(ATTACHMENT_ENVELOPE); // Attachment envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'attachment', undefined);
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

Expand All @@ -228,6 +258,12 @@ describe('createTransport', () => {
await transport.send(ERROR_ENVELOPE);
expect(requestExecutor).toHaveBeenCalledTimes(1);
expect(recordDroppedEventCallback).not.toHaveBeenCalled();
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

await transport.send(ATTACHMENT_ENVELOPE);
expect(requestExecutor).toHaveBeenCalledTimes(1);
expect(recordDroppedEventCallback).not.toHaveBeenCalled();
});

it('back-off using X-Sentry-Rate-Limits with missing categories should lock them all', async () => {
Expand All @@ -254,13 +290,17 @@ describe('createTransport', () => {

await transport.send(TRANSACTION_ENVELOPE); // Transaction envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'transaction');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'transaction', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

await transport.send(ERROR_ENVELOPE); // Error envelope should not be sent because of pending rate limit
expect(requestExecutor).not.toHaveBeenCalled();
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error');
expect(recordDroppedEventCallback).toHaveBeenCalledWith('ratelimit_backoff', 'error', {
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
});
requestExecutor.mockClear();
recordDroppedEventCallback.mockClear();

Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface Client<O extends ClientOptions = ClientOptions> {
*
* @param reason The reason why the event got dropped.
* @param category The data category of the dropped event.
* @param event The dropped event.
*/
recordDroppedEvent(reason: EventDropReason, category: DataCategory): void;
recordDroppedEvent(reason: EventDropReason, dataCategory: DataCategory, event?: Event): void;
}
5 changes: 2 additions & 3 deletions packages/types/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventDropReason } from './clientreport';
import { DataCategory } from './datacategory';
import { Client } from './client';
import { Envelope } from './envelope';
import { TextEncoderInternal } from './textencoder';

Expand All @@ -18,7 +17,7 @@ export type TransportMakeRequestResponse = {

export interface InternalBaseTransportOptions {
bufferSize?: number;
recordDroppedEvent: (reason: EventDropReason, dataCategory: DataCategory) => void;
recordDroppedEvent: Client['recordDroppedEvent'];
textEncoder?: TextEncoderInternal;
}

Expand Down