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

fix: Make full url customizable for Spotlight #9652

Merged
merged 1 commit into from
Nov 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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