Skip to content

test(e2e): Fix event buffering #13233

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

Merged
merged 8 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 18 additions & 5 deletions dev-packages/e2e-tests/publish-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@
// Publish built packages to the fake registry
packageTarballPaths.forEach(tarballPath => {
// `--userconfig` flag needs to be before `publish`
childProcess.execSync(`npm --userconfig ${__dirname}/test-registry.npmrc publish ${tarballPath}`, {
cwd: repositoryRoot, // Can't use __dirname here because npm would try to publish `@sentry-internal/e2e-tests`
encoding: 'utf8',
stdio: 'inherit',
});
childProcess.exec(
`npm --userconfig ${__dirname}/test-registry.npmrc publish ${tarballPath}`,
{
cwd: repositoryRoot, // Can't use __dirname here because npm would try to publish `@sentry-internal/e2e-tests`
encoding: 'utf8',
},
(err, stdout, stderr) => {
// eslint-disable-next-line no-console
console.log(stdout);
// eslint-disable-next-line no-console
console.log(stderr);
if (err) {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
}
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import { startEventProxyServer } from '@sentry-internal/test-utils';
startEventProxyServer({
port: 3031,
proxyServerName: 'aws-serverless-esm',
forwardToSentry: false,
});
93 changes: 24 additions & 69 deletions dev-packages/test-utils/src/event-proxy-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable max-lines */

import * as fs from 'fs';
import * as http from 'http';
import type { AddressInfo } from 'net';
Expand All @@ -18,11 +17,6 @@ interface EventProxyServerOptions {
port: number;
/** The name for the proxy server used for referencing it with listener functions */
proxyServerName: string;
/**
* Whether or not to forward the event to sentry. @default `false`
* This is helpful when you can't register a tunnel in the SDK setup (e.g. lambda layer without Sentry.init call)
*/
forwardToSentry?: boolean;
}

interface SentryRequestCallbackData {
Expand All @@ -36,12 +30,16 @@ interface EventCallbackListener {
(data: string): void;
}

type SentryResponseStatusCode = number;
type SentryResponseBody = string;
type SentryResponseHeaders = Record<string, string> | undefined;

type OnRequest = (
eventCallbackListeners: Set<EventCallbackListener>,
proxyRequest: http.IncomingMessage,
proxyRequestBody: string,
eventBuffer: BufferedEvent[],
) => Promise<[number, string, Record<string, string> | undefined]>;
) => Promise<[SentryResponseStatusCode, SentryResponseBody, SentryResponseHeaders]>;

interface BufferedEvent {
timestamp: number;
Expand Down Expand Up @@ -172,9 +170,7 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
await startProxyServer(options, async (eventCallbackListeners, proxyRequest, proxyRequestBody, eventBuffer) => {
const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0] as string);

const shouldForwardEventToSentry = options.forwardToSentry || false;

if (!envelopeHeader.dsn && shouldForwardEventToSentry) {
if (!envelopeHeader.dsn) {
// eslint-disable-next-line no-console
console.log(
'[event-proxy-server] Warn: No dsn on envelope header. Maybe a client-report was received. Proxy request body:',
Expand All @@ -184,69 +180,28 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
return [200, '{}', {}];
}

if (!shouldForwardEventToSentry) {
const data: SentryRequestCallbackData = {
envelope: parseEnvelope(proxyRequestBody),
rawProxyRequestBody: proxyRequestBody,
rawSentryResponseBody: '',
sentryResponseStatusCode: 200,
};
eventCallbackListeners.forEach(listener => {
listener(Buffer.from(JSON.stringify(data)).toString('base64'));
});

return [
200,
'{}',
{
'Access-Control-Allow-Origin': '*',
},
];
}

const { origin, pathname, host } = new URL(envelopeHeader.dsn as string);

const projectId = pathname.substring(1);
const sentryIngestUrl = `${origin}/api/${projectId}/envelope/`;

proxyRequest.headers.host = host;
const data: SentryRequestCallbackData = {
envelope: parseEnvelope(proxyRequestBody),
rawProxyRequestBody: proxyRequestBody,
rawSentryResponseBody: '',
sentryResponseStatusCode: 200,
};

const reqHeaders: Record<string, string> = {};
for (const [key, value] of Object.entries(proxyRequest.headers)) {
reqHeaders[key] = value as string;
}
const dataString = Buffer.from(JSON.stringify(data)).toString('base64');

// Fetch does not like this
delete reqHeaders['transfer-encoding'];

return fetch(sentryIngestUrl, {
body: proxyRequestBody,
headers: reqHeaders,
method: proxyRequest.method,
}).then(async res => {
const rawSentryResponseBody = await res.text();
const data: SentryRequestCallbackData = {
envelope: parseEnvelope(proxyRequestBody),
rawProxyRequestBody: proxyRequestBody,
rawSentryResponseBody,
sentryResponseStatusCode: res.status,
};

const dataString = Buffer.from(JSON.stringify(data)).toString('base64');

eventBuffer.push({ data: dataString, timestamp: getNanosecondTimestamp() });

eventCallbackListeners.forEach(listener => {
listener(dataString);
});

const resHeaders: Record<string, string> = {};
for (const [key, value] of res.headers.entries()) {
resHeaders[key] = value;
}
eventBuffer.push({ data: dataString, timestamp: getNanosecondTimestamp() });

return [res.status, rawSentryResponseBody, resHeaders];
eventCallbackListeners.forEach(listener => {
listener(dataString);
});

return [
200,
'{}',
{
'Access-Control-Allow-Origin': '*',
},
];
});
}

Expand Down
Loading