trace.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package rpc
  2. import (
  3. "strconv"
  4. "go-common/library/net/trace"
  5. )
  6. // TraceInfo propagate trace propagate gorpc call
  7. type TraceInfo struct {
  8. ID uint64
  9. SpanID uint64
  10. ParentID uint64
  11. Level int32
  12. Sampled bool
  13. Caller string
  14. Title string
  15. Time int64
  16. }
  17. // Set implement trace.Carrier
  18. func (i *TraceInfo) Set(key string, val string) {
  19. switch key {
  20. case trace.KeyTraceID:
  21. i.ID, _ = strconv.ParseUint(val, 10, 64)
  22. case trace.KeyTraceSpanID:
  23. i.SpanID, _ = strconv.ParseUint(val, 10, 64)
  24. case trace.KeyTraceParentID:
  25. i.ParentID, _ = strconv.ParseUint(val, 10, 64)
  26. case trace.KeyTraceSampled:
  27. i.Sampled, _ = strconv.ParseBool(val)
  28. case trace.KeyTraceLevel:
  29. lv, _ := strconv.Atoi(val)
  30. i.Level = int32(lv)
  31. case trace.KeyTraceCaller:
  32. i.Caller = val
  33. }
  34. }
  35. // Get implement trace.Carrier
  36. func (i *TraceInfo) Get(key string) string {
  37. switch key {
  38. case trace.KeyTraceID:
  39. return strconv.FormatUint(i.ID, 10)
  40. case trace.KeyTraceSpanID:
  41. return strconv.FormatUint(i.SpanID, 10)
  42. case trace.KeyTraceParentID:
  43. return strconv.FormatUint(i.ParentID, 10)
  44. case trace.KeyTraceSampled:
  45. return strconv.FormatBool(i.Sampled)
  46. case trace.KeyTraceLevel:
  47. return strconv.Itoa(int(i.Level))
  48. case trace.KeyTraceCaller:
  49. return i.Caller
  50. }
  51. return ""
  52. }