'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 1:[1]
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
}
Solution 2:[2]
Or, you can write your code to convert standard trace id(String) to xray trace id.
private static final String TRACE_ID_VERSION = "1";
private static final char TRACE_ID_DELIMITER = '-';
private static final int TRACE_ID_DELIMITER_INDEX_1 = 1;
private static final int TRACE_ID_DELIMITER_INDEX_2 = 10;
private static final int TRACE_ID_FIRST_PART_LENGTH = 8;
public static String toXRayTraceId(String traceId) {
return TRACE_ID_VERSION
+ TRACE_ID_DELIMITER
+ traceId.substring(0, TRACE_ID_FIRST_PART_LENGTH)
+ TRACE_ID_DELIMITER
+ traceId.substring(TRACE_ID_FIRST_PART_LENGTH);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Lei Wang |