-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Using Sentry with SST to monitor Lambda Function #13409
Comments
Hey @author thanks for writing in and sorry that you're experiencing problems! On first glance, can you confirm that you only add We'll further look into your issue next week as this week is Hackweek at Sentry (see #13421). |
Hello! I'm having the same issue with my SST project. Here's what my dependencies are:
I don't enable Sentry integrations when running locally, so my SST Stacks is a bit dynamic: import { LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { EventBus } from 'sst/constructs';
export default function IngressConsumer({ app, stack }) {
const { layers, env } = getSentryConfig(app.local, stack);
const ingressEB = new EventBus(stack, 'Ingress', {
rules: {
All: {
pattern: { source: [{ prefix: '' }] },
targets: {
index: {
function: {
runtime: 'nodejs18.x',
timeout: 30,
handler: 'packages/event-consumers/src/index.handler',
deadLetterQueueEnabled: true,
tracing: 'active',
layers,
environment: env,
},
},
},
},
},
});
stack.addOutputs({
Ingress: ingressEB.eventBusArn,
});
}
/**
* Retrieves the Sentry configuration based on the environment.
*
* @param {boolean} isLocal - Indicates if the environment is local.
* @param {object} stack - The stack object.
* @returns {object} The Sentry configuration.
*/
const getSentryConfig = (isLocal, stack) => (
isLocal
? {}
: {
layers: [
LayerVersion.fromLayerVersionArn(
stack,
'SentryLayer',
'arn:aws:lambda:us-east-1:943013980633:layer:SentryNodeServerlessSDK:224'
),
],
environment: {
SENTRY_DSN: process.env.SENTRY_DSN,
SENTRY_TRACE_SAMPLE_RATE: process.env.SENTRY_TRACE_SAMPLE_RATE,
SENTRY_ENABLED: process.env.SENTRY_ENABLED,
NODE_OPTIONS: '-r @sentry/serverless/dist/awslambda-auto',
},
}
);
Please let me know if you need any additional info. Thanks! Finally, the target handler code is something like this: import processIt from './processor';
const handleEvent = async (event) => {
console.log('[handler] event:', JSON.stringify(event));
await processIt(event.detail);
}
const sentryWrapper = async (handler) => {
const sentryEnabled = process.env.SENTRY_ENABLED === 'true';
if (!sentryEnabled) return handler;
try {
const Sentry = await import('@sentry/serverless');
return Sentry.AWSLambda.wrapHandler(handler);
} catch (error) {
console.error('[handler] failed to initialize Sentry.io wrapper. Ignored.', error);
}
};
export const handler = await sentryWrapper(handleEvent); |
@williamrjribeiro think i figured it out. Follow the ESM instructions as per the link below: Need to add below config to the Function construct to make sure the required packages are vendored via node_modules:
|
Thanks for the quick reply @sheeni17 ! I'll give it a shot and see how it goes. 🤞 |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/aws-serverless
SDK Version
SentryNodeServerlessSDK:262 (Lambda Layer)
Framework Version
"sst": 2.41.5
Link to Sentry event
No response
Reproduction Example/SDK Setup
I'm using SST to create and deploy my node lambda function (standard setup node 20.x, no fancy build options).
Following both sentry's and SST's. guidance, im using Lambda Layers (v267) and created the required env variables as per the docs. I couldn'y see any activity in my sentry console.
Then i looked in to Sentry's docs to find Layes only would with cjs buid outputs (SST build defaults to esm). So i followed the ESM guide and installed the dependencies as required (and added the ENV variables as required).
Now, the functions are failing with error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sentry/aws-serverless' imported from /var/task/
I'm initialising sentry via env variables (Lambda layer)
SENTRY_DSN: DSN,
NODE_OPTIONS: "-r @sentry/aws-serverless/awslambda-auto", //CJS
NODE_OPTIONS: "--import @sentry/aws-serverless/awslambda-auto" //ESM
SENTRY_TRACES_SAMPLE_RATE: "1.0"
Steps to Reproduce
I have verified that the function has the right environment variables via the console.
I have tried wrapping the handler with Sentry.wrapHandler and without no success and tried both CJS and ESM methods.
Expected Result
I expect to see transactions and errors appearing on the dashboard.
Actual Result
No activity recorded on the sentry console.
The text was updated successfully, but these errors were encountered: