mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
26 lines
594 B
Go
26 lines
594 B
Go
|
package httphandler
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"zero/core/logx"
|
||
|
"zero/core/sysx"
|
||
|
"zero/core/trace"
|
||
|
)
|
||
|
|
||
|
func TracingHandler(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
carrier, err := trace.Extract(trace.HttpFormat, r.Header)
|
||
|
// ErrInvalidCarrier means no trace id was set in http header
|
||
|
if err != nil && err != trace.ErrInvalidCarrier {
|
||
|
logx.Error(err)
|
||
|
}
|
||
|
|
||
|
ctx, span := trace.StartServerSpan(r.Context(), carrier, sysx.Hostname(), r.RequestURI)
|
||
|
defer span.Finish()
|
||
|
r = r.WithContext(ctx)
|
||
|
|
||
|
next.ServeHTTP(w, r)
|
||
|
})
|
||
|
}
|