-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): Add e2e test for AWS lambda in ESM mode (#12833)
Add an E2E test app that simulates running an AWS lambda function in ESM mode. At the same time, this test app represents the state of how to manually use the `@sentry/aws-serverless` SDK for ESM-based lambda functions.
- Loading branch information
Showing
13 changed files
with
216 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/aws-serverless-esm/.npmrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
23 changes: 23 additions & 0 deletions
23
dev-packages/e2e-tests/test-applications/aws-serverless-esm/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
"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" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
dev-packages/e2e-tests/test-applications/aws-serverless-esm/playwright.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
||
// 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; | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
const config = getPlaywrightConfig( | ||
{ startCommand: '' }, | ||
{ | ||
/* 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; |
26 changes: 26 additions & 0 deletions
26
dev-packages/e2e-tests/test-applications/aws-serverless-esm/src/lambda-function.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
{ | ||
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 }; |
5 changes: 5 additions & 0 deletions
5
dev-packages/e2e-tests/test-applications/aws-serverless-esm/src/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/aws-serverless-esm/src/run-lambda.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
16 changes: 16 additions & 0 deletions
16
dev-packages/e2e-tests/test-applications/aws-serverless-esm/src/run.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 @sentry/aws-serverless/awslambda-auto', | ||
SENTRY_DSN: 'http://public@localhost:3031/1337', | ||
SENTRY_TRACES_SAMPLE_RATE: '1.0', | ||
}, | ||
cwd: process.cwd(), | ||
}); |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/aws-serverless-esm/start-event-proxy.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
69 changes: 69 additions & 0 deletions
69
dev-packages/e2e-tests/test-applications/aws-serverless-esm/tests/basic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters