Description
Description of the feature request
Problem statement
When handling application errors, I want to embed the XRay Trace ID in the payload & error message returned by my API so that customers can cite the trace ID in support requests, and so our development team can easily look up the trace from a customer service request via AWS XRay's web console.
Summary of the feature
I'd like the tracer to provide the root trace id so that I can access it in my (otherwise) unhandled exceptions handler when I construct a user-facing error message.
Code examples
const rootTraceId = tracer.getRootTraceId();
// Example of returning an error with Express.js
res.status(500).json({
error: `Internal Error - Please contact support and quote the following id: ${rootTraceId}`,
// 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.
rootTraceId,
});
Benefits for you and the wider AWS community
Makes it easier for the wider community to expose trace IDs to their customers so that developers can look up the conditions and contexts of the errors reported by their customers.
Describe alternatives you've considered
For now, we've implemented this by parsing out the Root=
traceid from process.env['_X_AMZN_TRACE_ID']
, but our preference would be to depend on Powertools for this.
export function getTraceId(): string | undefined {
const traceId = process.env['_X_AMZN_TRACE_ID'];
if (!traceId) return;
return parseTraceId(traceId);
}
export function parseTraceId(traceId: string): string | undefined {
if (traceId.includes('Root=')) {
// Example:
// Root=1-6303d71b-63c75c564e336c9c2cffc1d5;Parent=58e97d4cc1ed1447;Sampled=1
const [rootTraceId] = traceId
.split(';')
.filter((segment) => segment.startsWith('Root='))
.map((segment) => {
const [_, rootTraceId] = segment.split('=');
return rootTraceId;
});
if (rootTraceId) {
// 1-6303d71b-63c75c564e336c9c2cffc1d5
return rootTraceId;
}
}
return traceId;
}
Additional context
None
Related issues, RFCs
None