You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
813 B
Go
29 lines
813 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
"github.com/tal-tech/go-zero/core/sysx"
|
|
"github.com/tal-tech/go-zero/core/trace"
|
|
)
|
|
|
|
// TracingHandler returns a middleware that traces the request.
|
|
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)
|
|
|
|
// Conveniently track error messages
|
|
w.Header().Set(trace.TraceIdKey, span.TraceId())
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|