The provider service interface used by the Tracer. This interface defines the methods and properties that a provider service must implement.
Add ColdStart annotation to the current segment or subsegment.
If Tracer has been initialized outside the Lambda handler then the same instance
of Tracer will be reused throughout the lifecycle of that same Lambda execution environment
and this method will annotate ColdStart: false
after the first invocation.
Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
If you want to patch a specific client use captureAWSClient and if you are using AWS SDK v3 use captureAWSv3Client instead.
AWS SDK v2 import
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
const AWS = tracer.captureAWS(require('aws-sdk'));
export const handler = async (_event: unknown, _context: unknown) => {
...
}
Use captureAWSv3Client instead.
Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service.
If you want to patch all clients use captureAWS and if you are using AWS SDK v3 use captureAWSv3Client instead.
AWS SDK v2 client
import { S3 } from 'aws-sdk';
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
const s3 = tracer.captureAWSClient(new S3({ apiVersion: '2006-03-01' }));
export const handler = async (_event: unknown, _context: unknown) => {
...
}
Use captureAWSv3Client instead.
Patch an AWS SDK v3 client and create traces when your application makes calls to that AWS service.
If you are using AWS SDK v2 use captureAWSClient instead.
AWS SDK v3 client
import { S3Client } from '@aws-sdk/client-s3';
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
const client = new S3Client({});
tracer.captureAWSv3Client(client);
export const handler = async (_event: unknown, _context: unknown) => {
...
}
A decorator automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
Using this decorator on your handler function will automatically:
ColdStart
annotationNote: Currently TypeScript only supports decorators on classes and methods. If you are using the function syntax, you should use the middleware instead.
Optional
options: CaptureLambdaHandlerOptions(optional) Options for the decorator
import { Tracer } from '@aws-lambda-powertools/tracer';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
class Lambda implements LambdaInterface {
@tracer.captureLambdaHandler()
public handler(_event: unknown, _context: unknown) {
// ...
}
}
const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);
A decorator automating capture of metadata and annotations on segments or subsegments for an arbitrary function.
Using this decorator on your function will automatically:
Note: Currently TypeScript only supports decorators on classes and methods. If you are using the function syntax, you should use the middleware instead.
Optional
options: CaptureMethodOptions(optional) Options for the decorator
import { Tracer } from '@aws-lambda-powertools/tracer';
import { LambdaInterface } from '@aws-lambda-powertools/commons';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
class Lambda implements LambdaInterface {
@tracer.captureMethod()
public myMethod(param: string) {
// ...
}
public handler(_event: unknown, _context: unknown) {
this.myMethod('foo');
}
}
const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);;
Protected
getGet the current root AWS X-Ray trace id.
Utility method that returns the current AWS X-Ray Root trace id. Useful as correlation id for downstream processes.
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
export const handler = async () => {
try {
...
} catch (err) {
const rootTraceId = tracer.getRootXrayTraceId();
// Example of returning an error response
return {
statusCode: 500,
// Include the rootTraceId in the response so we can show a "contact support" button that
// takes the customer to a customer service form with the trace as additional context.
body: `Internal Error - Please contact support and quote the following id: ${rootTraceId}`,
headers: { '_X_AMZN_TRACE_ID': rootTraceId },
};
}
}
Protected
isAdd metadata to existing segment or subsegment.
Metadata key
Value for metadata
Optional
namespace: stringNamespace that metadata will lie under, if none is passed it will use the serviceName
Set the passed subsegment as the current active subsegment.
If you are using a middleware or a decorator this is done automatically for you.
Subsegment to set as the current segment
import { Tracer } from '@aws-lambda-powertools/tracer';
import { Subsegment } from 'aws-xray-sdk-core';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
export const handler = async (_event: unknown, _context: unknown) => {
const subsegment = new Subsegment('### foo.bar');
tracer.setSegment(subsegment);
}
Intro
Tracer is an opinionated thin wrapper for AWS X-Ray SDK for Node.js.
Tracing data can be visualized through AWS X-Ray Console.
Key features
Usage
For more usage examples, see our documentation.
Functions usage with middleware
If you use function-based Lambda handlers you can use the Tracer.captureLambdaHandler middy middleware to automatically:
ServiceName
andColdStart
annotationsExample
Object oriented usage with decorators
If instead you use TypeScript Classes to wrap your Lambda handler you can use the Tracer.captureLambdaHandler decorator to automatically:
ServiceName
andColdStart
annotationsExample
Functions usage with manual instrumentation
If you prefer to manually instrument your Lambda handler you can use the methods in the tracer class directly.
Example