package jaeger import ( "context" "encoding/json" "github.com/gogf/gf/contrib/trace/otlphttp/v2" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/net/gtrace" "go.opentelemetry.io/otel/attribute" "strconv" ) var ShutDown func(ctx context.Context) func init() { jaegerAgent := g.Cfg().MustGet(context.Background(), "jaeger.addr").String() serverName := g.Cfg().MustGet(context.Background(), "server.Name").String() shutdown, err := otlphttp.Init(serverName, jaegerAgent, "/v1/traces") if err != nil { panic(err) } ShutDown = shutdown } func NewTracer(r *ghttp.Request) { _, span := gtrace.NewSpan(r.Context(), r.GetServeHandler().GetMetaTag("summary")) defer span.End() span.SetAttributes(attribute.String("request", getParams(r))) r.Middleware.Next() span.SetAttributes(attribute.String("response", r.Response.BufferString())) } func getParams(r *ghttp.Request) string { params := map[string]interface{}{} if r.Method == "POST" { json.Unmarshal(r.GetBody(), ¶ms) //获取raw传参 } if r.Method == "GET" { r.Request.ParseForm() form := r.Form for k, v := range form { if vl, e := strconv.Atoi(v[0]); e == nil { params[k] = vl } else { params[k] = v[0] } } } rp, _ := json.Marshal(¶ms) return string(rp) }