Skip to content

Commit

Permalink
fix: Always try to use sidecar along with direct SDK transport (#672)
Browse files Browse the repository at this point in the history
Fixes #669
  • Loading branch information
BYK authored Jan 24, 2025
1 parent f3d71f2 commit 03e386a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-gifts-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/overlay': patch
---

fix: Always try to use sidecar along with direct SDK transport
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"notarytool",
"outro",
"pageload",
"platformicons",
"postject",
"raleway",
"rcodesign",
"spotlightjs",
"svgr",
Expand Down
7 changes: 4 additions & 3 deletions packages/overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"devDependencies": {
"@fontsource/raleway": "^5.1.0",
"@microlink/react-json-view": "^1.23.4",
"@sentry/react": "^8.43.0",
"@sentry/types": "^8.33.1",
"@sentry/utils": "^8.33.1",
"@sentry/browser": "^8.51.0",
"@sentry/react": "^8.51.0",
"@sentry/types": "^8.51.0",
"@sentry/core": "^8.51.0",
"@spotlightjs/sidecar": "workspace:*",
"@spotlightjs/tsconfig": "workspace:*",
"@types/beautify": "^0.0.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Envelope } from '@sentry/types';
import type { Envelope } from '@sentry/types';
import { CONTEXT_LINES_ENDPOINT } from '@spotlightjs/sidecar/constants';
import { DEFAULT_SIDECAR_URL } from '~/constants';
import { RawEventContext } from '~/integrations/integration';
import type { RawEventContext } from '~/integrations/integration';
import { log } from '../../../lib/logger';
import { generateUuidv4 } from '../../../lib/uuid';
import { Sdk, SentryErrorEvent, SentryEvent, SentryTransactionEvent, Span, Trace } from '../types';
import type { Sdk, SentryErrorEvent, SentryEvent, SentryTransactionEvent, Span, Trace } from '../types';
import { getNativeFetchImplementation } from '../utils/fetch';
import { sdkToPlatform } from '../utils/sdkToPlatform';
import { groupSpans } from '../utils/traces';
Expand Down
19 changes: 12 additions & 7 deletions packages/overlay/src/integrations/sentry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import InsightsTab from './tabs/InsightsTab';
import PerformanceTab from './tabs/PerformanceTab';
import type { SentryErrorEvent, SentryEvent } from './types';

import { spotlightBrowserIntegration } from '@sentry/browser';

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

type SentryIntegrationOptions = {
Expand All @@ -28,10 +30,13 @@ export default function sentryIntegration(options: SentryIntegrationOptions = {}
if (options.retries == null) {
options.retries = 10;
}
let baseSidecarUrl: string | undefined = undefined;
if (sidecarUrl) {
sentryDataCache.setSidecarUrl(removeURLSuffix(sidecarUrl, '/stream'));
baseSidecarUrl = removeURLSuffix(sidecarUrl, '/stream');
sentryDataCache.setSidecarUrl(baseSidecarUrl);
}
addSpotlightIntegrationToSentry(options);
log('Setting up Sentry integration for Spotlight');
addSpotlightIntegrationToSentry(options, baseSidecarUrl && new URL('/stream', baseSidecarUrl).href);

if (options.openLastError) {
sentryDataCache.subscribe('event', (e: SentryEvent) => {
Expand Down Expand Up @@ -197,7 +202,7 @@ type WindowWithSentry = Window & {
*
* @param options options of the Sentry integration for Spotlight
*/
function addSpotlightIntegrationToSentry(options: SentryIntegrationOptions) {
function addSpotlightIntegrationToSentry(options: SentryIntegrationOptions, sidecarUrl?: string) {
if (options.injectIntoSDK === false) {
return;
}
Expand All @@ -211,7 +216,7 @@ function addSpotlightIntegrationToSentry(options: SentryIntegrationOptions) {
log(`Will retry ${options.retries} more time(s) at 100ms intervals...`);
options.retries--;
setTimeout(() => {
addSpotlightIntegrationToSentry(options);
addSpotlightIntegrationToSentry(options, sidecarUrl);
}, 500);
}
return;
Expand Down Expand Up @@ -243,12 +248,12 @@ function addSpotlightIntegrationToSentry(options: SentryIntegrationOptions) {
const existingIntegration = Object.keys(sentryClient._integrations).find(i => /spotlight/i.test(i));
if (existingIntegration) {
log(
`Skipping direct integration as there's already a Spotlight integration enabled in Sentry SDK: ${existingIntegration}`,
`Skipping adding integration as there's already a Spotlight integration enabled in Sentry SDK: ${existingIntegration}`,
);
return;
}
const integration = spotlightIntegration();
sentryClient.addIntegration(integration);
sentryClient.addIntegration(spotlightIntegration());
sentryClient.addIntegration(spotlightBrowserIntegration({ sidecarUrl }));
} catch (e) {
warn('Failed to add Spotlight integration to Sentry', e);
log('Please open an issue with the error at: https://github.com/getsentry/spotlight/issues/new/choose');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Client, Envelope, Event, Integration } from '@sentry/types';
import { serializeEnvelope } from '@sentry/utils';
import { serializeEnvelope } from '@sentry/core';
import { trigger } from '../../lib/eventTarget';
import { log } from '../../lib/logger';
import sentryDataCache from './data/sentryDataCache';
Expand All @@ -20,14 +20,13 @@ export const spotlightIntegration = () => {
setupOnce: () => {
/* Empty function to ensure compatibility w/ JS SDK v7 >= 7.99.0 */
},
setup: () => {
log('Setting up the *direct* Sentry SDK integration for Spotlight');
},
setup: () => {},
processEvent: (event: Event) => {
// We don't want to send interaction transactions/root spans created from
// clicks within Spotlight to Sentry. Neither do we want them to be sent to
// spotlight.
if (isSpotlightInteraction(event)) {
log('Dropping transaction created from Spotlight interaction');
return null;
}

Expand Down
7 changes: 3 additions & 4 deletions packages/overlay/src/sidecar.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type React from 'react';
import { log } from './lib/logger';

export function connectToSidecar(
sidecarUrl: string,
// Content Type to listener
contentTypeListeners: Record<string, (event: { data: string | Uint8Array }) => void>,
setOnline: React.Dispatch<React.SetStateAction<boolean>>,
setOnline: (online: boolean) => void,
): () => void {
log('Connecting to sidecar at', sidecarUrl);
const sidecarStreamUrl = new URL('/stream', sidecarUrl);
Expand All @@ -18,12 +17,12 @@ export function connectToSidecar(

source.addEventListener('open', () => {
setOnline(true);
log('Open');
log('Sidecar connected.');
});

source.addEventListener('error', err => {
setOnline(false);
console.error('EventSource failed:', err);
console.error('Sidecar connection error:', err);
});

return () => {
Expand Down
93 changes: 52 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03e386a

Please sign in to comment.