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(core): Add replay_event type for events #6481

Merged
merged 4 commits into from
Jan 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ export function createEventEnvelope(
tunnel?: string,
): EventEnvelope {
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
const eventType = event.type || 'event';

/*
Note: Due to TS, event.type may be `replay_event`, theoretically.
In practice, we never call `createEventEnvelope` with `replay_event` type,
and we'd have to adjut a looot of types to make this work properly.
We want to avoid casting this around, as that could lead to bugs (e.g. when we add another type)
So the safe choice is to really guard against the replay_event type here.
*/
const eventType = event.type && event.type !== 'replay_event' ? event.type : 'event';
Copy link
Member Author

Choose a reason for hiding this comment

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

To note, this is technically correct, but may increase the bundle size slightly for "nothing", as this will not really be hit right now. I'd still rather leave this in and remove it once we (hopefully soon!) actually handle replay events "normally". WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me.


enhanceEventWithSdkInfo(event, metadata && metadata.sdk);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class Hub implements HubInterface {
*/
public captureEvent(event: Event, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
if (event.type !== 'transaction') {
if (!event.type) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Question: I guess replays should not count here? (Eventually - currently they do not go through this method at all)

Copy link
Member

Choose a reason for hiding this comment

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

Reading #3966, I think you're right.

Copy link
Member

Choose a reason for hiding this comment

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

We might also add a test for the new event type here:

test('transactions do not set lastEventId', () => {
const event: Event = {
extra: { b: 3 },
type: 'transaction',
};
const testClient = makeClient();
const hub = new Hub(testClient);
hub.captureEvent(event);
const args = getPassedArgs(testClient.captureEvent);
expect(args[1].event_id).not.toEqual(hub.lastEventId());
});

Copy link
Member Author

Choose a reason for hiding this comment

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

done in 37535a0

this._lastEventId = eventId;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/hub/test/hub.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable deprecation/deprecation */

import { Client, Event } from '@sentry/types';
import { Client, Event, EventType } from '@sentry/types';

import { getCurrentHub, Hub, Scope } from '../src';

Expand Down Expand Up @@ -358,10 +358,11 @@ describe('Hub', () => {
expect(args[1].event_id).toEqual(hub.lastEventId());
});

test('transactions do not set lastEventId', () => {
const eventTypesToIgnoreLastEventId: EventType[] = ['transaction', 'replay_event'];
it.each(eventTypesToIgnoreLastEventId)('eventType %s does not set lastEventId', eventType => {
const event: Event = {
extra: { b: 3 },
type: 'transaction',
type: eventType,
};
const testClient = makeClient();
const hub = new Hub(testClient);
Expand Down
7 changes: 2 additions & 5 deletions packages/replay/src/coreHandlers/handleGlobalEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import { isRrwebError } from '../util/isRrwebError';
export function handleGlobalEventListener(replay: ReplayContainer): (event: Event) => Event | null {
return (event: Event) => {
// Do not apply replayId to the root event
if (
// @ts-ignore new event type
event.type === REPLAY_EVENT_NAME
) {
if (event.type === REPLAY_EVENT_NAME) {
// Replays have separate set of breadcrumbs, do not include breadcrumbs
// from core SDK
delete event.breadcrumbs;
Expand All @@ -31,7 +28,7 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even

// Only tag transactions with replayId if not waiting for an error
// @ts-ignore private
if (event.type !== 'transaction' || replay.recordingMode === 'session') {
if (!event.type || replay.recordingMode === 'session') {
event.tags = { ...event.tags, replayId: replay.session?.id };
}

Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/datacategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type DataCategory =
| 'error'
// Transaction type event
| 'transaction'
// Replay type event
| 'replay_event'
// Events with `event_type` csp, hpkp, expectct, expectstaple
| 'security'
// Attachment bytes stored (unused for rate limiting
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface Event {
* Note that `ErrorEvent`s do not have a type (hence its undefined),
* while all other events are required to have one.
*/
export type EventType = 'transaction' | 'profile' | undefined;
export type EventType = 'transaction' | 'profile' | 'replay_event' | undefined;

export interface ErrorEvent extends Event {
type: undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type {
UserFeedbackItem,
} from './envelope';
export type { ExtendedError } from './error';
export type { Event, EventHint, ErrorEvent, TransactionEvent } from './event';
export type { Event, EventHint, EventType, ErrorEvent, TransactionEvent } from './event';
export type { EventProcessor } from './eventprocessor';
export type { Exception } from './exception';
export type { Extra, Extras } from './extra';
Expand Down