Skip to content

Commit a355f7b

Browse files
committed
docs: added docs section for getRootXrayTraceId
1 parent 8d3fa28 commit a355f7b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/core/tracer.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,38 @@ Use **`POWERTOOLS_TRACER_CAPTURE_ERROR=false`** environment variable to instruct
484484

485485
1. You might **return sensitive** information from exceptions, stack traces you might not control
486486

487+
### Access AWS X-Ray Root Trace ID
488+
489+
Tracer exposes a `getRootXrayTraceId()` method that allows you to retrieve the [AWS X-Ray Root Trace ID](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces) corresponds to the current function execution.
490+
491+
!!! info "This is commonly useful in two scenarios"
492+
493+
1. By including the root trace id in your response consumers can use it to correlate requests
494+
2. You might want to surface the root trace id to your end users so that they can reference it while contacting customer service
495+
496+
=== "index.ts"
497+
498+
```typescript hl_lines="9"
499+
import { Tracer } from '@aws-lambda-powertools/tracer';
500+
501+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
502+
503+
export const handler = async (event: unknown, context: Context): Promise<void> => {
504+
try {
505+
...
506+
} catch (err) {
507+
const rootTraceId = tracer.getRootXrayTraceId();
508+
509+
// Example of returning an error response
510+
return {
511+
statusCode: 500,
512+
body: `Internal Error - Please contact support and quote the following id: ${rootTraceId}`,
513+
headers: { "_X_AMZN_TRACE_ID": rootTraceId },
514+
};
515+
}
516+
};
517+
```
518+
487519
### Escape hatch mechanism
488520

489521
You can use `tracer.provider` attribute to access all methods provided by the [AWS X-Ray SDK](https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/AWSXRay.html).

0 commit comments

Comments
 (0)