carrier.go 713 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package trace
  2. import (
  3. "errors"
  4. "net/http"
  5. "strings"
  6. )
  7. var ErrInvalidCarrier = errors.New("invalid carrier")
  8. type (
  9. Carrier interface {
  10. Get(key string) string
  11. Set(key, value string)
  12. }
  13. httpCarrier http.Header
  14. // grpc metadata takes keys as case insensitive
  15. grpcCarrier map[string][]string
  16. )
  17. func (h httpCarrier) Get(key string) string {
  18. return http.Header(h).Get(key)
  19. }
  20. func (h httpCarrier) Set(key, val string) {
  21. http.Header(h).Set(key, val)
  22. }
  23. func (g grpcCarrier) Get(key string) string {
  24. if vals, ok := g[strings.ToLower(key)]; ok && len(vals) > 0 {
  25. return vals[0]
  26. }
  27. return ""
  28. }
  29. func (g grpcCarrier) Set(key, val string) {
  30. key = strings.ToLower(key)
  31. g[key] = append(g[key], val)
  32. }