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

Added a check for spotlight v1.x.x to not load sentry sdk >= v8 #401

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
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 .changeset/cold-kings-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@spotlightjs/spotlight': patch
'@spotlightjs/overlay': patch
---

added a check for spotlight v1.x.x to not load sentry sdk >= v8
5 changes: 5 additions & 0 deletions .changeset/ninety-days-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/overlay': patch
---

updated the logic to retrieve the version of browser sentry sdk.
6 changes: 5 additions & 1 deletion packages/overlay/src/components/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export default function Overview({
<Tabs tabs={tabs} setOpen={setOpen} />
<div className="flex-1 overflow-auto overflow-x-hidden">
<Routes>
<Route path="/not-found" element={<p>Not Found - How'd you manage to get here?</p>} key={'not-found'}></Route>
<Route
path="/not-found"
element={<div className="text-primary-300 p-6">Not Found - How'd you manage to get here?</div>}
key={'not-found'}
></Route>
{tabs.map(tab => {
const TabContent = tab.content && tab.content;

Expand Down
2 changes: 2 additions & 0 deletions packages/overlay/src/integrations/sentry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,5 @@ export const PERFORMANCE_SCORE_PROFILES = {
// }
],
};

export const SDK_VERSION_SUPPORT_REGEX = /^[0-7]\.\d+\.\d+$/;
66 changes: 44 additions & 22 deletions packages/overlay/src/integrations/sentry/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { Envelope } from '@sentry/types';
import { getSpotlightEventTarget } from '../../lib/eventTarget';
import { log } from '../../lib/logger';
import { log, warn } from '../../lib/logger';
import type { Integration, RawEventContext } from '../integration';
import sentryDataCache from './data/sentryDataCache';
import { Spotlight } from './sentry-integration';
import DeveloperInfo from './tabs/DeveloperInfo';
import ErrorsTab from './tabs/ErrorsTab';
import InfoTab from './tabs/InfoTab';
import PerformanceTab from './tabs/PerformanceTab';
import SdksTab from './tabs/SdksTab';
import TracesTab from './tabs/TracesTab';
import { LegacyCarrier, WindowWithSentry } from './types';
import { checkBrowserSDKVersion } from './utils/sdkValidation';

const HEADER = 'application/x-sentry-envelope';

Expand All @@ -17,7 +20,7 @@ type SentryIntegrationOptions = {
injectIntoSDK?: boolean;
};

export default function sentryIntegration(options?: SentryIntegrationOptions) {
function _sentryIntegration(options?: SentryIntegrationOptions) {
return {
name: 'sentry',
forwardedContentType: [HEADER],
Expand Down Expand Up @@ -96,19 +99,36 @@ export default function sentryIntegration(options?: SentryIntegrationOptions) {
} satisfies Integration<Envelope>;
}

type WindowWithSentry = Window & {
__SENTRY__?: {
hub: {
getClient: () =>
| {
setupIntegrations: (force: boolean) => void;
addIntegration(integration: Integration): void;
on: (event: string, callback: (envelope: Envelope) => void) => void;
}
| undefined;
};
};
};
function _fallbackSentryIntegration() {
return {
name: 'sentry',
forwardedContentType: [],
setup: () => {},
processEvent: () => undefined,
tabs: () => {
return [
{
id: 'info',
title: 'Info',
content: () =>
InfoTab({
info: 'This version of Spotlight supports Sentry browser SDK version less than 8.x.x. Check out Spotlight v2.x.x',
}),
},
];
},
reset: () => {},
} satisfies Integration<Envelope>;
}

export default function sentryIntegration(options?: SentryIntegrationOptions) {
if (checkBrowserSDKVersion()) {
return _sentryIntegration(options);
} else {
warn(`This version of Spotlight only supports Sentry browser SDK versions below 8.x.x. Check out Spotlight v2.x.x`);
return _fallbackSentryIntegration();
}
}

export function processEnvelope(rawEvent: RawEventContext) {
const { data } = rawEvent;
Expand Down Expand Up @@ -150,12 +170,14 @@ function addSpotlightIntegrationToSentry(options?: SentryIntegrationOptions) {
if (options?.injectIntoSDK === false) return;
// A very hacky way to hook into Sentry's SDK
// but we love hacks
const sentryHub = (window as WindowWithSentry).__SENTRY__?.hub;
const sentryClient = sentryHub?.getClient();
if (sentryClient) {
const spotlightIntegration = new Spotlight({
sidecarUrl: options?.sidecarUrl,
});
sentryClient.addIntegration(spotlightIntegration);
const sentryHub = ((window as WindowWithSentry).__SENTRY__ as LegacyCarrier)?.hub;
if (sentryHub) {
const sentryClient = sentryHub?.getClient();
if (sentryClient) {
const spotlightIntegration = new Spotlight({
sidecarUrl: options?.sidecarUrl,
});
sentryClient.addIntegration(spotlightIntegration);
}
}
}
3 changes: 3 additions & 0 deletions packages/overlay/src/integrations/sentry/tabs/InfoTab.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function InfoTab({ info }: { info?: string }) {
return <div className="text-primary-300 p-6">{info}</div>;
}
37 changes: 36 additions & 1 deletion packages/overlay/src/integrations/sentry/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Measurements } from '@sentry/types';
import { Client, Envelope, Measurements } from '@sentry/types';
import { Integration } from '../integration';

export type FrameVars = {
[key: string]: string;
Expand Down Expand Up @@ -212,3 +213,37 @@ export type MetricWeightsProps = {
fid: number;
ttfb: number;
};

type V8Carrier = {
stack: {
getScope?: () => {
getClient?: () => Client | undefined;
};
};
};

export type LegacyCarrier = {
/** pre-v8 way of accessing client (v7 and earlier) */
hub: {
getClient: () =>
| {
setupIntegrations: (force: boolean) => void;
addIntegration(integration: Integration): void;
on: (event: string, callback: (envelope: Envelope) => void) => void;
getOptions: () => {
_metadata: {
sdk: {
version: string;
};
};
};
}
| undefined;
};
};

export type VersionedCarrier = { version: string } & Record<Exclude<string, 'version'>, V8Carrier>;

export type WindowWithSentry = Window & {
__SENTRY__?: LegacyCarrier | VersionedCarrier;
};
15 changes: 15 additions & 0 deletions packages/overlay/src/integrations/sentry/utils/sdkValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SDK_VERSION_SUPPORT_REGEX } from '../constants';
import { LegacyCarrier, VersionedCarrier, WindowWithSentry } from '../types';

export function checkBrowserSDKVersion() {
const sentrySDKVersionPreV8 = ((window as WindowWithSentry).__SENTRY__ as LegacyCarrier)?.hub
?.getClient()
?.getOptions()?._metadata?.sdk?.version;

const sentrySDKVersionPostV8 = ((window as WindowWithSentry).__SENTRY__ as VersionedCarrier)?.version;

// if browser SDK verion not found, return true to load the events from server side.
return sentrySDKVersionPreV8 || sentrySDKVersionPostV8
? new RegExp(SDK_VERSION_SUPPORT_REGEX).test(sentrySDKVersionPreV8 || sentrySDKVersionPostV8)
: true;
}
18 changes: 18 additions & 0 deletions packages/overlay/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ export function log(...args: unknown[]) {
console.log('🔎 [Spotlight]', ...args);
}
}

export function warn(...args: unknown[]) {
if (loggerActive) {
console.warn('🔎 [Spotlight]', ...args);
}
}

export function info(...args: unknown[]) {
if (loggerActive) {
console.info('🔎 [Spotlight]', ...args);
}
}

export function error(...args: unknown[]) {
if (loggerActive) {
console.error('🔎 [Spotlight]', ...args);
}
}
6 changes: 3 additions & 3 deletions packages/website/src/components/InstallCommand.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs>
<TabItem label="npm">
```sh
npm add @spotlightjs/spotlight
npm i @spotlightjs/spotlight --save-dev
Copy link
Member

Choose a reason for hiding this comment

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

good catch!

```
</TabItem>
<TabItem label="pnpm">
```sh
pnpm add @spotlightjs/spotlight
pnpm add -D @spotlightjs/spotlight
```
</TabItem>
<TabItem label="yarn">
```sh
yarn add @spotlightjs/spotlight
yarn add -D @spotlightjs/spotlight
```
</TabItem>
</Tabs>
Loading