首页 > 解决方案 > aws xray 和 golang http 调用

问题描述

我正在尝试在我的 go 应用程序上使用 awx xray 来对服务进行 http 调用。我只是跟着这个,不确定我是否错过了什么,https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-go-httpclients.html

我像这样进行http调用:

payloadStr, _ := json.Marshal(dxPayload)

fmt.Println("size: ", int(unsafe.Sizeof(bytes.NewBuffer(payloadStr))))

clambda = &http.Client{}

//-------ADDED XRAY HERE-------//
xray.Client(clambda)

reqLambda, errReq := http.NewRequest("POST", lambdaurl, bytes.NewBuffer(payloadStr))
if errReq != nil {
    log.Fatal("Request Error: ", errReq)
    return
}

reqLambda.Header.Add("accept", "application/json;v=1")
reqLambda.Header.Add("cach-control", "no-cache")
reqLambda.Header.Add("content-type", "application/json")
reqLambda.Header.Add("authorization", "Bearer " + devexToken.AccessToken)

respLambda, errResp := clambda.Do(reqLambda)
if errResp != nil {
    log.Fatal("Status Response Error ", errResp)
    return
} else {

}

在我的main.go文件中,我有这个func init()

func init() {

    pConfig = createpConfig()

    //aws xray config
    ss, err := sampling.NewLocalizedStrategyFromFilePath("xray.json")

    if err != nil {
        panic(err)
    }
    xray.Configure(xray.Config{
        SamplingStrategy: ss,
    })
}

我的xray.json配置如下所示:

{
  "version": 1,
  "rules": [
    {
      "description": "ehb",
      "service_name": "ehb-kafka-push",
      "http_method": "*",
      "url_path": "/private/api/calllambda/*",
      "fixed_target": 0,
      "rate": 0.85
    }
  ],
  "default": {
    "fixed_target": 1,
    "rate": 0.1
  }
}

现在,当我启动我的应用程序时......我的 api 调用正在通过,但我在 AWS xray 和本地的 xray 守护程序中看不到任何东西,我只在日志中看到:

2018-08-26T18:45:04-07:00 [Info] Initializing AWS X-Ray daemon 2.1.3
2018-08-26T18:45:04-07:00 [Debug] Listening on UDP 127.0.0.1:2000
2018-08-26T18:45:04-07:00 [Info] Using buffer memory limit of 163 MB
2018-08-26T18:45:04-07:00 [Info] 2608 segment buffers allocated
2018-08-26T18:45:04-07:00 [Debug] Fetch region us-east-1 from commandline argument
2018-08-26T18:45:04-07:00 [Info] Using region: us-east-1
2018-08-26T18:45:04-07:00 [Debug] ARN of the AWS resource running the daemon: 
2018-08-26T18:45:04-07:00 [Debug] No Metadata set for telemetry records
2018-08-26T18:45:04-07:00 [Debug] Using Endpoint: https://xray.us-east-1.amazonaws.com
2018-08-26T18:45:04-07:00 [Debug] Telemetry initiated
2018-08-26T18:45:04-07:00 [Debug] Using Endpoint: https://xray.us-east-1.amazonaws.com
2018-08-26T18:45:04-07:00 [Debug] Batch size: 50
2018-08-26T18:46:04-07:00 [Debug] Skipped telemetry data as no segments found
2018-08-26T18:47:04-07:00 [Debug] Skipped telemetry data as no segments found
2018-08-26T18:48:04-07:00 [Debug] Skipped telemetry data as no segments found

我在这里想念什么?除了 X 射线部分,一切似乎都正常工作,为什么我没有收到任何数据或错误?

标签: amazon-web-servicesgoaws-xray

解决方案


在 Lambda 场景中,Lambda 负责创建 Segments,AWS X-Ray Golang SDK 只创建 Subsegments 然后发出它们。根据您的代码片段,您没有找到使用 X-Ray Go SDK API 检测您的应用程序以在 Lambda 函数中生成子段的代码。在这里,我附上了一个链接,您可以在其中找到如何检测您的应用程序:lambdadocs


推荐阅读