Repository navigation
feat(node): Add awsLambdaWebIntegration for Lambda Web Functions
#25355
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
Open
msonnb
wants to merge
8
commits into
develop
Choose a base branch
from
ms/lambda-web-functions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
32b5d10
feat(node): Add `awsLambdaWebIntegration` for Lambda Web Functions
msonnb 9f349a3
fix(node): Use faas.max_memory for Lambda Web Function spans
msonnb 4eb78d8
fix(node): Use bytes for Lambda Web Function memory context
msonnb 8639881
test(node): Use a fixed log message in Lambda Web Function fixture
msonnb 8106676
fix(node): Re-export Lambda Web integration from dependent SDKs
msonnb 9e67f46
test(node): Clarify Lambda Web scenarios and use attribute constants
msonnb 94fd6f7
test(node): Simplify Lambda Web Function fixture
msonnb c5e823d
test(node): Remove redundant Lambda Web unit tests
msonnb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
dev-packages/node-integration-tests/suites/aws-lambda-web/requests/instrument.mjs
This file contains hidden or 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,8 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@example.com/1', | ||
| tracesSampleRate: process.env.NO_TRACING ? undefined : 1, | ||
| transport: loggingTransport, | ||
| }); |
28 changes: 28 additions & 0 deletions
28
dev-packages/node-integration-tests/suites/aws-lambda-web/requests/scenario.mjs
This file contains hidden or 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,28 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests'; | ||
| import express from 'express'; | ||
|
|
||
| const app = express(); | ||
| let releaseFirst; | ||
| const secondRequestStarted = new Promise(resolve => { | ||
| releaseFirst = resolve; | ||
| }); | ||
|
|
||
| app.get('/:request', async (req, res) => { | ||
| if (req.params.request === 'first') { | ||
| process.stdout.write('FIRST_STARTED\n'); | ||
| await secondRequestStarted; | ||
| } else { | ||
| releaseFirst(); | ||
| } | ||
|
|
||
| Sentry.captureException(new Error(req.params.request)); | ||
| Sentry.logger.info('Request handled'); | ||
| Sentry.metrics.count('request_count', 1); | ||
| res.send({ ok: true }); | ||
| setImmediate(() => { | ||
| void Sentry.flush(); | ||
| }); | ||
| }); | ||
|
|
||
| startExpressServerAndSendPortToRunner(app); |
98 changes: 98 additions & 0 deletions
98
dev-packages/node-integration-tests/suites/aws-lambda-web/requests/test.ts
This file contains hidden or 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,98 @@ | ||
| import { | ||
| CLOUD_ACCOUNT_ID, | ||
| CLOUD_PLATFORM, | ||
| CLOUD_PROVIDER, | ||
| CLOUD_REGION, | ||
| FAAS_COLDSTART, | ||
| FAAS_INVOCATION_ID, | ||
| SENTRY_OP, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { expect, vi } from 'vitest'; | ||
| import { createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test.each([ | ||
| { name: 'default tracing', tracing: true }, | ||
| { name: 'tracing disabled', tracing: false }, | ||
| ])('$name: isolates concurrent requests and leaves logs and metrics unenriched', async ({ tracing }) => { | ||
| const runner = createRunner() | ||
| .withEnv({ | ||
| AWS_LAMBDA_HTTP_ENDPOINT: '0.0.0.0:3000', | ||
| AWS_REGION: 'us-east-1', | ||
| AWS_ACCOUNT_ID: '123456789012', | ||
| AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '2048', | ||
| AWS_LAMBDA_INITIALIZATION_TYPE: 'native-http', | ||
| AWS_EXECUTION_ENV: 'AWS_Lambda_nodejs24.x-lite', | ||
| NO_TRACING: tracing ? '' : '1', | ||
| }) | ||
| .unordered(); | ||
| const spans = []; | ||
| for (const request of ['first', 'second']) { | ||
| runner.expect({ | ||
| event: { | ||
| exception: { values: [expect.objectContaining({ value: request })] }, | ||
| contexts: { | ||
| cloud_resource: { | ||
| [CLOUD_PROVIDER]: 'aws', | ||
| [CLOUD_PLATFORM]: 'aws_lambda', | ||
| [CLOUD_REGION]: 'us-east-1', | ||
| [CLOUD_ACCOUNT_ID]: '123456789012', | ||
| }, | ||
| 'aws.lambda': { | ||
| memory_size: 2_147_483_648, | ||
| initialization_type: 'native-http', | ||
| cold_start: request === 'first', | ||
| aws_request_id: request, | ||
| endpoint_name: `${request}-endpoint`, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| if (tracing) { | ||
| spans.push({ | ||
| request, | ||
| collected: runner.collectStreamedSpansUntilSegment( | ||
| span => span.attributes[FAAS_INVOCATION_ID]?.value === request, | ||
| ), | ||
| }); | ||
| } | ||
| } | ||
| const assertUnenriched = (items: { attributes?: Record<string, unknown> }[]): void => { | ||
| expect(items).toHaveLength(2); | ||
| for (const item of items) { | ||
| expect(Object.keys(item.attributes || {}).filter(key => /^(aws\.lambda\.|faas\.|cloud\.)/.test(key))).toEqual( | ||
| [], | ||
| ); | ||
| } | ||
| }; | ||
| runner.expect({ log: container => assertUnenriched(container.items) }); | ||
| runner.expect({ trace_metric: container => assertUnenriched(container.items) }); | ||
| const running = runner.start(); | ||
| const first = running.makeRequest('get', '/first', { | ||
| headers: { 'lambda-web-request-id': 'first', 'lambda-web-endpoint-name': 'first-endpoint' }, | ||
| }); | ||
| await vi.waitFor(() => expect(running.getLogs().join('\n')).toContain('FIRST_STARTED'), { timeout: 10_000 }); | ||
| await Promise.all([ | ||
| first, | ||
| running.makeRequest('get', '/second', { | ||
| headers: { 'lambda-web-request-id': 'second', 'lambda-web-endpoint-name': 'second-endpoint' }, | ||
| }), | ||
| ]); | ||
| await running.completed(); | ||
| for (const { request, collected } of spans) { | ||
| const items = await collected; | ||
| expect(items.find(item => item.is_segment)).toMatchObject({ | ||
| name: 'GET /:request', | ||
| attributes: { | ||
| [SENTRY_OP]: { type: 'string', value: 'http.server' }, | ||
| [CLOUD_PLATFORM]: { type: 'string', value: 'aws_lambda' }, | ||
| [CLOUD_REGION]: { type: 'string', value: 'us-east-1' }, | ||
| 'faas.max_memory': { type: 'integer', value: 2_147_483_648 }, | ||
| [FAAS_COLDSTART]: { type: 'boolean', value: request === 'first' }, | ||
| [FAAS_INVOCATION_ID]: { type: 'string', value: request }, | ||
| 'aws.lambda.web.endpoint_name': { type: 'string', value: `${request}-endpoint` }, | ||
| }, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
| import { | ||
| CLOUD_ACCOUNT_ID, | ||
| CLOUD_PLATFORM, | ||
| CLOUD_PROVIDER, | ||
| CLOUD_REGION, | ||
| FAAS_COLDSTART, | ||
| FAAS_INVOCATION_ID, | ||
| } from '@sentry/conventions/attributes'; | ||
| import type { IntegrationFn, SpanAttributes } from '@sentry/core'; | ||
| import { defineIntegration, getClient, getIsolationScope, safeSetSpanJSONAttributes } from '@sentry/core'; | ||
|
|
||
| let requestHandled = false; | ||
|
|
||
| const _awsLambdaWebIntegration = (() => { | ||
| const enabled = !!process.env.AWS_LAMBDA_HTTP_ENDPOINT; | ||
| const cloudContext: SpanAttributes = { | ||
| [CLOUD_PROVIDER]: 'aws', | ||
| [CLOUD_PLATFORM]: 'aws_lambda', | ||
| [CLOUD_REGION]: process.env.AWS_REGION, | ||
| [CLOUD_ACCOUNT_ID]: process.env.AWS_ACCOUNT_ID, | ||
| }; | ||
| const memory = Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE) * 1024 * 1024; | ||
| const lambdaContext = { | ||
| memory_size: Number.isFinite(memory) && memory > 0 ? memory : undefined, | ||
| initialization_type: process.env.AWS_LAMBDA_INITIALIZATION_TYPE, | ||
| }; | ||
| const environmentAttributes = { | ||
| ...cloudContext, | ||
| 'faas.max_memory': lambdaContext.memory_size, | ||
| 'aws.lambda.initialization_type': lambdaContext.initialization_type, | ||
|
Comment on lines
+29
to
+30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we already have those attributes in conventions?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not yet: getsentry/sentry-conventions#715 |
||
| }; | ||
|
|
||
| return { | ||
| name: 'AwsLambdaWeb', | ||
| setup(client) { | ||
| if (!enabled) return; | ||
|
|
||
| client.on('httpServerRequest', (_request, _response, request) => { | ||
| if (getClient() !== client) { | ||
| return; | ||
| } | ||
|
|
||
| const coldStart = !requestHandled; | ||
| requestHandled = true; | ||
| const requestId = request.headers?.['lambda-web-request-id']; | ||
| const endpointName = request.headers?.['lambda-web-endpoint-name']; | ||
| const scope = getIsolationScope(); | ||
| const requestContext: { cold_start: boolean; aws_request_id?: string; endpoint_name?: string } = { | ||
| ...scope.getScopeData().contexts['aws.lambda'], | ||
| cold_start: coldStart, | ||
| }; | ||
| if (typeof requestId === 'string') { | ||
| requestContext.aws_request_id = requestId; | ||
| } else { | ||
| delete requestContext.aws_request_id; | ||
| } | ||
| if (typeof endpointName === 'string') { | ||
| requestContext.endpoint_name = endpointName; | ||
| } else { | ||
| delete requestContext.endpoint_name; | ||
| } | ||
| scope.setContext('aws.lambda', requestContext); | ||
| }); | ||
| }, | ||
| processEvent(event) { | ||
| if (!enabled) return event; | ||
| event.contexts = { | ||
| ...event.contexts, | ||
| cloud_resource: { ...cloudContext, ...event.contexts?.cloud_resource }, | ||
| 'aws.lambda': { ...lambdaContext, ...event.contexts?.['aws.lambda'] }, | ||
| }; | ||
| return event; | ||
| }, | ||
| processSegmentSpan(span) { | ||
| if (!enabled) return; | ||
|
|
||
| const requestContext = getIsolationScope().getScopeData().contexts['aws.lambda']; | ||
| const attributes: SpanAttributes = { ...environmentAttributes }; | ||
| if (typeof requestContext?.cold_start === 'boolean') { | ||
| attributes[FAAS_COLDSTART] = requestContext.cold_start; | ||
| } | ||
| if (typeof requestContext?.aws_request_id === 'string') { | ||
| attributes[FAAS_INVOCATION_ID] = requestContext.aws_request_id; | ||
| } | ||
| if (typeof requestContext?.endpoint_name === 'string') { | ||
| attributes['aws.lambda.web.endpoint_name'] = requestContext.endpoint_name; | ||
| } | ||
| safeSetSpanJSONAttributes(span, attributes); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** Enrich AWS Lambda Web Function spans and error contexts with platform metadata. */ | ||
| export const awsLambdaWebIntegration = defineIntegration(_awsLambdaWebIntegration); | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.