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

ref(replay): Send SDK version in Replay events #6814

Merged
merged 4 commits into from Jan 18, 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
5 changes: 1 addition & 4 deletions packages/replay/jest.setup.ts
@@ -1,13 +1,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { getCurrentHub } from '@sentry/core';
import type { ReplayRecordingData, Transport } from '@sentry/types';
import {TextEncoder} from 'util';
import { TextEncoder } from 'util';

import type { ReplayContainer, Session } from './src/types';

// @ts-ignore TS error, this is replaced in prod builds bc of rollup
global.__SENTRY_REPLAY_VERSION__ = 'version:Test';

(global as any).TextEncoder = TextEncoder;

type MockTransport = jest.MockedFunction<Transport['send']>;
Expand Down
14 changes: 0 additions & 14 deletions packages/replay/rollup.bundle.config.js
@@ -1,25 +1,11 @@
import replace from '@rollup/plugin-replace';

import { makeBaseBundleConfig, makeBundleConfigVariants } from '../../rollup/index.js';

import pkg from './package.json';

const baseBundleConfig = makeBaseBundleConfig({
bundleType: 'addon',
entrypoints: ['src/index.ts'],
jsVersion: 'es6',
licenseTitle: '@sentry/replay',
outputFileBase: () => 'bundles/replay',
packageSpecificConfig: {
plugins: [
replace({
preventAssignment: true,
values: {
__SENTRY_REPLAY_VERSION__: JSON.stringify(pkg.version),
},
}),
],
},
});

const builds = makeBundleConfigVariants(baseBundleConfig);
Expand Down
15 changes: 2 additions & 13 deletions packages/replay/rollup.npm.config.js
@@ -1,26 +1,15 @@
import replace from '@rollup/plugin-replace';

import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index';

import pkg from './package.json';

export default makeNPMConfigVariants(
makeBaseNPMConfig({
hasBundles: true,
packageSpecificConfig: {
plugins: [
// TODO: Remove this - replay version will be in sync w/ SDK version
replace({
preventAssignment: true,
values: {
__SENTRY_REPLAY_VERSION__: JSON.stringify(pkg.version),
},
}),
],
output: {
// set exports to 'named' or 'auto' so that rollup doesn't warn about
// the default export in `worker/worker.js`
exports: 'named',
// set preserveModules to false because for Replay we actually want
// to bundle everything into one file.
preserveModules: false,
},
},
Expand Down
4 changes: 0 additions & 4 deletions packages/replay/src/types.ts
Expand Up @@ -29,10 +29,6 @@ export interface WorkerRequest {
args: unknown[];
}

declare global {
const __SENTRY_REPLAY_VERSION__: string;
}

// PerformancePaintTiming and PerformanceNavigationTiming are only available with TS 4.4 and newer
// Therefore, we're exporting them here to make them available in older TS versions
export type PerformancePaintTiming = PerformanceEntry;
Expand Down
6 changes: 3 additions & 3 deletions packages/replay/src/util/prepareReplayEvent.ts
Expand Up @@ -30,12 +30,12 @@ export async function prepareReplayEvent({

// extract the SDK name because `client._prepareEvent` doesn't add it to the event
const metadata = client.getSdkMetadata && client.getSdkMetadata();
const name = (metadata && metadata.sdk && metadata.sdk.name) || 'sentry.javascript.unknown';
const { name, version } = (metadata && metadata.sdk) || {};

preparedEvent.sdk = {
...preparedEvent.sdk,
version: __SENTRY_REPLAY_VERSION__,
name,
name: name || 'sentry.javascript.unknown',
version: version || '0.0.0',
Copy link
Member

Choose a reason for hiding this comment

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

Just to be sure, version will already have been replaced somewhere else here, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, version is set automatically during SDK initialization:

options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.react',
packages: [
{
name: 'npm:@sentry/react',
version: SDK_VERSION,
},
],

The reason for providing the fallback here is that users can potentially delete it or use a custom or an old client that doesn't yet have getSdkMetadata.

};

return preparedEvent;
Expand Down
19 changes: 17 additions & 2 deletions packages/replay/test/unit/util/prepareReplayEvent.test.ts
Expand Up @@ -18,6 +18,19 @@ describe('Unit | util | prepareReplayEvent', () => {

client = hub.getClient()!;
scope = hub.getScope()!;

jest.spyOn(client, 'getSdkMetadata').mockImplementation(() => {
return {
sdk: {
name: 'sentry.javascript.testSdk',
version: '1.0.0',
},
};
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('works', async () => {
Expand All @@ -39,6 +52,8 @@ describe('Unit | util | prepareReplayEvent', () => {

const replayEvent = await prepareReplayEvent({ scope, client, replayId, event });

expect(client.getSdkMetadata).toHaveBeenCalledTimes(1);

expect(replayEvent).toEqual({
type: 'replay_event',
timestamp: 1670837008.634,
Expand All @@ -52,8 +67,8 @@ describe('Unit | util | prepareReplayEvent', () => {
event_id: 'replay-ID',
environment: 'production',
sdk: {
name: 'sentry.javascript.unknown',
version: 'version:Test',
name: 'sentry.javascript.testSdk',
version: '1.0.0',
},
sdkProcessingMetadata: {},
breadcrumbs: undefined,
Expand Down
14 changes: 0 additions & 14 deletions packages/tracing/rollup.bundle.config.js
@@ -1,9 +1,5 @@
import replace from '@rollup/plugin-replace';

import { makeBaseBundleConfig, makeBundleConfigVariants } from '../../rollup/index.js';

import pkg from './package.json';

const builds = [];

['es5', 'es6'].forEach(jsVersion => {
Expand All @@ -26,16 +22,6 @@ const replayBaseBundleConfig = makeBaseBundleConfig({
licenseTitle: '@sentry/tracing & @sentry/browser & @sentry/replay',
outputFileBase: () => 'bundles/bundle.tracing.replay',
includeReplay: true,
packageSpecificConfig: {
plugins: [
replace({
preventAssignment: true,
values: {
__SENTRY_REPLAY_VERSION__: JSON.stringify(pkg.version),
},
}),
],
},
});

builds.push(...makeBundleConfigVariants(replayBaseBundleConfig));
Expand Down