tracinghandler.go 659 B

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