span.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package model
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. // RefType Kind
  7. const (
  8. RefTypeChildOf int8 = iota
  9. RefTypeFollowsFrom
  10. )
  11. // TagKind
  12. const (
  13. TagString int8 = iota
  14. TagInt
  15. TagBool
  16. TagFloat
  17. )
  18. // SpanRef describes causal relationship of the current span to another span (e.g. 'child-of')
  19. type SpanRef struct {
  20. RefType int8
  21. TraceID uint64
  22. SpanID uint64
  23. }
  24. // Tag span tag
  25. type Tag struct {
  26. Kind int8
  27. Key string
  28. Value interface{}
  29. }
  30. // Field log field
  31. type Field struct {
  32. Key string
  33. Value []byte
  34. }
  35. // Log span log
  36. type Log struct {
  37. Timestamp int64
  38. Fields []Field
  39. }
  40. // Span represents a named unit of work performed by a service.
  41. type Span struct {
  42. ServiceName string
  43. OperationName string
  44. TraceID uint64
  45. SpanID uint64
  46. ParentID uint64
  47. Env string
  48. StartTime time.Time
  49. Duration time.Duration
  50. References []SpanRef
  51. Tags map[string]interface{}
  52. Logs []Log
  53. }
  54. // TraceIDStr return hex format trace_id
  55. func (s *Span) TraceIDStr() string {
  56. return strconv.FormatUint(s.TraceID, 16)
  57. }
  58. // SpanIDStr return hex format span_id
  59. func (s *Span) SpanIDStr() string {
  60. return strconv.FormatUint(s.SpanID, 16)
  61. }
  62. // ParentIDStr return hex format parent_id
  63. func (s *Span) ParentIDStr() string {
  64. return strconv.FormatUint(s.ParentID, 16)
  65. }
  66. // IsServer span kind is server
  67. func (s *Span) IsServer() bool {
  68. kind, ok := s.Tags["span.kind"].(string)
  69. if !ok {
  70. return false
  71. }
  72. return kind == "server"
  73. }
  74. // IsError is error happend
  75. func (s *Span) IsError() bool {
  76. isErr, _ := s.Tags["error"].(bool)
  77. return isErr
  78. }
  79. // StringTag get string value from tag
  80. func (s *Span) StringTag(key string) string {
  81. val, _ := s.Tags[key].(string)
  82. return val
  83. }