1 回答
TA貢獻2039條經驗 獲得超8個贊
父段存儲在上下文中,因此如果您只將頂級上下文傳遞給其他函數,它只會為該父段生成段。
要達到您想要的嵌套級別,您必須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 關注
- 137 瀏覽
添加回答
舉報
