carrier.go 834 B

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