Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ref(replay): Make some things public
  • Loading branch information
mydea committed Dec 9, 2022
commit 229bd5121a44d056bb82bab264276473db815850
3 changes: 1 addition & 2 deletions packages/replay/src/coreHandlers/handleFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export function handleFetch(handlerData: FetchHandlerData): null | ReplayPerform

export function handleFetchSpanListener(replay: ReplayContainer): (handlerData: FetchHandlerData) => void {
return (handlerData: FetchHandlerData) => {
// @ts-ignore private
if (!replay._isEnabled) {
if (!replay.isEnabled()) {
return;
}

Expand Down
11 changes: 3 additions & 8 deletions packages/replay/src/coreHandlers/handleGlobalEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even
// Collect traceIds in _context regardless of `_waitForError` - if it's true,
// _context gets cleared on every checkout
if (event.type === 'transaction') {
// @ts-ignore private
replay._context.traceIds.add(String(event.contexts?.trace?.trace_id || ''));
replay.getContext().traceIds.add(String(event.contexts?.trace?.trace_id || ''));
return event;
}

// XXX: Is it safe to assume that all other events are error events?
// @ts-ignore: Type 'undefined' is not assignable to type 'string'.ts(2345)
replay._context.errorIds.add(event.event_id);
replay.getContext().errorIds.add(event.event_id as string);

const exc = event.exception?.values?.[0];
addInternalBreadcrumb({
Expand All @@ -56,10 +54,7 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even
// than the session replay.
await replay.flushImmediate();

// @ts-ignore private
if (replay._stopRecording) {
// @ts-ignore private
replay._stopRecording();
if (replay.stopRecording()) {
// Reset all "capture on error" configuration before
// starting a new recording
// @ts-ignore private
Expand Down
6 changes: 2 additions & 4 deletions packages/replay/src/coreHandlers/handleHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ function handleHistory(handlerData: HistoryHandlerData): ReplayPerformanceEntry

export function handleHistorySpanListener(replay: ReplayContainer): (handlerData: HistoryHandlerData) => void {
return (handlerData: HistoryHandlerData) => {
// @ts-ignore private
if (!replay._isEnabled) {
if (!replay.isEnabled()) {
return;
}

Expand All @@ -36,8 +35,7 @@ export function handleHistorySpanListener(replay: ReplayContainer): (handlerData
}

// Need to collect visited URLs
// @ts-ignore private
replay._context.urls.push(result.name);
replay.getContext().urls.push(result.name);
Copy link
Member

Choose a reason for hiding this comment

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

Similar thing to above with trace/error ids.

replay.triggerUserActivity();

replay.addUpdate(() => {
Expand Down
3 changes: 1 addition & 2 deletions packages/replay/src/coreHandlers/handleXhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function handleXhr(handlerData: XhrHandlerData): ReplayPerformanceEntry | null {

export function handleXhrSpanListener(replay: ReplayContainer): (handlerData: XhrHandlerData) => void {
return (handlerData: XhrHandlerData) => {
// @ts-ignore private
if (!replay._isEnabled) {
if (!replay.isEnabled()) {
return;
}

Expand Down
23 changes: 23 additions & 0 deletions packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ export class ReplayContainer {
});
}

/** Get the event context. */
public getContext(): InternalEventContext {
return this._context;
}

/** If recording is currently enabled. */
public isEnabled(): boolean {
return this._isEnabled;
}

/**
* Initializes the plugin.
*
Expand Down Expand Up @@ -193,6 +203,19 @@ export class ReplayContainer {
}
}

/**
* Stops the recording, if it was running.
* Returns true if it was stopped, else false.
*/
public stopRecording(): boolean {
if (this._stopRecording) {
this._stopRecording();
return true;
}

return false;
}

/**
* Currently, this needs to be manually called (e.g. for tests). Sentry SDK
* does not support a teardown
Expand Down
10 changes: 4 additions & 6 deletions packages/replay/test/unit/index-handleGlobalEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ it('only tags errors with replay id, adds trace and error id to context for erro
}),
);

// @ts-ignore private
expect(replay._context.traceIds).toContain('trace_id');
// @ts-ignore private
expect(replay._context.errorIds).toContain('event_id');
expect(replay.getContext().traceIds).toContain('trace_id');
expect(replay.getContext().errorIds).toContain('event_id');

jest.runAllTimers();
await new Promise(process.nextTick); // wait for flush
Expand All @@ -96,7 +94,7 @@ it('strips out dropped events from errorIds', async () => {
const error3 = Error({ event_id: 'err3' });

// @ts-ignore private
overwriteRecordDroppedEvent(replay._context.errorIds);
overwriteRecordDroppedEvent(replay.getContext().errorIds);

const client = getCurrentHub().getClient()!;

Expand All @@ -107,7 +105,7 @@ it('strips out dropped events from errorIds', async () => {
client.recordDroppedEvent('before_send', 'error', { event_id: 'err2' });

// @ts-ignore private
expect(Array.from(replay._context.errorIds)).toEqual(['err1', 'err3']);
expect(Array.from(replay.getContext().errorIds)).toEqual(['err1', 'err3']);

restoreRecordDroppedEvent();
});
Expand Down
18 changes: 6 additions & 12 deletions packages/replay/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ describe('Replay', () => {
const initialSession = replay.session;

expect(initialSession?.id).toBeDefined();
// @ts-ignore private member
expect(replay._context).toEqual(
expect(replay.getContext()).toEqual(
expect.objectContaining({
initialUrl: 'http://localhost/',
initialTimestamp: BASE_TIMESTAMP,
Expand Down Expand Up @@ -423,8 +422,7 @@ describe('Replay', () => {
});

// `_context` should be reset when a new session is created
// @ts-ignore private member
expect(replay._context).toEqual(
expect(replay.getContext()).toEqual(
expect.objectContaining({
initialUrl: 'http://dummy/',
initialTimestamp: newTimestamp,
Expand All @@ -437,8 +435,7 @@ describe('Replay', () => {
const initialSession = replay.session;

expect(initialSession?.id).toBeDefined();
// @ts-ignore private member
expect(replay._context).toEqual(
expect(replay.getContext()).toEqual(
expect.objectContaining({
initialUrl: 'http://localhost/',
initialTimestamp: BASE_TIMESTAMP,
Expand Down Expand Up @@ -536,8 +533,7 @@ describe('Replay', () => {
});

// `_context` should be reset when a new session is created
// @ts-ignore private member
expect(replay._context).toEqual(
expect(replay.getContext()).toEqual(
expect.objectContaining({
initialUrl: 'http://dummy/',
initialTimestamp: newTimestamp,
Expand Down Expand Up @@ -855,8 +851,7 @@ describe('Replay', () => {
);

// This should be null because `addEvent` has not been called yet
// @ts-ignore private member
expect(replay._context.earliestEvent).toBe(null);
expect(replay.getContext().earliestEvent).toBe(null);
expect(mockTransportSend).toHaveBeenCalledTimes(0);

// A new checkout occurs (i.e. a new session was started)
Expand Down Expand Up @@ -896,8 +891,7 @@ describe('Replay', () => {
});

// This gets reset after sending replay
// @ts-ignore private member
expect(replay._context.earliestEvent).toBe(null);
expect(replay.getContext().earliestEvent).toBe(null);
});

it('has single flush when checkout flush and debounce flush happen near simultaneously', async () => {
Expand Down