|
| 1 | +import { |
| 2 | + AWS_LAMBDA_WEB_ENDPOINT_NAME, |
| 3 | + CLOUD_PLATFORM, |
| 4 | + CLOUD_PROVIDER, |
| 5 | + CLOUD_REGION, |
| 6 | + FAAS_COLDSTART, |
| 7 | + FAAS_INVOCATION_ID, |
| 8 | + FAAS_MAX_MEMORY, |
| 9 | + SENTRY_OP, |
| 10 | +} from '@sentry/conventions/attributes'; |
| 11 | +import { expect, vi } from 'vitest'; |
| 12 | +import { createEsmAndCjsTests } from '../../../utils/runner'; |
| 13 | + |
| 14 | +createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { |
| 15 | + test.each([ |
| 16 | + { name: 'default tracing', tracing: true }, |
| 17 | + { name: 'tracing disabled', tracing: false }, |
| 18 | + ])('$name: isolates concurrent requests and leaves logs and metrics unenriched', async ({ tracing }) => { |
| 19 | + const runner = createRunner() |
| 20 | + .withEnv({ |
| 21 | + AWS_LAMBDA_HTTP_ENDPOINT: '0.0.0.0:3000', |
| 22 | + AWS_REGION: 'us-east-1', |
| 23 | + AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '2048', |
| 24 | + AWS_LAMBDA_INITIALIZATION_TYPE: 'native-http', |
| 25 | + AWS_EXECUTION_ENV: 'AWS_Lambda_nodejs24.x-lite', |
| 26 | + NO_TRACING: tracing ? '' : '1', |
| 27 | + }) |
| 28 | + .unordered(); |
| 29 | + |
| 30 | + const spans = []; |
| 31 | + |
| 32 | + for (const request of ['first', 'second']) { |
| 33 | + runner.expect({ |
| 34 | + event: { |
| 35 | + exception: { values: [expect.objectContaining({ value: request })] }, |
| 36 | + contexts: { |
| 37 | + cloud_resource: { |
| 38 | + [CLOUD_PROVIDER]: 'aws', |
| 39 | + [CLOUD_PLATFORM]: 'aws_lambda', |
| 40 | + [CLOUD_REGION]: 'us-east-1', |
| 41 | + }, |
| 42 | + 'aws.lambda': { |
| 43 | + memory_size: 2_147_483_648, |
| 44 | + initialization_type: 'native-http', |
| 45 | + cold_start: request === 'first', |
| 46 | + aws_request_id: request, |
| 47 | + endpoint_name: `${request}-endpoint`, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + if (tracing) { |
| 54 | + spans.push({ |
| 55 | + request, |
| 56 | + collected: runner.collectStreamedSpansUntilSegment( |
| 57 | + span => span.attributes[FAAS_INVOCATION_ID]?.value === request, |
| 58 | + ), |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const assertUnenriched = (items: { attributes?: Record<string, unknown> }[]): void => { |
| 64 | + expect(items).toHaveLength(2); |
| 65 | + for (const item of items) { |
| 66 | + expect(Object.keys(item.attributes || {}).filter(key => /^(aws\.lambda\.|faas\.|cloud\.)/.test(key))).toEqual( |
| 67 | + [], |
| 68 | + ); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + runner.expect({ log: container => assertUnenriched(container.items) }); |
| 73 | + runner.expect({ trace_metric: container => assertUnenriched(container.items) }); |
| 74 | + |
| 75 | + const running = runner.start(); |
| 76 | + const first = running.makeRequest('get', '/first', { |
| 77 | + headers: { 'lambda-web-request-id': 'first', 'lambda-web-endpoint-name': 'first-endpoint' }, |
| 78 | + }); |
| 79 | + await vi.waitFor(() => expect(running.getLogs().join('\n')).toContain('FIRST_STARTED'), { timeout: 10_000 }); |
| 80 | + |
| 81 | + await Promise.all([ |
| 82 | + first, |
| 83 | + running.makeRequest('get', '/second', { |
| 84 | + headers: { 'lambda-web-request-id': 'second', 'lambda-web-endpoint-name': 'second-endpoint' }, |
| 85 | + }), |
| 86 | + ]); |
| 87 | + await running.completed(); |
| 88 | + |
| 89 | + const segmentSpans = []; |
| 90 | + |
| 91 | + for (const { request, collected } of spans) { |
| 92 | + const items = await collected; |
| 93 | + segmentSpans.push(...items.filter(item => item.is_segment)); |
| 94 | + |
| 95 | + expect(items.find(item => item.is_segment)).toMatchObject({ |
| 96 | + name: 'GET /:request', |
| 97 | + attributes: { |
| 98 | + [SENTRY_OP]: { type: 'string', value: 'http.server' }, |
| 99 | + [CLOUD_PLATFORM]: { type: 'string', value: 'aws_lambda' }, |
| 100 | + [CLOUD_REGION]: { type: 'string', value: 'us-east-1' }, |
| 101 | + [FAAS_MAX_MEMORY]: { type: 'integer', value: 2_147_483_648 }, |
| 102 | + [FAAS_COLDSTART]: { type: 'boolean', value: request === 'first' }, |
| 103 | + [FAAS_INVOCATION_ID]: { type: 'string', value: request }, |
| 104 | + [AWS_LAMBDA_WEB_ENDPOINT_NAME]: { type: 'string', value: `${request}-endpoint` }, |
| 105 | + }, |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + expect(segmentSpans).toHaveLength(tracing ? 2 : 0); |
| 110 | + }); |
| 111 | +}); |
0 commit comments