Add an error to the current segment or subsegment as metadata.
https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
Error to serialize as metadata
Optional
remote: booleanWhether the error was thrown by a remote service. Defaults to false
Add response data to the current segment or subsegment as metadata.
https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-annotations
Optional
data: unknownData to serialize as metadata
Optional
methodName: stringName of the method that is being traced
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.
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
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.
https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
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: any, _context: any) => {
...
}
AWS SDK v2 import
AWS - Instrumented AWS SDK
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.
https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
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: any, _context: any) => {
...
}
AWS SDK v2 client
service - Instrumented AWS SDK v2 client
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.
https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
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: any, _context: any) => {
...
}
AWS SDK v3 client
service - Instrumented AWS SDK v3 client
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.
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.captureLambdaHandler()
public handler(event: any, context: any) {
...
}
}
const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);
Class
Optional
options: CaptureLambdaHandlerOptions(optional) Options for the decorator
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.
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: any) {
...
}
public handler(event: any, context: any) {
...
}
}
const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);;
Class
Optional
options: CaptureMethodOptions(optional) Options for the decorator
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.
https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces
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 },
};
}
}
string - The root X-Ray trace id.
Get the active segment or subsegment (if any) in the current scope.
Usually you won't need to call this method unless you are creating custom subsegments or using manual mode.
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
export const handler = async (_event: any, _context: any) => {
const currentSegment = tracer.getSegment();
... // Do something with segment
}
The active segment or subsegment in the current scope. Will log a warning and return undefined
if no segment is found.
Get the current value of the AWS X-Ray Sampled flag.
Utility method that returns the current AWS X-Ray Sampled flag.
https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces
boolean - true
if the trace is sampled, false
if tracing is disabled or the trace is not sampled.
Protected
isAdds annotation to existing segment or subsegment.
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
export const handler = async (_event: any, _context: any) => {
tracer.putAnnotation('successfulBooking', true);
}
Annotation key
Value for annotation
Adds metadata to existing segment or subsegment.
import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
export const handler = async (_event: any, _context: any) => {
const res = someLogic();
tracer.putMetadata('paymentResponse', res);
}
Metadata key
Value for metadata
Optional
namespace: stringNamespace that metadata will lie under, if none is passed it will use the serviceName
Sets the passed subsegment as the current active subsegment.
If you are using a middleware or a decorator this is done automatically for you.
https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html
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: any, _context: any) => {
const subsegment = new Subsegment('### foo.bar');
tracer.setSegment(subsegment);
}
Subsegment to set as the current segment
Generated using TypeDoc
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 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