how to generate X-Amzn-Trace-Id for Golang in the simplest way

Issue

I have been seeing this documentation by AWS

Is there any simple way to generate "X-Amzn-Trace-Id" with X-Ray?
the func NewIDGenerator() doesn’t produce the format of Root xxx.

or can we just use a trusted library for it? Thank you

Solution

First Create a Trace using OpenTelemetry’s tracer and then inject that context to XRAY Propagator to get TraceId as per AWS’s ID specification.

func GetAmazonTraceId(ctx context.Context) string {
    propogator := xray.Propagator{}
    carrier := propagation.HeaderCarrier{}
    propogator.Inject(ctx, carrier)
    traceId := carrier.Get("X-Amzn-Trace-Id")
    return traceId
}

Answered By – Bharat Gadde

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.