douhuzhi0907 2018-03-21 00:38
浏览 65

AWS X-Ray GoLang Lambda到lambda跟踪并显示在服务地图中

I have the API Gateway that calls Lamdba function 1 and that invokes lambda function 2 in Go. I want to see these 2 functions joined in the service map.

The only way i have been able to do this so far is to create a custom segment eg called "parent" and the create a subsegment from this context eg called "child". Then using client.InvokeWithContext to invoke the function 2 passing the "child" segment context.

sess := session.Must(session.NewSession())
client := lambda.New(sess, &aws.Config{Region: aws.String(region)})

xray.Configure(xray.Config{LogLevel: "trace"})

ctx, seg := xray.BeginSegment(context.Background(), "Parent")
ctx, subseg := xray.BeginSubsegment(ctx, "Child")
result, _ := client.InvokeWithContext(ctx, 
    lambda.InvokeInput{FunctionName: aws.String(functionName), Payload: nil})

Problem is that this creates trace parent -> child in sevice map but also has function 1 too.

What is the best way to join these 2 functions on the service map please ? Note. I have more than 2 that i want to see linked up on the service map to show me my whole flow through lambdas.

Please help.

Thanks Rick

  • 写回答

2条回答 默认 最新

  • doufu3718 2018-03-30 13:48

    This Go and Lambda boilerplate app demonstrates Lambda to Lambda traces on the X-Ray service map:

    Resulting X-Ray Service Map

    WorkCreateFunction (function 1) is an API Gateway handler function. It calls WorkerFunction (function 2) via a Lambda.InvokeWithContext call.

    The trick is to instrument the Lambda API client with xray before making Lambda API calls:

    // Lambda is an xray instrumented Lambda client
    func Lambda() *lambda.Lambda {
        c := lambda.New(sess)
        return c
    out, err := Lambda().InvokeWithContext(ctx, &lambda.InvokeInput{
        FunctionName:   aws.String(os.Getenv("WORKER_FUNCTION_NAME")),
        InvocationType: aws.String("Event"), // async
    if err != nil {
        return responseEmpty, errors.WithStack(err)

    The aws-xray-sdk-go copies the X-Amzn-Trace-Id header from function 1 into the Lambda API request for function 2:

    If this is not working, try updating to the latest aws-xray-sdk-go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 recipe通过gem协议传的是什么
  • ¥15 TS2307: Cannot find module 'cc'.
  • ¥15 100小时学会sap 书上pp章节5.22,标准成本计算逻辑?
  • ¥50 达梦数据库误删日志文件重做DAMENG01.log启动仍然-712错误
  • ¥15 cellranger化学处理类型报错
  • ¥15 用texstudio插入图片出现下面情况,怎么办
  • ¥15 ubantu 用samba挂载windows的共享文件夹,无法挂载二级目录和修改文件
  • ¥15 有没有会五轴RTCP算法,双转台AC结构。
  • ¥25 对于LSTM实践问题的疑问
  • ¥15 PHP中关于排名和显示的问题