What is @sentry/serverless?
@sentry/serverless is an npm package that provides error tracking and performance monitoring for serverless applications. It helps developers capture and report errors, exceptions, and performance issues in serverless environments such as AWS Lambda, Google Cloud Functions, and Azure Functions.
What are @sentry/serverless's main functionalities?
Error Tracking
This feature allows you to capture and report errors in your serverless functions. The code sample demonstrates how to initialize Sentry for AWS Lambda and wrap a handler to automatically capture any errors that occur.
const Sentry = require('@sentry/serverless');
Sentry.AWSLambda.init({
dsn: 'your-dsn-url',
});
exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
// Your handler code
throw new Error('Something went wrong!');
});
Performance Monitoring
This feature allows you to monitor the performance of your serverless functions. The code sample demonstrates how to initialize Sentry with performance monitoring enabled and how to create and finish a transaction to measure the duration of a task.
const Sentry = require('@sentry/serverless');
Sentry.AWSLambda.init({
dsn: 'your-dsn-url',
tracesSampleRate: 1.0,
});
exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
// Your handler code
const transaction = Sentry.startTransaction({
op: 'task',
name: 'My Task',
});
// Simulate some work
await new Promise(resolve => setTimeout(resolve, 1000));
transaction.finish();
});
Custom Context
This feature allows you to add custom context to your error reports. The code sample demonstrates how to set tags, user information, and extra data in the Sentry scope before throwing an error.
const Sentry = require('@sentry/serverless');
Sentry.AWSLambda.init({
dsn: 'your-dsn-url',
});
exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
Sentry.configureScope(scope => {
scope.setTag('my-tag', 'my-value');
scope.setUser({ id: 'user-id' });
scope.setExtra('extra-info', 'some extra information');
});
// Your handler code
throw new Error('Something went wrong!');
});
Other packages similar to @sentry/serverless
newrelic
New Relic is a comprehensive monitoring and observability platform that provides error tracking, performance monitoring, and more for various environments, including serverless applications. Compared to @sentry/serverless, New Relic offers a broader range of monitoring capabilities but may be more complex to set up and use.
datadog-lambda-js
Datadog Lambda is a monitoring and analytics platform specifically designed for serverless applications. It provides error tracking, performance monitoring, and custom metrics. Compared to @sentry/serverless, Datadog Lambda offers more specialized features for serverless environments but may require additional configuration and integration with the Datadog platform.
logdna
LogDNA is a log management and analysis platform that can be used to monitor serverless applications. It provides real-time log aggregation, error tracking, and alerting. Compared to @sentry/serverless, LogDNA focuses more on log management and may require additional setup to achieve the same level of error tracking and performance monitoring.
Official Sentry SDK for Serverless environments
Links
General
This package is a wrapper around @sentry/node
, with added functionality related to various Serverless solutions. All
methods available in @sentry/node
can be imported from @sentry/serverless
.
Currently supported environment:
AWS Lambda
To use this SDK, call Sentry.AWSLambda.init(options)
at the very beginning of your JavaScript file.
import * as Sentry from '@sentry/serverless';
Sentry.AWSLambda.init({
dsn: '__DSN__',
});
exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
throw new Error('oh, hello there!');
});
exports.handler = Sentry.AWSLambda.wrapHandler((event, context, callback) => {
throw new Error('oh, hello there!');
});
If you also want to trace performance of all the incoming requests and also outgoing AWS service requests, just set the tracesSampleRate
option.
import * as Sentry from '@sentry/serverless';
Sentry.AWSLambda.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
});
Integrate Sentry using internal extension
Another and much simpler way to integrate Sentry to your AWS Lambda function is to add an official layer.
- Choose Layers -> Add Layer.
- Specify an ARN:
arn:aws:lambda:us-west-1:TODO:layer:TODO:VERSION
. - Go to Environment variables and add:
NODE_OPTIONS
: -r @sentry/serverless/build/npm/cjs/awslambda-auto
.SENTRY_DSN
: your dsn
.SENTRY_TRACES_SAMPLE_RATE
: a number between 0 and 1 representing the chance a transaction is sent to Sentry. For more information, see docs.
Google Cloud Functions
To use this SDK, call Sentry.GCPFunction.init(options)
at the very beginning of your JavaScript file.
import * as Sentry from '@sentry/serverless';
Sentry.GCPFunction.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
});
exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
throw new Error('oh, hello there!');
});
exports.helloEvents = Sentry.GCPFunction.wrapEventFunction((data, context, callback) => {
throw new Error('oh, hello there!');
});
exports.helloEvents = Sentry.GCPFunction.wrapCloudEventFunction((context, callback) => {
throw new Error('oh, hello there!');
});