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
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
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
}
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);
}