diff --git a/packages/node/src/integrations/spotlight.ts b/packages/node/src/integrations/spotlight.ts index 4b4b9d907721..5489933fa6d9 100644 --- a/packages/node/src/integrations/spotlight.ts +++ b/packages/node/src/integrations/spotlight.ts @@ -1,4 +1,4 @@ -import type { Client, Integration } from '@sentry/types'; +import type { Client, Envelope, Integration } from '@sentry/types'; import { logger, serializeEnvelope } from '@sentry/utils'; import * as http from 'http'; import { URL } from 'url'; @@ -6,7 +6,7 @@ import { URL } from 'url'; type SpotlightConnectionOptions = { /** * Set this if the Spotlight Sidecar is not running on localhost:8969 - * By default, the Url is set to http://localhost:8969 + * By default, the Url is set to http://localhost:8969/stream */ sidecarUrl?: string; }; @@ -26,7 +26,7 @@ export class Spotlight implements Integration { public constructor(options?: SpotlightConnectionOptions) { this._options = { - sidecarUrl: options?.sidecarUrl || 'http://localhost:8969', + sidecarUrl: options?.sidecarUrl || 'http://localhost:8969/stream', }; } @@ -61,7 +61,7 @@ function connectToSpotlight(client: Client, options: Required { + client.on('beforeEnvelope', (envelope: Envelope) => { if (failedRequests > 3) { logger.warn('[Spotlight] Disabled Sentry -> Spotlight integration due to too many failed requests'); return; @@ -72,7 +72,7 @@ function connectToSpotlight(client: Client, options: Required { ); }); + it('sends an envelope POST request to a custom sidecar url', () => { + const httpSpy = jest.spyOn(http, 'request').mockImplementationOnce(() => { + return { + on: jest.fn(), + write: jest.fn(), + end: jest.fn(), + } as any; + }); + + let callback: (envelope: Envelope) => void = () => {}; + const clientWithSpy = { + ...client, + on: jest.fn().mockImplementationOnce((_, cb) => (callback = cb)), + }; + + const integration = new Spotlight({ sidecarUrl: 'http://mylocalhost:8888/abcd' }); + // @ts-expect-error - this is fine in tests + integration.setup(clientWithSpy); + + const envelope = createEnvelope({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ + [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }], + ]); + + callback(envelope); + + expect(httpSpy).toHaveBeenCalledWith( + { + headers: { + 'Content-Type': 'application/x-sentry-envelope', + }, + hostname: 'mylocalhost', + method: 'POST', + path: '/abcd', + port: '8888', + }, + expect.any(Function), + ); + }); + describe('no-ops if', () => { it('an invalid URL is passed', () => { const integration = new Spotlight({ sidecarUrl: 'invalid-url' });