util.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package model
  2. import (
  3. "encoding/binary"
  4. "math"
  5. "strconv"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/golang/protobuf/ptypes/duration"
  9. "github.com/golang/protobuf/ptypes/timestamp"
  10. protogen "go-common/library/net/trace/proto"
  11. )
  12. const protoVersion2 int32 = 2
  13. // FromProtoSpan convert protogen.Span to model.Span
  14. func FromProtoSpan(data []byte) (*Span, error) {
  15. protoSpan := new(protogen.Span)
  16. if err := proto.Unmarshal(data, protoSpan); err != nil {
  17. return nil, err
  18. }
  19. var span *Span
  20. var err error
  21. if protoSpan.Version != protoVersion2 {
  22. span, err = fromProtoSpanLeagcy(protoSpan)
  23. } else {
  24. span, err = fromProtoSpanInternal(protoSpan)
  25. }
  26. return span, err
  27. }
  28. func convertLeagcyTag(protoTag *protogen.Tag) Tag {
  29. tag := Tag{Key: protoTag.Key}
  30. switch protoTag.Kind {
  31. case protogen.Tag_STRING:
  32. tag.Kind = TagString
  33. tag.Value = string(protoTag.Value)
  34. case protogen.Tag_INT:
  35. tag.Kind = TagInt
  36. tag.Value, _ = strconv.ParseInt(string(protoTag.Value), 10, 64)
  37. case protogen.Tag_BOOL:
  38. tag.Kind = TagBool
  39. tag.Value, _ = strconv.ParseBool(string(protoTag.Value))
  40. case protogen.Tag_FLOAT:
  41. tag.Kind = TagFloat
  42. tag.Value, _ = strconv.ParseFloat(string(protoTag.Value), 64)
  43. }
  44. return tag
  45. }
  46. func convertLeagcyLog(protoLog *protogen.Log) Log {
  47. log := Log{Timestamp: protoLog.Timestamp}
  48. log.Fields = []Field{{Key: protoLog.Key, Value: protoLog.Value}}
  49. return log
  50. }
  51. func fromProtoSpanLeagcy(protoSpan *protogen.Span) (*Span, error) {
  52. span := &Span{
  53. ServiceName: protoSpan.ServiceName,
  54. OperationName: protoSpan.OperationName,
  55. TraceID: protoSpan.TraceId,
  56. SpanID: protoSpan.SpanId,
  57. ParentID: protoSpan.ParentId,
  58. Env: protoSpan.Env,
  59. }
  60. span.StartTime = time.Unix(protoSpan.StartAt/int64(time.Second), protoSpan.StartAt%int64(time.Second))
  61. span.Duration = time.Duration(protoSpan.FinishAt - protoSpan.StartAt)
  62. span.References = []SpanRef{{
  63. RefType: RefTypeChildOf,
  64. TraceID: protoSpan.TraceId,
  65. SpanID: protoSpan.ParentId,
  66. }}
  67. span.Tags = make(map[string]interface{})
  68. for _, tag := range protoSpan.Tags {
  69. newTag := convertLeagcyTag(tag)
  70. span.Tags[newTag.Key] = newTag.Value
  71. }
  72. span.Logs = make([]Log, 0, len(protoSpan.Logs))
  73. for _, log := range protoSpan.Logs {
  74. span.Logs = append(span.Logs, convertLeagcyLog(log))
  75. }
  76. return span, nil
  77. }
  78. func timeFromTimestamp(t *timestamp.Timestamp) time.Time {
  79. return time.Unix(t.Seconds, int64(t.Nanos))
  80. }
  81. func durationFromDuration(d *duration.Duration) time.Duration {
  82. return time.Duration(d.Seconds*int64(time.Second) + int64(d.Nanos))
  83. }
  84. func convertSpanRef(protoRef *protogen.SpanRef) SpanRef {
  85. ref := SpanRef{
  86. TraceID: protoRef.TraceId,
  87. SpanID: protoRef.SpanId,
  88. }
  89. switch protoRef.RefType {
  90. case protogen.SpanRef_CHILD_OF:
  91. ref.RefType = RefTypeChildOf
  92. case protogen.SpanRef_FOLLOWS_FROM:
  93. ref.RefType = RefTypeFollowsFrom
  94. }
  95. return ref
  96. }
  97. func unSerializeInt64(data []byte) int64 {
  98. return int64(binary.BigEndian.Uint64(data))
  99. }
  100. func unSerializeBool(data []byte) bool {
  101. return data[0] == byte(1)
  102. }
  103. func unSerializeFloat64(data []byte) float64 {
  104. value := binary.BigEndian.Uint64(data)
  105. return math.Float64frombits(value)
  106. }
  107. func convertTag(protoTag *protogen.Tag) Tag {
  108. tag := Tag{Key: protoTag.Key}
  109. switch protoTag.Kind {
  110. case protogen.Tag_STRING:
  111. tag.Kind = TagString
  112. tag.Value = string(protoTag.Value)
  113. case protogen.Tag_INT:
  114. tag.Kind = TagInt
  115. tag.Value = unSerializeInt64(protoTag.Value)
  116. case protogen.Tag_BOOL:
  117. tag.Kind = TagBool
  118. tag.Value = unSerializeBool(protoTag.Value)
  119. case protogen.Tag_FLOAT:
  120. tag.Kind = TagFloat
  121. tag.Value = unSerializeFloat64(protoTag.Value)
  122. }
  123. return tag
  124. }
  125. func convertLog(protoLog *protogen.Log) Log {
  126. log := Log{Timestamp: protoLog.Timestamp}
  127. log.Fields = make([]Field, 0, len(protoLog.Fields))
  128. for _, protoFiled := range protoLog.Fields {
  129. log.Fields = append(log.Fields, Field{Key: protoFiled.Key, Value: protoFiled.Value})
  130. }
  131. return log
  132. }
  133. func fromProtoSpanInternal(protoSpan *protogen.Span) (*Span, error) {
  134. span := &Span{
  135. ServiceName: protoSpan.ServiceName,
  136. OperationName: protoSpan.OperationName,
  137. TraceID: protoSpan.TraceId,
  138. SpanID: protoSpan.SpanId,
  139. Env: protoSpan.Env,
  140. ParentID: protoSpan.ParentId,
  141. StartTime: timeFromTimestamp(protoSpan.StartTime),
  142. Duration: durationFromDuration(protoSpan.Duration),
  143. }
  144. span.References = make([]SpanRef, 0, len(protoSpan.References))
  145. for _, ref := range protoSpan.References {
  146. span.References = append(span.References, convertSpanRef(ref))
  147. }
  148. span.Tags = make(map[string]interface{})
  149. for _, tag := range protoSpan.Tags {
  150. newTag := convertTag(tag)
  151. span.Tags[newTag.Key] = newTag.Value
  152. }
  153. span.Logs = make([]Log, 0, len(protoSpan.Logs))
  154. for _, log := range protoSpan.Logs {
  155. span.Logs = append(span.Logs, convertLog(log))
  156. }
  157. return span, nil
  158. }