- Notifications
You must be signed in to change notification settings - Fork26
This plugin adds automatic forwarding of errors and exceptions to Sentry (https://sentry.io) and Serverless (https://serverless.com)
License
arabold/serverless-sentry-plugin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This Serverless plugin simplifies the integration ofSentry with the popularServerless Framework and AWS Lambda.
Currently, we supportLambda Runtimes for Node.js 12, 14, and 16 for AWS Lambda. Other platforms can be added by providing a respective integration library. Pull Requests are welcome!
Theserverless-sentry-plugin andserverless-sentry-lib libraries are not affiliated with either Functional Software Inc., Sentry, Serverless or Amazon Web Services but developed independently and in my spare time.
- Easy to use. Promised 🤞
- Integrates withServerless Framework as well as theAWS Serverless Application Model for AWS Lambda (though no use of any framework is required).
- Wraps your Node.js code withSentry error capturing.
- Forwards any errors returned by your AWS Lambda function to Sentry.
- Warn if your code is about to hit the execution timeout limit.
- Warn if your Lambda function is low on memory.
- Reports unhandled promise rejections.
- Reports uncaught exceptions.
- Serverless, Sentry and as well as this library are all Open Source. Yay! 🎉
- TypeScript support
Sentry integration splits into two components:
- This plugin, which simplifies installation with the Serverless Framework
- Theserverless-sentry-lib, which performs the runtime monitoring and error reporting.
For a detailed overview of how to use theserverless-sentry-lib referto itsREADME.md.
Install the
@sentry/nodemodule as aproduction dependency (so it gets packaged together with your source code):npm install --save @sentry/node
Install theserverless-sentry-lib as aproduction dependency as well:
npm install --save serverless-sentry-lib
Install this plugin as adevelopment dependency (you don't want to package it with your release artifacts):
npm install --save-dev serverless-sentry
Check out the examples below on how to integrate it with your projectby updating
serverless.ymlas well as your Lambda handler code.
TheServerless Sentry Plugin allows configuration of the library through theserverless.ymland will create release and deployment information for you (if wanted). This is the recommended way of using theserverless-sentry-lib library.
The plugin determines your environment during deployment and adds theSENTRY_DSN environment variables to your Lambda function. All you need todo is to load the plugin and set thedsn configuration option as follows:
service:my-serverless-projectprovider:# ...plugins: -serverless-sentrycustom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzz# URL provided by Sentry
The actual reporting to Sentry happens in platform-specific libraries. Currently, only Node.js and Python are supported.
Each library provides awithSentry helper that acts as decorators around your original AWS Lambda handler code and is configured via this plugin or manually through environment variables.
For more details refer to the individual libraries' repositories:
- Node.js:serverless-sentry-lib
Old, now unsupported libraries:
For maximum flexibility, this library is implemented as a wrapper around your original AWS Lambda handler code (yourhandler.js or similar function). ThewithSentry higher-order function adds error and exception handling and takes care of configuring the Sentry client automatically.
withSentry is pre-configured to reasonable defaults and doesn't need any configuration. It will automatically load and configure@sentry/node which needs to be installed as a peer dependency.
Original Lambda Handler Code:
exports.handler=asyncfunction(event,context){console.log("EVENT: \n"+JSON.stringify(event,null,2));returncontext.logStreamName;};
New Lambda Handler Code UsingwithSentry For Sentry Reporting
constwithSentry=require("serverless-sentry-lib");// This helper libraryexports.handler=withSentry(asyncfunction(event,context){console.log("EVENT: \n"+JSON.stringify(event,null,2));returncontext.logStreamName;});
ES6 Module: Original Lambda Handler Code:
exportasyncfunctionhandler(event,context){console.log("EVENT: \n"+JSON.stringify(event,null,2));returncontext.logStreamName;}
ES6 Module: New Lambda Handler Code UsingwithSentry For Sentry Reporting
importwithSentryfrom"serverless-sentry-lib";// This helper libraryexportconsthandler=withSentry(async(event,context)=>{console.log("EVENT: \n"+JSON.stringify(event,null,2));returncontext.logStreamName;});
Once your Lambda handler code is wrapped inwithSentry, it will be extended with automatic error reporting. Whenever your Lambda handler sets an error response, the error is forwarded to Sentry with additional context information. For more details about the different configuration options available refer to theserverless-sentry-lib documentation.
Configure the Sentry plugin using the following options in yourserverless.yml:
dsn- Your Sentry project's DSN URL (required)enabled- Specifies whether this SDK should activate and send events to Sentry (defaults totrue)environment- Explicitly set the Sentry environment (defaults to theServerless stage)
In order for some features such as releases and deployments to work,you need to grant API access to this plugin by setting the following options:
organization- Organization nameproject- Project nameauthToken- API authentication token withproject:writeaccess
👉Important: You need to make sure you’re usingAuth Tokens not API Keys, which are deprecated.
Releases are used by Sentry to provide you with additional context when determining the cause of an issue. The plugin can automatically create releases for you and tag all messages accordingly. To find out more about releases inSentry, refer to theofficial documentation.
In order to enable release tagging, you need to set therelease option in yourserverless.yml:
custom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzzorganization:my-sentry-organziationproject:my-sentry-projectauthToken:my-sentry-api-keyrelease:version:<RELEASE VERSION>refs: -repository:<REPOSITORY NAME>commit:<COMMIT HASH>previousCommit:<COMMIT HASH>
version- Set the release version used in Sentry. Use any of the below values:git- Uses the current git commit hash or tag as release identifier.random- Generates a random release during deployment.true- First tries to determine the release viagitand falls back torandomif Git is not available.false- Disable release versioning.- any fixed string - Use a fixed string for the release. Serverless variables are allowed.
refs- If you have set up Sentry to collect commit data, you can use commit refs to associate your commits with your Sentry releases. Refer to theSentry Documentation for details about how to use commit refs. If you set yourversiontogit(ortrue), therefsoptions are populated automatically and don't need to be set.
👉Tip {"refs":["Invalid repository names: xxxxx/yyyyyyy"]}: If your repository provider is not supported by Sentry (currently only GitHub orGitlab with Sentry Integrations) you have the following options:
- set
refs: false, this will not automatically population the refs but also dismisses your commit id as version - set
refs: trueandversion: trueto populate the version with the commit short id
If you don't specify any refs, you can also use the short notation forrelease and simply set it to the desired release version as follows:
custom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzzrelease:<RELEASE VERSION>
If you don't need or want the plugin to create releases and deployments, you can omit theauthToken,organization andproject options. Messages and exceptions sent by your Lambda functions will still be tagged with the release version and show up grouped in Sentry nonetheless.
👉Pro Tip: The possibility to use a fixed string in combination with Serverless variables allows you to inject your release version through the command line, e.g. when running on your continuous integration machine.
custom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzzorganization:my-sentry-organziationproject:my-sentry-projectauthToken:my-sentry-api-keyrelease:version:${opt:sentryVersion}refs: -repository:${opt:sentryRepository}commit:${opt:sentryCommit}
And then deploy your project using the command-line options from above:
sls deploy --sentryVersion 1.0.0 --sentryRepository foo/bar --sentryCommit 2da95dfb
👉Tip when using Sentry with multiple projects: Releases in Sentry are specific to the organization and can span multiple projects. Take this in consideration when choosing a version name. If your version applies to the current project only, you should prefix it with your project name.
If no option forrelease is provided, releases and deployments aredisabled.
Sourcemap files can be uploaded to Sentry to display source files in the stack traces rather than the compiled versions. This only uploads existing filesbeing output, you'll need to configure your bundling tool separately. You'll also need to have releases configured, see above.
Default options:
custom:sentry:sourceMaps:true
Add custom prefix (required if your app is not at the filesystem root)
custom:sentry:sourceMaps:urlPrefix:/var/task
In addition, you can configure the Sentry error reporting on a service as well as a per-function level. For more details about the individual configuration options see theserverless-sentry-lib documentation.
autoBreadcrumbs- Automatically create breadcrumbs (see Sentry Raven docs, defaults totrue)filterLocal- Don't report errors from local environments (defaults totrue)captureErrors- Capture Lambda errors (defaults totrue)captureUnhandledRejections- Capture unhandled Promise rejections (defaults totrue)captureUncaughtException- Capture unhandled exceptions (defaults totrue)captureMemoryWarnings- Monitor memory usage (defaults totrue)captureTimeoutWarnings- Monitor execution timeouts (defaults totrue)
# serverless.ymlservice:my-serverless-projectprovider:# ...plugins: -serverless-sentrycustom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzz# URL provided by SentrycaptureTimeoutWarnings:false# disable timeout warnings globally for all functionsfunctions:FuncFoo:handler:Foo.handlerdescription:Hello Worldsentry:captureErrors:false# Disable error capturing for this specific function onlycaptureTimeoutWarnings:true# Turn timeout warnings back onFuncBar:handler:Bar.handlersentry:false# completely turn off Sentry reporting
In some cases, it might be desired to use a different Sentry configuration depending on the currently deployed stage. To make this work we can use a built-inServerless variable resolutions trick:
# serverless.ymlplugins: -serverless-sentrycustom:config:default:sentryDsn:""prod:sentryDsn:"https://xxxx:yyyy@sentry.io/zzzz"# URL provided by Sentrysentry:dsn:${self:custom.config.${self:provider.stage}.sentryDsn, self:custom.config.default.sentryDsn}captureTimeoutWarnings:false# disable timeout warnings globally for all functions
Double-check the DSN settings in yourserverless.yml and compare it with what Sentry shows you in your project settings under "Client Keys (DSN)". You need a URL in the following format - see theSentry Quick Start:
{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}Also, make sure to add the plugin to your plugins list in theserverless.yml:
plugins: -serverless-sentrycustom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzz# URL provided by Sentry
Make sure to set theauthToken,organization as well asproject options in yourserverless.yml, and setrelease to a non-empty value as shown in the example below:
plugins: -serverless-sentrycustom:sentry:dsn:https://xxxx:yyyy@sentry.io/zzzz# URL provided by Sentryorganization:my-sentry-organziationproject:my-sentry-projectauthToken:my-sentry-api-keyrelease:git
Check out thefilterLocal configuration setting. If you test Sentry locally and want to make sure your messages are sent, set this flag tofalse. Once done testing, don't forget to switch it back totrue as otherwise, you'll spam your Sentry projects with meaningless errors of local code changes.
- Increased number of parallel uploads of source map artifacts for better performance.
- Upload multiple source map artifacts in parallel for better performance.
- Fix#63: Upload source maps serially to avoid running out of sockets.
- Correctly disable uploading source maps if the config setting is
falseor unset.
- Added support for uploading Source Maps to Sentry. Thanks tojonmast for the contribution.
- Fixed an issue in the configuration validation. Thanks toDonaldoLog for the fix.
- Fixed an issue if using
- Updated dependencies.
- Explicitly check for
enabledflag. Thanks toaaronbannin for the contribution. - Explicit peer dependency on Serverless
- Updated dependencies minor versions; locked TypeScript to 4.4 for now
- Added configuration validation. Serverless will now warn if you pass an invalid configuration value in
custom.sentry.
- Added
captureUncaughtExceptionconfiguration option. This already exists inserverless-sentry-libbut was never exposed in the plugin. - Don't fail if
SENTRY_DSNis not set but simply disable Sentry integration.
- Support for deploying individual functions only (
sls deploy -f MyFunction). Thanks todominik-meissner! - Improved documentation. Thanks toaheissenberger
- Updated dependencies.
- Fixed custom release names not being set. Thanks tobotond-veress!
- Fixed error when creating new Sentry releases. Thanks todryror!
- This version of
serverless-sentry-pluginrequires the use ofserverless-sentry-libv2.x.x - Rewrite using TypeScript. The use of TypeScript in your project is fully optional, but if you do, we got you covered!
- Added new default uncaught exception handler.
- Dropped support for Node.js 6 and 8. The only supported versions are Node.js 10 and 12.
- Upgrade from Sentry SDK
ravento theUnified Node.js SDK@sentry/node. - Simplified integration using
withSentryhigher-order function. Passing the Sentry instance is now optional. - Thank you@aheissenberger and@Vadorequest for their contributions to this release! 🤗
- Fixed a compatibility issue with Serverless 1.28.0.
- Support for
sls invoke local. Thanks tosifrenettefor his contribution.
⚠️ Dropped support for Node 4.3. AWS deprecates Node 4.3 starting July 31, 2018.- Pair with
serverless-sentry-libv1.1.x.
- Version falls back to git hash if no tag is set for the current head (#15).
- Fixed reporting bugs in the local environment despite config telling otherwise (#17).This requires an update of
serverless-sentry-libas well!
- Fixed an issue with creating random version numbers
- Allow disabling Sentry for specific functions by settings
sentry: falseintheserverless.yml. - Added support for theServerless Offline Plugin.
- Fixed an issue with the plugin not being initialized properly when deployingan existing artifact.
- First official release of this plugin for use with Serverless 1.x
- For older versions of this plugin that work with Serverless 0.5, seehttps://github.com/arabold/serverless-sentry-plugin/tree/serverless-0.5
- Bring back automatic instrumentation of the Lambda code during packaging
- Provide CLI commands to create releases and perform other operations in Sentry
- Ensure all exceptions and messages have been sent to Sentry before returning; see#338.
That you for supporting me and my projects.
About
This plugin adds automatic forwarding of errors and exceptions to Sentry (https://sentry.io) and Serverless (https://serverless.com)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.
