Skip to content

test(aws-lambda): Add basic lambda layer e2e test #12279

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 4 commits into from
May 31, 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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ jobs:
[
'angular-17',
'angular-18',
'aws-lambda-layer',
'cloudflare-astro',
'node-express',
'create-react-app',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@sentry:registry=http://127.0.0.1:4873
@sentry-internal:registry=http://127.0.0.1:4873
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "node-express-app",
"version": "1.0.0",
"private": true,
"scripts": {
"copy:layer": "cp -r ./../../../../packages/aws-serverless/build/aws/dist-serverless/nodejs/node_modules/ ./node_modules",
"start": "node src/run.js",
"test": "playwright test",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"test:build": "pnpm install && pnpm copy:layer",
"test:assert": "pnpm test"
},
"dependencies": {
},
"devDependencies": {
"@sentry-internal/test-utils": "link:../../../test-utils",
"@playwright/test": "^1.41.1",
"wait-port": "1.0.4"
},
"volta": {
"extends": "../../package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

// Fix urls not resolving to localhost on Node v17+
// See: https://github.com/axios/axios/issues/3821#issuecomment-1413727575
import { setDefaultResultOrder } from 'dns';
setDefaultResultOrder('ipv4first');

const eventProxyPort = 3031;
const lambdaPort = 3030;

/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 150_000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000,
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: 0,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'list',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,

/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: `http://localhost:${lambdaPort}`,

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
// For now we only test Chrome!
// {
// name: 'firefox',
// use: {
// ...devices['Desktop Firefox'],
// },
// },
// {
// name: 'webkit',
// use: {
// ...devices['Desktop Safari'],
// },
// },
],

/* Run your local dev server before starting the tests */
webServer: [
{
command: `node start-event-proxy.mjs && pnpm wait-port ${eventProxyPort}`,
port: eventProxyPort,
stdout: 'pipe',
},
],
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Sentry = require('@sentry/aws-serverless');

const http = require('http');

function handle() {
Sentry.startSpanManual({ name: 'aws-lambda-layer-test-txn', op: 'test' }, span => {
http.get('http://example.com', res => {
res.on('data', d => {
process.stdout.write(d);
});

res.on('end', () => {
span.end();
});
});
});
}

module.exports = { handle };
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { handle } = require('./lambda-function');
handle();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const child_process = require('child_process');

child_process.execSync('node ./src/run-lambda.js', {
stdio: 'inherit',
env: {
...process.env,
LAMBDA_TASK_ROOT: '.',
_HANDLER: 'handle',

NODE_OPTIONS: '--require @sentry/aws-serverless/dist/awslambda-auto',
SENTRY_DSN: 'http://public@localhost:3031/1337',
SENTRY_TRACES_SAMPLE_RATE: '1.0',
SENTRY_DEBUG: 'true',
},
cwd: process.cwd(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'aws-serverless-lambda-layer',
forwardToSentry: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as child_process from 'child_process';
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('Lambda layer SDK bundle sends events', async ({ request }) => {
const transactionEventPromise = waitForTransaction('aws-serverless-lambda-layer', transactionEvent => {
return transactionEvent?.transaction === 'aws-lambda-layer-test-txn';
});

await new Promise<void>(resolve =>
setTimeout(() => {
resolve();
}, 1000),
);

child_process.execSync('pnpm start', {
stdio: 'ignore',
});

const transactionEvent = await transactionEventPromise;

// shows the SDK sent a transaction
expect(transactionEvent.transaction).toEqual('aws-lambda-layer-test-txn');

// shows that the Otel Http instrumentation is working
expect(transactionEvent.spans).toHaveLength(1);
expect(transactionEvent.spans![0]).toMatchObject({
data: expect.objectContaining({
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.http',
url: 'http://example.com/',
}),
description: 'GET http://example.com/',
op: 'http.client',
});
});
38 changes: 34 additions & 4 deletions dev-packages/test-utils/src/event-proxy-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ 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 `true`
* 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 Down Expand Up @@ -56,7 +61,9 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P

const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0]);

if (!envelopeHeader.dsn) {
const shouldForwardEventToSentry = options.forwardToSentry != null ? options.forwardToSentry : true;

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:',
Expand All @@ -69,6 +76,23 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
return;
}

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

proxyResponse.writeHead(200);
proxyResponse.write('{}', 'utf-8');
proxyResponse.end();
return;
}

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

const projectId = pathname.substring(1);
Expand Down Expand Up @@ -269,7 +293,13 @@ async function registerCallbackServerPort(serverName: string, port: string): Pro
await writeFile(tmpFilePath, port, { encoding: 'utf8' });
}

function retrieveCallbackServerPort(serverName: string): Promise<string> {
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
return readFile(tmpFilePath, 'utf8');
async function retrieveCallbackServerPort(serverName: string): Promise<string> {
try {
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
return await readFile(tmpFilePath, 'utf8');
} catch (e) {
// eslint-disable-next-line no-console
console.log('Could not read callback server port', e);
throw e;
}
}
5 changes: 3 additions & 2 deletions packages/aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@
},
"scripts": {
"build": "run-p build:transpile build:types build:bundle",
"build:bundle": "yarn ts-node scripts/buildLambdaLayer.ts",
"build:bundle": "yarn build:layer",
"build:layer": "yarn ts-node scripts/buildLambdaLayer.ts",
"build:dev": "run-p build:transpile build:types",
"build:transpile": "rollup -c rollup.npm.config.mjs",
"build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:layer",
"build:types": "run-s build:types:core build:types:downlevel",
"build:types:core": "tsc -p tsconfig.types.json",
"build:types:downlevel": "yarn downlevel-dts build/npm/types build/npm/types-ts3.8 --to ts3.8",
Expand Down
Loading