Official Sentry SDK for various serverless solutions including AWS Lambda and Google Cloud Functions
npx @tessl/cli install tessl/npm-sentry--serverless@7.120.0Sentry Serverless is an official SDK that provides comprehensive error monitoring and performance tracing capabilities specifically designed for serverless environments including AWS Lambda and Google Cloud Functions. It wraps the core @sentry/node SDK with added functionality for serverless platforms, enabling automatic error capture, performance monitoring, and distributed tracing.
npm install @sentry/serverlessimport * as Sentry from "@sentry/serverless";For namespace-specific imports:
import { AWSLambda, GCPFunction } from "@sentry/serverless";For CommonJS:
const Sentry = require("@sentry/serverless");
const { AWSLambda, GCPFunction } = require("@sentry/serverless");import * as Sentry from "@sentry/serverless";
// Initialize Sentry for AWS Lambda
Sentry.AWSLambda.init({
dsn: "your-dsn-here",
tracesSampleRate: 1.0,
});
// Wrap your handler
exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
// Your handler logic
throw new Error("Something went wrong!");
});import * as Sentry from "@sentry/serverless";
// Initialize Sentry for GCP Functions
Sentry.GCPFunction.init({
dsn: "your-dsn-here",
tracesSampleRate: 1.0,
});
// HTTP Functions
exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
res.send("Hello World!");
});
// Event Functions
exports.helloEvents = Sentry.GCPFunction.wrapEventFunction((data, context, callback) => {
callback();
});Sentry Serverless is built around several key architectural components:
AWSLambda and GCPFunction namespaces provide platform-specific functionalityComplete AWS Lambda support with handler wrapping, automatic context enhancement, and AWS services integration.
namespace AWSLambda {
function init(options?: AWSLambdaOptions): void;
function wrapHandler<TEvent, TResult>(
handler: Handler<TEvent, TResult>,
wrapOptions?: Partial<AWSLambdaWrapperOptions>
): Handler<TEvent, TResult>;
function getDefaultIntegrations(options: Options): Integration[];
}
interface AWSLambdaOptions extends NodeOptions {
_invokedByLambdaLayer?: boolean;
}Support for HTTP functions, event functions, and cloud event functions with automatic tracing and error capture.
namespace GCPFunction {
function init(options?: NodeOptions): void;
function wrapHttpFunction(
fn: HttpFunction,
wrapOptions?: Partial<HttpFunctionWrapperOptions>
): HttpFunction;
function wrapEventFunction(
fn: EventFunction | EventFunctionWithCallback,
wrapOptions?: Partial<EventFunctionWrapperOptions>
): EventFunctionWithCallback;
function wrapCloudEventFunction(
fn: CloudEventFunction | CloudEventFunctionWithCallback,
wrapOptions?: Partial<CloudEventFunctionWrapperOptions>
): CloudEventFunctionWithCallback;
function getDefaultIntegrations(options: Options): Integration[];
}Google Cloud Functions Integration
All core Sentry capabilities for error tracking, performance monitoring, and context management.
// Initialization
function init(options?: NodeOptions): void;
// Error Capture
function captureException(exception: any, scope?: (scope: Scope) => Scope): string;
function captureMessage(message: string, level?: SeverityLevel, scope?: (scope: Scope) => Scope): string;
// Performance Monitoring
function startSpan<T>(context: SpanContext, fn: (span: Span) => T): T;
function startSpanManual(context: SpanContext, fn: (span: Span) => T): T;
// Context Management
function withScope<T>(callback: (scope: Scope) => T): T;
function getCurrentScope(): Scope;Automatic tracking and tracing of AWS SDK service requests with detailed operation context.
function awsServicesIntegration(options?: { optional?: boolean }): Integration;// AWS Lambda Types
type AsyncHandler<T extends Handler> = (
event: Parameters<T>[0],
context: Parameters<T>[1]
) => Promise<NonNullable<Parameters<Parameters<T>[2]>[1]>>;
interface AWSLambdaWrapperOptions {
flushTimeout: number;
callbackWaitsForEmptyEventLoop: boolean;
captureTimeoutWarning: boolean;
timeoutWarningLimit: number;
captureAllSettledReasons: boolean;
startTrace: boolean;
}
// Google Cloud Functions Types
interface HttpFunction {
(req: Request, res: Response): any;
}
interface EventFunction {
(data: Record<string, any>, context: Context): any;
}
interface EventFunctionWithCallback {
(data: Record<string, any>, context: Context, callback: Function): any;
}
interface CloudEventFunction {
(cloudevent: CloudEventsContext): any;
}
interface CloudEventFunctionWithCallback {
(cloudevent: CloudEventsContext, callback: Function): any;
}
// Context Types
interface CloudFunctionsContext {
eventId?: string;
timestamp?: string;
eventType?: string;
resource?: string;
}
interface CloudEventsContext {
[key: string]: any;
type?: string;
specversion?: string;
source?: string;
id?: string;
time?: string;
schemaurl?: string;
contenttype?: string;
}
type Context = CloudFunctionsContext | CloudEventsContext;
// GCP Function Wrapper Options
interface GCPWrapperOptions {
flushTimeout: number;
}
interface HttpFunctionWrapperOptions extends GCPWrapperOptions {
addRequestDataToEventOptions?: AddRequestDataToEventOptions;
}
type EventFunctionWrapperOptions = GCPWrapperOptions;
type CloudEventFunctionWrapperOptions = GCPWrapperOptions;