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): Add e2e test for AWS lambda in ESM mode #12833

Merged
merged 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -995,6 +995,7 @@ jobs:
'angular-17',
'angular-18',
'aws-lambda-layer-cjs',
'aws-serverless-esm',
'node-express',
'create-react-app',
'create-next-app',
Expand Down
3 changes: 2 additions & 1 deletion dev-packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"esbuild": "0.20.0",
"glob": "8.0.3",
"ts-node": "10.9.1",
"yaml": "2.2.2"
"yaml": "2.2.2",
"rimraf": "^5.0.0"
Copy link
Member Author

@Lms24 Lms24 Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why but without adding rimraf explicitly here, I can't use the yarn test:run command to run e2e tests locally as rimraf isn't found otherwise 🤔

},
"volta": {
"extends": "../../package.json",
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,24 @@
{
"name": "node-express-app",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node src/run.mjs",
"test": "playwright test",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"test:build": "pnpm install",
"test:assert": "pnpm test"
},
"dependencies": {
"@sentry/aws-serverless": "* || latest"
},
"devDependencies": {
"@sentry-internal/test-utils": "link:../../../test-utils",
"@playwright/test": "^1.41.1",
"wait-port": "1.0.4",
"rimraf": "^5.0.8"
},
"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,7 @@
import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
dsn: 'http://public@localhost:3031/1337',
tracesSampleRate: 1.0,
debug: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as http from 'node:http';
import * as Sentry from '@sentry/aws-serverless';

const handler = Sentry.wrapHandler(async () => {
await new Promise(resolve => {
const req = http.request(
Copy link
Member Author

@Lms24 Lms24 Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

took me ages to find out why the previously used http.get call didn't produce a span. Turns out it's an Otel ESM bug. Reported it to Otel here: open-telemetry/opentelemetry-js#4857

Anyway, this http.request call now emits a span which we assert on in the test. So this shows that otel instrumentation generally works

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find

{
host: 'example.com',
},
res => {
res.on('data', d => {
process.stdout.write(d);
});

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

Sentry.startSpan({ name: 'manual-span', op: 'manual' }, () => {});
});

export { handler };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"//": "This is a mock package.json file which is usually created by AWS when deploying the lambda. OTEL instrumentation tries to read this file to get the lambda version",
"name": "lambda",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { handler } from './lambda-function.mjs';

// Simulate minimal event and context objects being passed to the handler by the AWS runtime
const event = {};
const context = {
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123453789012:function:my-lambda',
functionName: 'my-lambda',
};

await handler(event, context);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import child_process from 'child_process';

child_process.execSync('node ./src/run-lambda.mjs', {
stdio: 'inherit',
env: {
...process.env,
// On AWS, LAMBDA_TASK_ROOT is usually /var/task but for testing, we set it to the CWD to correctly apply our handler
LAMBDA_TASK_ROOT: process.cwd(),
_HANDLER: 'src/lambda-function.handler',

NODE_OPTIONS: '--import ./src/instrument.mjs',
},
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-esm',
forwardToSentry: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as child_process from 'child_process';
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('AWS Serverless SDK sends events in ESM mode', async ({ request }) => {
const transactionEventPromise = waitForTransaction('aws-serverless-esm', transactionEvent => {
return transactionEvent?.transaction === 'my-lambda';
});

// Waiting for 1s here because attaching the listener for events in `waitForTransaction` is not synchronous
// Since in this test, we don't start a browser via playwright, we don't have the usual delays (page.goto, etc)
// which are usually enough for us to never have noticed this race condition before.
// This is a workaround but probably sufficient as long as we only experience it in this test.
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('my-lambda'); // name should be the function name
expect(transactionEvent.contexts?.trace).toEqual({
data: {
'sentry.sample_rate': 1,
'sentry.source': 'component',
'sentry.origin': 'auto.function.serverless',
'sentry.op': 'function.aws.lambda',
'otel.kind': 'INTERNAL',
},
op: 'function.aws.lambda',
origin: 'auto.function.serverless',
span_id: expect.any(String),
status: 'ok',
trace_id: expect.any(String),
});

expect(transactionEvent.spans).toHaveLength(2);

// shows that the Otel Http instrumentation is working
expect(transactionEvent.spans).toContainEqual(
expect.objectContaining({
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',
}),
);

// shows that the manual span creation is working
expect(transactionEvent.spans).toContainEqual(
expect.objectContaining({
data: expect.objectContaining({
'sentry.op': 'manual',
'sentry.origin': 'manual',
}),
description: 'manual-span',
op: 'manual',
}),
);
});
3 changes: 3 additions & 0 deletions packages/aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"./awslambda-auto": {
"require": {
"default": "./build/npm/cjs/awslambda-auto.js"
},
"import": {
"default": "./build/npm/esm/awslambda-auto.js"
}
},
"./dist/awslambda-auto": {
Expand Down
Loading
Loading