Skip to content

Commit dbe4f76

Browse files
authored
feat(aws): Detect SDK source for AWS Lambda layer (#17128)
closes #16878
1 parent 60b045e commit dbe4f76

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

dev-packages/e2e-tests/test-applications/aws-lambda-layer-cjs/tests/basic.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ test('Lambda layer SDK bundle sends events', async ({ request }) => {
6969
op: 'test',
7070
}),
7171
);
72+
73+
// shows that the SDK source is correctly detected
74+
expect(transactionEvent.sdk?.packages).toContainEqual(
75+
expect.objectContaining({ name: 'aws-lambda-layer:@sentry/aws-serverless' }),
76+
);
7277
});

dev-packages/e2e-tests/test-applications/aws-lambda-layer-esm/tests/basic.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ test('Lambda layer SDK bundle sends events', async ({ request }) => {
6969
op: 'test',
7070
}),
7171
);
72+
73+
// shows that the SDK source is correctly detected
74+
expect(transactionEvent.sdk?.packages).toContainEqual(
75+
expect.objectContaining({ name: 'aws-lambda-layer:@sentry/aws-serverless' }),
76+
);
7277
});

dev-packages/e2e-tests/test-applications/aws-serverless-esm/tests/basic.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ test('AWS Serverless SDK sends events in ESM mode', async ({ request }) => {
8282
op: 'manual',
8383
}),
8484
);
85+
86+
// shows that the SDK source is correctly detected
87+
expect(transactionEvent.sdk?.packages).toContainEqual(
88+
expect.objectContaining({ name: 'npm:@sentry/aws-serverless' }),
89+
);
8590
});

packages/aws-serverless/scripts/buildLambdaLayer.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ async function buildLambdaLayer(): Promise<void> {
4343
'build/aws/dist-serverless/nodejs/node_modules/@sentry/aws-serverless/dist/awslambda-auto.js',
4444
);
4545

46+
replaceSDKSource();
47+
4648
const zipFilename = `sentry-node-serverless-${version}.zip`;
4749
console.log(`Creating final layer zip file ${zipFilename}.`);
4850
// need to preserve the symlink above with -y
@@ -178,3 +180,30 @@ function buildPackageJson(): void {
178180
const packageJsonPath = './build/aws/dist-serverless/nodejs/package.json';
179181
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
180182
}
183+
184+
function replaceSDKSource(): void {
185+
console.log('Replacing SDK source.');
186+
187+
const envFiles = [
188+
'./build/aws/dist-serverless/nodejs/node_modules/@sentry/core/build/cjs/utils/env.js',
189+
'./build/aws/dist-serverless/nodejs/node_modules/@sentry/core/build/esm/utils/env.js',
190+
];
191+
192+
for (const envFile of envFiles) {
193+
try {
194+
let content = fs.readFileSync(envFile, 'utf-8');
195+
196+
// Replace the line marked with __SENTRY_SDK_SOURCE__ comment
197+
// Change from 'npm' to 'aws-lambda-layer' to identify that this is the AWS Lambda layer
198+
content = content.replace(
199+
"/* __SENTRY_SDK_SOURCE__ */ return 'npm';",
200+
"/* __SENTRY_SDK_SOURCE__ */ return 'aws-lambda-layer';",
201+
);
202+
203+
fs.writeFileSync(envFile, content);
204+
console.log(`Updated SDK source in ${envFile}`);
205+
} catch {
206+
console.warn(`Warning: Could not update SDK source in ${envFile}`);
207+
}
208+
}
209+
}

packages/aws-serverless/src/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Integration, Options, Scope, Span } from '@sentry/core';
22
import {
33
applySdkMetadata,
44
debug,
5+
getSDKSource,
56
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
67
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
78
} from '@sentry/core';
@@ -81,7 +82,7 @@ export function init(options: NodeOptions = {}): NodeClient | undefined {
8182
...options,
8283
};
8384

84-
applySdkMetadata(opts, 'aws-serverless');
85+
applySdkMetadata(opts, 'aws-serverless', ['aws-serverless'], getSDKSource());
8586

8687
return initWithoutDefaultIntegrations(opts);
8788
}

packages/core/src/utils/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
1717

18-
export type SdkSource = 'npm' | 'cdn' | 'loader';
18+
export type SdkSource = 'npm' | 'cdn' | 'loader' | 'aws-lambda-layer';
1919

2020
/**
2121
* Figures out if we're building a browser bundle.

0 commit comments

Comments
 (0)