Skip to content

fix(replay): Streamline logger usage #6305

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 3 commits into from
Nov 28, 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
6 changes: 6 additions & 0 deletions packages/replay/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ export default async (): Promise<Config.InitialOptions> => {
setupFilesAfterEnv: ['./jest.setup.ts'],
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/test/**/*(*.)@(spec|test).ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.json',
},
__DEBUG_BUILD__: true,
},
};
};
14 changes: 7 additions & 7 deletions packages/replay/src/eventBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// TODO: figure out member access types and remove the line above

import { captureException } from '@sentry/core';
import { logger } from '@sentry/utils';

import { RecordingEvent, WorkerRequest, WorkerResponse } from './types';
import { logger } from './util/logger';
import workerString from './worker/worker.js';

interface CreateEventBufferParams {
Expand All @@ -17,7 +17,7 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
const workerUrl = URL.createObjectURL(workerBlob);

try {
logger.log('Using compression worker');
__DEBUG_BUILD__ && logger.log('[Replay] Using compression worker');
const worker = new Worker(workerUrl);
if (worker) {
return new EventBufferCompressionWorker(worker);
Expand All @@ -27,10 +27,10 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
} catch {
// catch and ignore, fallback to simple event buffer
}
logger.log('Falling back to simple event buffer');
__DEBUG_BUILD__ && logger.log('[Replay] Falling back to simple event buffer');
}

logger.log('Using simple buffer');
__DEBUG_BUILD__ && logger.log('[Replay] Using simple buffer');
return new EventBufferArray();
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export class EventBufferCompressionWorker implements IEventBuffer {

if (!data.success) {
// TODO: Do some error handling, not sure what
logger.error(data.response);
__DEBUG_BUILD__ && logger.error('[Replay]', data.response);

reject(new Error('Error in compression worker'));
return;
Expand All @@ -142,11 +142,11 @@ export class EventBufferCompressionWorker implements IEventBuffer {

init(): void {
this.postMessage({ id: this.id, method: 'init', args: [] });
logger.log('Initialized compression worker');
__DEBUG_BUILD__ && logger.log('[Replay] Initialized compression worker');
}

destroy(): void {
logger.log('Destroying compression worker');
__DEBUG_BUILD__ && logger.log('[Replay] Destroying compression worker');
this.worker?.terminate();
this.worker = null;
}
Expand Down
19 changes: 0 additions & 19 deletions packages/replay/src/flags.ts

This file was deleted.

27 changes: 13 additions & 14 deletions packages/replay/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */ // TODO: We might want to split this file up
import { addGlobalEventProcessor, getCurrentHub, Scope, setContext } from '@sentry/core';
import { Breadcrumb, Client, Event, Integration } from '@sentry/types';
import { addInstrumentationHandler, createEnvelope } from '@sentry/utils';
import { addInstrumentationHandler, createEnvelope, logger } from '@sentry/utils';
import debounce from 'lodash.debounce';
import { PerformanceObserverEntryList } from 'perf_hooks';
import { EventType, record } from 'rrweb';
Expand Down Expand Up @@ -40,7 +40,6 @@ import { createPayload } from './util/createPayload';
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries';
import { isExpired } from './util/isExpired';
import { isSessionExpired } from './util/isSessionExpired';
import { logger } from './util/logger';

/**
* Returns true to return control to calling function, otherwise continue with normal batching
Expand Down Expand Up @@ -300,7 +299,7 @@ export class Replay implements Integration {
emit: this.handleRecordingEmit,
});
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}
}
Expand All @@ -315,14 +314,14 @@ export class Replay implements Integration {
}

try {
logger.log('Stopping Replays');
__DEBUG_BUILD__ && logger.log('[Replay] Stopping Replays');
this.isEnabled = false;
this.removeListeners();
this.stopRecording?.();
this.eventBuffer?.destroy();
this.eventBuffer = null;
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}
}
Expand All @@ -340,7 +339,7 @@ export class Replay implements Integration {
this.stopRecording = undefined;
}
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}
}
Expand All @@ -361,7 +360,7 @@ export class Replay implements Integration {
deleteSession();
this.session = undefined;
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}
}
Expand Down Expand Up @@ -437,7 +436,7 @@ export class Replay implements Integration {
this.hasInitializedCoreListeners = true;
}
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}

Expand Down Expand Up @@ -487,7 +486,7 @@ export class Replay implements Integration {
this.performanceObserver = null;
}
} catch (err) {
logger.error(err);
__DEBUG_BUILD__ && logger.error('[Replay]', err);
captureInternalException(err);
}
}
Expand Down Expand Up @@ -599,7 +598,7 @@ export class Replay implements Integration {
) => {
// If this is false, it means session is expired, create and a new session and wait for checkout
if (!this.checkAndHandleExpiredSession()) {
logger.error(new Error('Received replay event after session expired.'));
__DEBUG_BUILD__ && logger.error('[Replay] Received replay event after session expired.');

return;
}
Expand Down Expand Up @@ -827,7 +826,7 @@ export class Replay implements Integration {
// If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
// ms, we will re-use the existing session, otherwise create a new
// session
logger.log('Document has become active, but session has expired');
__DEBUG_BUILD__ && logger.log('[Replay] Document has become active, but session has expired');
return;
}

Expand All @@ -841,7 +840,7 @@ export class Replay implements Integration {
* create a new Replay event.
*/
triggerFullSnapshot(): void {
logger.log('Taking full rrweb snapshot');
__DEBUG_BUILD__ && logger.log('[Replay] Taking full rrweb snapshot');
record.takeFullSnapshot(true);
}

Expand Down Expand Up @@ -1131,12 +1130,12 @@ export class Replay implements Integration {
}

if (!this.checkAndHandleExpiredSession()) {
logger.error(new Error('Attempting to finish replay event after session expired.'));
__DEBUG_BUILD__ && logger.error('[Replay] Attempting to finish replay event after session expired.');
return;
}

if (!this.session?.id) {
console.error(new Error('[Sentry]: No transaction, no replay'));
console.error('[Replay]: No transaction, no replay');
return;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/replay/src/session/createSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '@sentry/utils';

import { SessionOptions } from '../types';
import { logger } from '../util/logger';
import { saveSession } from './saveSession';
import { Session } from './Session';

Expand All @@ -15,7 +16,7 @@ export function createSession({ sessionSampleRate, errorSampleRate, stickySessio
sessionSampleRate,
});

logger.log(`Creating new session: ${session.id}`);
__DEBUG_BUILD__ && logger.log(`[Replay] Creating new session: ${session.id}`);

if (stickySession) {
saveSession(session);
Expand Down
5 changes: 3 additions & 2 deletions packages/replay/src/session/getSession.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from '@sentry/utils';

import { SessionOptions } from '../types';
import { isSessionExpired } from '../util/isSessionExpired';
import { logger } from '../util/logger';
import { createSession } from './createSession';
import { fetchSession } from './fetchSession';
import { Session } from './Session';
Expand Down Expand Up @@ -39,7 +40,7 @@ export function getSession({
if (!isExpired) {
return { type: 'saved', session };
} else {
logger.log('Session has expired');
__DEBUG_BUILD__ && logger.log('[Replay] Session has expired');
}
// Otherwise continue to create a new session
}
Expand Down
16 changes: 0 additions & 16 deletions packages/replay/src/util/logger.ts

This file was deleted.