tracinghandler.go 714 B

1234567891011121314151617181920212223242526
  1. package handler
  2. import (
  3. "net/http"
  4. "git.i2edu.net/i2/go-zero/core/logx"
  5. "git.i2edu.net/i2/go-zero/core/sysx"
  6. "git.i2edu.net/i2/go-zero/core/trace"
  7. )
  8. // TracingHandler returns a middleware that traces the request.
  9. func TracingHandler(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. carrier, err := trace.Extract(trace.HttpFormat, r.Header)
  12. // ErrInvalidCarrier means no trace id was set in http header
  13. if err != nil && err != trace.ErrInvalidCarrier {
  14. logx.Error(err)
  15. }
  16. ctx, span := trace.StartServerSpan(r.Context(), carrier, sysx.Hostname(), r.RequestURI)
  17. defer span.Finish()
  18. r = r.WithContext(ctx)
  19. next.ServeHTTP(w, r)
  20. })
  21. }