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

test(e2e): Fix event buffering #13233

Merged
merged 8 commits into from
Aug 5, 2024
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
2 changes: 1 addition & 1 deletion dev-packages/e2e-tests/Dockerfile.publish-packages
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ ARG NODE_VERSION=18.18.0
FROM node:${NODE_VERSION}

WORKDIR /sentry-javascript/dev-packages/e2e-tests
CMD [ "yarn", "ts-node", "publish-packages.ts" ]
CMD [ "yarn", "ts-node", "publish-packages.ts", "--transpile-only" ]
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 @@ const packageTarballPaths = glob.sync('packages/*/sentry-*.tgz', {
// 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}`,
Dismissed Show dismissed Hide dismissed
{
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,
});
103 changes: 23 additions & 80 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 @@ -170,83 +168,28 @@ export async function startProxyServer(
*/
export async function startEventProxyServer(options: EventProxyServerOptions): Promise<void> {
await startProxyServer(options, async (eventCallbackListeners, proxyRequest, proxyRequestBody, eventBuffer) => {
const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0] as string);
const data: SentryRequestCallbackData = {
envelope: parseEnvelope(proxyRequestBody),
rawProxyRequestBody: proxyRequestBody,
rawSentryResponseBody: '',
sentryResponseStatusCode: 200,
};

const shouldForwardEventToSentry = options.forwardToSentry || false;
const dataString = Buffer.from(JSON.stringify(data)).toString('base64');

if (!envelopeHeader.dsn && shouldForwardEventToSentry) {
// 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:',
proxyRequestBody,
);

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 reqHeaders: Record<string, string> = {};
for (const [key, value] of Object.entries(proxyRequest.headers)) {
reqHeaders[key] = value as string;
}

// 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