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

我正在尝试检测用 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 控制台中。但是,我只看到一层嵌套。

http://img1.mukewang.com/63fdf1150001fefb10190139.jpg

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



扬帆大鱼
浏览 108回答 1
1回答

largeQ

父段存储在上下文中,因此如果您只将顶级上下文传递给其他函数,它只会为该父段生成段。要达到您想要的嵌套级别,您必须context使用xray.BeginSubsegment. 此上下文包含一个可以追溯到父级的新段。嵌套段示例:package mainimport (    "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)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go