0

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

rulisastra
  • 311
  • 1
  • 2
  • 14
  • What do you mean by "doesn't produce the format of Root xxx"? – Srikanth Chekuri Feb 22 '22 at 08:02
  • @SrikanthChekuri it should look like this `X-Amzn-Trace-Id: Root=1-67891233-abcdef012345678912345678` from this doc https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html – rulisastra Feb 24 '22 at 01:21
  • That is context propagation not id generation. Are you looking for how to generate ids or propagate context? – Srikanth Chekuri Feb 24 '22 at 09:49
  • i am looking for something to generate an "X-Amzn-Trace-Id". cmiiw it could be produce by generating a Trace Id first, then propagate it? @SrikanthChekuri – rulisastra Feb 25 '22 at 06:44

2 Answers2

2

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
}
Bharat Gadde
  • 173
  • 1
  • 10
0

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);
  }
Lei Wang
  • 217
  • 1
  • 4