gpt4 book ai didi

amazon-web-services - 在 AWS 控制台中看不到手动创建的嵌套 AWS XRay 子分段

转载 作者:行者123 更新时间:2023-12-05 05:32:35 30 4
gpt4 key购买 nike

我正在尝试检测用 Go 编写的 Web 服务器,该服务器提供 REST API,在 AWS ECS 上的容器中运行。我现在正在 VSCode 中的 Debug 中运行服务器,致力于概念证明,该证明将显示不同端点的跟踪,并将主要功能作为子段。我像这样在我们的中间件中检测了路由器:

h = xray.Handler(xray.NewFixedSegmentNamer("myappname"), h)

通过传入请求上下文来检测从各种处理函数进行的函数调用,然后:

_, subSeg := xray.BeginSubsegment(ctx, "get-user")

calculateUsefulInfo(ctx)

subSeg.Close(nil)

然后 calculateUsefulInfo() 函数可以调用其他函数,传递上下文 (ctx) 并在内部使用不同的子段名称执行相同的操作(另一个 BeginSubsegment+subSeg.Close)。

我正在运行 AWS XRay 守护程序,并具有适当的权限,我看到跟踪显示在 AWS 控制台中。但是,我只看到一层嵌套。

X-Ray trace visual

我在 AWS 中启用了 100% 采样。我在本地模式下运行 XRay 守护程序,开发级别的日志记录。知道我在这里缺少什么吗?

最佳答案

父段存储在上下文中,因此如果您只将顶级上下文传递给其他函数,它只会为该父段生成段。

要达到所需的嵌套级别,您必须使用 xray.BeginSubsegment 提供的 context。此上下文包含一个可以追溯到父级的新段。

嵌套段示例:

package main

import (
"net"
"net/http"
"time"

"github.com/aws/aws-xray-sdk-go/xray"
"github.com/davecgh/go-spew/spew"
)

type ConsoleEmitter struct {
}

func (c *ConsoleEmitter) Emit(seg *xray.Segment) {
spew.Dump(seg)
}

func (c *ConsoleEmitter) RefreshEmitterWithAddress(raddr *net.UDPAddr) {
return
}

var _ xray.Emitter = (*ConsoleEmitter)(nil)

func init() {
xray.Configure(xray.Config{
DaemonAddr: "127.0.0.1:2000", // default
ServiceVersion: "1.2.3",
// console emitter to view the hierarchy of the traces locally
// without the xray daemon
Emitter: &ConsoleEmitter{},
})
}

func main() {
http.Handle("/", xray.Handler(xray.NewFixedSegmentNamer("myApp"), http.HandlerFunc(top)))
http.ListenAndServe(":7000", nil)
}

func top(w http.ResponseWriter, r *http.Request) {
// use the context provided by xray for nested hierarchy
ctx, subSeg := xray.BeginSubsegment(r.Context(), "top")
_, childSeg := xray.BeginSubsegment(ctx, "top-sleep")
time.Sleep(time.Millisecond * 50)
childSeg.Close(nil)
middle(w, r)
subSeg.Close(nil)
}

func middle(w http.ResponseWriter, r *http.Request) {
ctx, subSeg := xray.BeginSubsegment(r.Context(), "middle")
_, childSeg := xray.BeginSubsegment(ctx, "middle-sleep")
time.Sleep(time.Millisecond * 100)
childSeg.Close(nil)
bottom(w, r)
subSeg.Close(nil)
}

func bottom(w http.ResponseWriter, r *http.Request) {
_, subSeg := xray.BeginSubsegment(r.Context(), "bottom")
w.Write([]byte("Hello!"))
subSeg.Close(nil)
}

关于amazon-web-services - 在 AWS 控制台中看不到手动创建的嵌套 AWS XRay 子分段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74043317/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com