简体   繁体   English

如何用最简单的方式为Golang生成X-Amzn-Trace-Id

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

I have been seeing this documentation by AWS我一直在AWS 看到这个文档

Is there any simple way to generate "X-Amzn-Trace-Id" with X-Ray?有什么简单的方法可以用 X-Ray 生成“X-Amzn-Trace-Id”吗? the func NewIDGenerator() doesn't produce the format of Root xxx . func NewIDGenerator()不会生成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.首先使用 OpenTelemetry 的跟踪器创建跟踪,然后将该上下文注入 XRAY Propagator 以根据 AWS 的 ID 规范获取 TraceId。

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.或者,您可以编写代码将标准跟踪 id(String) 转换为 xray 跟踪 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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 x-amzn-trace-id 分配给下游流程 - assign x-amzn-trace-id to downstream process 使用本地放大模拟 api 时出错:请求有一个 'X-Amzn-Trace-Id' HTTP header 保留给 AWS X-Ray 跟踪 header - Getting error when using local amplify mock api: The request has a 'X-Amzn-Trace-Id' HTTP header which is reserved for AWS X-Ray trace header 调用 lambda 时如何记录 X-Amzn-Request-id? (aws-sdk) - How to log X-Amzn-Request-id when invoking a lambda? (aws-sdk) 作为客户端应用程序,我如何获取从 Amazon 负载均衡器 (ALB) 转发给我的“x-amzn-oidc-*”标头? - As a client app, how do I get the 'x-amzn-oidc-*' headers forwaded to me from Amazon Load Balancers (ALB)? 如何在 go 微服务中为每个日志添加跟踪 ID - How to add trace id to each logs in go micro service 如何更改我的 lambda 中的 Xray 跟踪 ID? - How can I change trace id in my lambda for xray? golang:如何使用 make() function 生成二维切片? - golang: how to use make() function to generate 2-demensional slice? 加壳器无法使用过滤器来选择正确的图像 ID (amzn-Linux2) - packer unable use filter to pick the correct image-id (amzn-Linux2) 有没有办法在新的实时数据库中从控制台生成推送 ID? - Is there a way to generate a push ID from the Console in the new Realtime Database? 如何生成一个短 ID 用作 dynamodb 上的密钥? - how to generate a short id to use as key on dynamodb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM