在Golang和X射线中创建子段

pcww981p  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(84)

我试图在golang和lambda中创建子段,但它不适合我:
这是我的代码:

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-xray-sdk-go/xray"
)

func handler(ctx context.Context, event int) (string, error) {

    awsContext, segment := xray.BeginSegment(ctx, "service-A")

    segment.Close(nil)

    sumResult, err := performSum(awsContext, event, event)
    if err != nil {
        return "", err
    }

    return fmt.Sprintf("Resultado: %d", sumResult), nil
}

func performSum(ctx context.Context, num1 int, num2 int) (int, error) {

    _, subSeg := xray.BeginSubsegment(ctx, "service-B")
    myMap := make(map[string]interface{})
    myMap["test"] = "test value"
    subSeg.Annotations = myMap
    defer subSeg.Close(nil)

    return num1 + num2, nil
}

func main() {

    lambda.Start(handler)
}

段被创建但子段没有,y尝试使用xRay。捕获和我有相同的结果。
在我的X射线控制台中:
(Client)->(My lambda Lambda Context)->(My lambda Lambda Function)
(客户端)->(服务-A)
我不知道为什么我的父段不是我的lambda,是其他客户端,我不知道为什么我的子段没有出现。
我已经阅读了所有的AWS帖子,所有的人都这样做,但我不明白我的基本示例发生了什么。
这是使用Capture的代码,但只有在控制台中才会出现Segment:
(Client)->(My lambda Lambda Context)->(My lambda Lambda Function)
(客户端)->(测试)
我以SDK测试为例:
https://github.com/aws/aws-xray-sdk-go/blob/master/xray/capture_test.go
但它不起作用!

func handler(ctx context.Context, event int) (string, error) {

    logger.InitAppContext(&ctx)

    ctx, root := xray.BeginSegment(ctx, "Test")
    err := xray.Capture(ctx, "TestService", func(context.Context) error {

        logger.Info(ctx, "Inside Capture")
        time.Sleep(2 * time.Second)

        root.Close(nil)
        return nil
    })

    return fmt.Sprintf("Resultado: %d", 0), err
}

func main() {

    lambda.Start(handler)
}

我做的另一件事是用xray创建一个HTTP请求,因为使用HTTP请求xray自动创建子段,它可以工作,然后在SDK库中我包括一些日志
这是带有Http请求的Json段(它工作,子段出现在控制台中):

{
   "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
   "id":"c150f7dc4062e071",
   "name":"www.google.com",
   "start_time":1692203575.1287372,
   "end_time":1692203575.8716385,
   "parent_id":"37a80e6fee94619c",
   "type":"subsegment",
   "namespace":"remote",
   "http":{
      "request":{
         "method":"GET",
         "url":"https://www.google.com"
      },
      "response":{
         "status":200
      }
   },
   "subsegments":[
      {
         "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
         "id":"a5fb68c55cd525a5",
         "name":"connect",
         "start_time":1692203575.128795,
         "end_time":1692203575.3913538,
         "parent_id":"37a80e6fee94619c",
         "metadata":{
            "http":{
               "connection":{
                  "reused":false,
                  "was_idle":false
               }
            }
         },
         "subsegments":[
            {
               "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
               "id":"57008dcea715c1f2",
               "name":"dns",
               "start_time":1692203575.1288614,
               "end_time":1692203575.133045,
               "parent_id":"37a80e6fee94619c",
               "metadata":{
                  "http":{
                     "dns":{
                        "addresses":[
                           {
                              "IP":"172.253.63.147",
                              "Zone":""
                           },
                           {
                              "IP":"172.253.63.99",
                              "Zone":""
                           },
                           {
                              "IP":"172.253.63.103",
                              "Zone":""
                           },
                           {
                              "IP":"172.253.63.105",
                              "Zone":""
                           },
                           {
                              "IP":"172.253.63.104",
                              "Zone":""
                           },
                           {
                              "IP":"172.253.63.106",
                              "Zone":""
                           },
                           {
                              "IP":"2607:f8b0:4004:c09::63",
                              "Zone":""
                           },
                           {
                              "IP":"2607:f8b0:4004:c09::6a",
                              "Zone":""
                           },
                           {
                              "IP":"2607:f8b0:4004:c09::68",
                              "Zone":""
                           },
                           {
                              "IP":"2607:f8b0:4004:c09::67",
                              "Zone":""
                           }
                        ],
                        "coalesced":false
                     }
                  }
               },
               "Dummy":false
            },
            {
               "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
               "id":"c39998ed3d0d880d",
               "name":"dial",
               "start_time":1692203575.133106,
               "end_time":1692203575.1430182,
               "parent_id":"37a80e6fee94619c",
               "metadata":{
                  "http":{
                     "connect":{
                        "network":"tcp"
                     }
                  }
               },
               "Dummy":false
            },
            {
               "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
               "id":"54572f92d93d9010",
               "name":"tls",
               "start_time":1692203575.1441321,
               "end_time":1692203575.3912807,
               "parent_id":"37a80e6fee94619c",
               "metadata":{
                  "http":{
                     "tls":{
                        "cipher_suite":4865,
                        "did_resume":false,
                        "negotiated_protocol":"h2",
                        "negotiated_protocol_is_mutual":true
                     }
                  }
               },
               "Dummy":false
            }
         ],
         "Dummy":false
      },
      {
         "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
         "id":"0451ff84d78b5450",
         "name":"request",
         "start_time":1692203575.3913682,
         "end_time":1692203575.3914084,
         "parent_id":"37a80e6fee94619c",
         "Dummy":false
      },
      {
         "trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
         "id":"99487356836991ff",
         "name":"response",
         "start_time":1692203575.3914256,
         "end_time":1692203575.8714898,
         "parent_id":"37a80e6fee94619c",
         "Dummy":false
      }
   ],
   "Dummy":false
}

但这是使用我分享的BeginSegment代码构建的请求的Json:

{
    "trace_id": "1-64de1eea-85ad2d01b432e20eb6440b91",
    "id": "6ded59db0c459e1a",
    "name": "service-A",
    "start_time": 1692278506.7173197,
    "end_time": 1692278506.7180274,
    "aws": {
        "xray": {
            "sdk_version": "1.8.1",
            "sdk": "X-Ray for Go"
        }
    },
    "service": {
        "version": "1.2.3",
        "runtime_version": "go1.19.2",
        "runtime": "gc"
    },
    "annotations": {
        "parTest": "test parent"
    },
    "subsegments": [
        {
            "trace_id": "1-64de1eea-85ad2d01b432e20eb6440b91",
            "id": "aa38e4906cab13b9",
            "name": "service-B",
            "start_time": 1692278506.7177432,
            "end_time": 1692278506.7180173,
            "parent_id": "6ded59db0c459e1a",
            "annotations": {
                "test": "test value"
            },
            "Dummy": false
        }
    ],
    "Dummy": false
}

因为在埃尔塞贡多没有父母的身份,他也不会有什么影响

wmvff8tz

wmvff8tz1#

Lambda应该为您创建Segment,这意味着您不应该自己创建Segment。
对于下面的行,您应该将其删除或将其更改为子段(此子段应该在performSum()之后“关闭”)。
awsContext,segment:= xray.BeginSegment(ctx,“service-A”)
进行此更改后,子段应开始出现在(My lambda Lambda Function)节点中。

相关问题