Search code examples
gosdkgeneratoraws-xrayopen-telemetry

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


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
    }