span.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package model
  2. import (
  3. "strconv"
  4. "time"
  5. "github.com/golang/protobuf/proto"
  6. protogen "go-common/library/net/trace/proto"
  7. )
  8. // ProtoSpan alias to tgo-common/library/net/trace/proto.Span
  9. type ProtoSpan protogen.Span
  10. // RefType Kind
  11. const (
  12. RefTypeChildOf int8 = iota
  13. RefTypeFollowsFrom
  14. )
  15. // TagKind
  16. const (
  17. TagString int8 = iota
  18. TagInt
  19. TagBool
  20. TagFloat
  21. )
  22. // SpanRef describes causal relationship of the current span to another span (e.g. 'child-of')
  23. type SpanRef struct {
  24. RefType int8
  25. TraceID uint64
  26. SpanID uint64
  27. }
  28. // Tag span tag
  29. type Tag struct {
  30. Kind int8
  31. Key string
  32. Value interface{}
  33. }
  34. // Field log field
  35. type Field struct {
  36. Key string
  37. Value []byte
  38. }
  39. // Log span log
  40. type Log struct {
  41. Timestamp int64
  42. Fields []Field
  43. }
  44. // Span represents a named unit of work performed by a service.
  45. type Span struct {
  46. ServiceName string
  47. OperationName string
  48. TraceID uint64
  49. SpanID uint64
  50. ParentID uint64
  51. Env string
  52. StartTime time.Time
  53. Duration time.Duration
  54. References []SpanRef
  55. Tags map[string]interface{}
  56. Logs []Log
  57. ProtoSpan *ProtoSpan
  58. }
  59. // SetTag attach tag
  60. func (s *Span) SetTag(key string, value interface{}) error {
  61. ptag, err := toProtoTag(key, value)
  62. if err != nil {
  63. return err
  64. }
  65. s.Tags[key] = value
  66. s.ProtoSpan.Tags = append(s.ProtoSpan.Tags, ptag)
  67. return nil
  68. }
  69. // SetOperationName .
  70. func (s *Span) SetOperationName(operationName string) {
  71. s.OperationName = operationName
  72. s.ProtoSpan.OperationName = operationName
  73. }
  74. // TraceIDStr return hex format trace_id
  75. func (s *Span) TraceIDStr() string {
  76. return strconv.FormatUint(s.TraceID, 16)
  77. }
  78. // SpanIDStr return hex format span_id
  79. func (s *Span) SpanIDStr() string {
  80. return strconv.FormatUint(s.SpanID, 16)
  81. }
  82. // ParentIDStr return hex format parent_id
  83. func (s *Span) ParentIDStr() string {
  84. return strconv.FormatUint(s.ParentID, 16)
  85. }
  86. // IsServer span kind is server
  87. func (s *Span) IsServer() bool {
  88. kind, ok := s.Tags["span.kind"].(string)
  89. if !ok {
  90. return false
  91. }
  92. return kind == "server"
  93. }
  94. // IsError is error happend
  95. func (s *Span) IsError() bool {
  96. isErr, _ := s.Tags["error"].(bool)
  97. return isErr
  98. }
  99. // StringTag get string type tag
  100. func (s *Span) StringTag(key string) string {
  101. val, _ := s.Tags[key].(string)
  102. return val
  103. }
  104. // BoolTag get string type tag
  105. func (s *Span) BoolTag(key string) bool {
  106. val, _ := s.Tags[key].(bool)
  107. return val
  108. }
  109. // GetTagString .
  110. func (s *Span) GetTagString(key string) string {
  111. val, _ := s.Tags[key].(string)
  112. return val
  113. }
  114. // Marshal return
  115. func (s *Span) Marshal() ([]byte, error) {
  116. if s.ProtoSpan == nil {
  117. return nil, nil
  118. }
  119. return proto.Marshal((*protogen.Span)(s.ProtoSpan))
  120. }