1 回答
TA贡献2039条经验 获得超7个赞
父段存储在上下文中,因此如果您只将顶级上下文传递给其他函数,它只会为该父段生成段。
要达到您想要的嵌套级别,您必须context使用xray.BeginSubsegment. 此上下文包含一个可以追溯到父级的新段。
嵌套段示例:
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)
}
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报