Skip to content

Commit

Permalink
fix: Make full url customizable for Spotlight (#9652)
Browse files Browse the repository at this point in the history
  • Loading branch information
HazAT committed Nov 24, 2023
1 parent 075cc6e commit 739d904
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/node/src/integrations/spotlight.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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';

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;
};
Expand All @@ -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',
};
}

Expand Down Expand Up @@ -61,7 +61,7 @@ function connectToSpotlight(client: Client, options: Required<SpotlightConnectio
return;
}

client.on('beforeEnvelope', envelope => {
client.on('beforeEnvelope', (envelope: Envelope) => {
if (failedRequests > 3) {
logger.warn('[Spotlight] Disabled Sentry -> Spotlight integration due to too many failed requests');
return;
Expand All @@ -72,7 +72,7 @@ function connectToSpotlight(client: Client, options: Required<SpotlightConnectio
const req = http.request(
{
method: 'POST',
path: '/stream',
path: spotlightUrl.pathname,
hostname: spotlightUrl.hostname,
port: spotlightUrl.port,
headers: {
Expand Down Expand Up @@ -102,7 +102,7 @@ function connectToSpotlight(client: Client, options: Required<SpotlightConnectio

function parseSidecarUrl(url: string): URL | undefined {
try {
return new URL(`${url}/stream`);
return new URL(`${url}`);
} catch {
logger.warn(`[Spotlight] Invalid sidecar URL: ${url}`);
return undefined;
Expand Down
39 changes: 39 additions & 0 deletions packages/node/test/integrations/spotlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,45 @@ describe('Spotlight', () => {
);
});

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<EventEnvelope>({ 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' });
Expand Down

0 comments on commit 739d904

Please sign in to comment.