jaeger_thrift_span.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jaeger
  15. import (
  16. "time"
  17. "github.com/opentracing/opentracing-go"
  18. j "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  19. "github.com/uber/jaeger-client-go/utils"
  20. )
  21. // BuildJaegerThrift builds jaeger span based on internal span.
  22. func BuildJaegerThrift(span *Span) *j.Span {
  23. startTime := utils.TimeToMicrosecondsSinceEpochInt64(span.startTime)
  24. duration := span.duration.Nanoseconds() / int64(time.Microsecond)
  25. jaegerSpan := &j.Span{
  26. TraceIdLow: int64(span.context.traceID.Low),
  27. TraceIdHigh: int64(span.context.traceID.High),
  28. SpanId: int64(span.context.spanID),
  29. ParentSpanId: int64(span.context.parentID),
  30. OperationName: span.operationName,
  31. Flags: int32(span.context.flags),
  32. StartTime: startTime,
  33. Duration: duration,
  34. Tags: buildTags(span.tags),
  35. Logs: buildLogs(span.logs),
  36. References: buildReferences(span.references),
  37. }
  38. return jaegerSpan
  39. }
  40. // BuildJaegerProcessThrift creates a thrift Process type.
  41. func BuildJaegerProcessThrift(span *Span) *j.Process {
  42. return buildJaegerProcessThrift(span.tracer)
  43. }
  44. func buildJaegerProcessThrift(tracer *Tracer) *j.Process {
  45. process := &j.Process{
  46. ServiceName: tracer.serviceName,
  47. Tags: buildTags(tracer.tags),
  48. }
  49. return process
  50. }
  51. func buildTags(tags []Tag) []*j.Tag {
  52. jTags := make([]*j.Tag, 0, len(tags))
  53. for _, tag := range tags {
  54. jTag := buildTag(&tag)
  55. jTags = append(jTags, jTag)
  56. }
  57. return jTags
  58. }
  59. func buildLogs(logs []opentracing.LogRecord) []*j.Log {
  60. jLogs := make([]*j.Log, 0, len(logs))
  61. for _, log := range logs {
  62. jLog := &j.Log{
  63. Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(log.Timestamp),
  64. Fields: ConvertLogsToJaegerTags(log.Fields),
  65. }
  66. jLogs = append(jLogs, jLog)
  67. }
  68. return jLogs
  69. }
  70. func buildTag(tag *Tag) *j.Tag {
  71. jTag := &j.Tag{Key: tag.key}
  72. switch value := tag.value.(type) {
  73. case string:
  74. vStr := truncateString(value)
  75. jTag.VStr = &vStr
  76. jTag.VType = j.TagType_STRING
  77. case []byte:
  78. if len(value) > maxAnnotationLength {
  79. value = value[:maxAnnotationLength]
  80. }
  81. jTag.VBinary = value
  82. jTag.VType = j.TagType_BINARY
  83. case int:
  84. vLong := int64(value)
  85. jTag.VLong = &vLong
  86. jTag.VType = j.TagType_LONG
  87. case uint:
  88. vLong := int64(value)
  89. jTag.VLong = &vLong
  90. jTag.VType = j.TagType_LONG
  91. case int8:
  92. vLong := int64(value)
  93. jTag.VLong = &vLong
  94. jTag.VType = j.TagType_LONG
  95. case uint8:
  96. vLong := int64(value)
  97. jTag.VLong = &vLong
  98. jTag.VType = j.TagType_LONG
  99. case int16:
  100. vLong := int64(value)
  101. jTag.VLong = &vLong
  102. jTag.VType = j.TagType_LONG
  103. case uint16:
  104. vLong := int64(value)
  105. jTag.VLong = &vLong
  106. jTag.VType = j.TagType_LONG
  107. case int32:
  108. vLong := int64(value)
  109. jTag.VLong = &vLong
  110. jTag.VType = j.TagType_LONG
  111. case uint32:
  112. vLong := int64(value)
  113. jTag.VLong = &vLong
  114. jTag.VType = j.TagType_LONG
  115. case int64:
  116. vLong := int64(value)
  117. jTag.VLong = &vLong
  118. jTag.VType = j.TagType_LONG
  119. case uint64:
  120. vLong := int64(value)
  121. jTag.VLong = &vLong
  122. jTag.VType = j.TagType_LONG
  123. case float32:
  124. vDouble := float64(value)
  125. jTag.VDouble = &vDouble
  126. jTag.VType = j.TagType_DOUBLE
  127. case float64:
  128. vDouble := float64(value)
  129. jTag.VDouble = &vDouble
  130. jTag.VType = j.TagType_DOUBLE
  131. case bool:
  132. vBool := value
  133. jTag.VBool = &vBool
  134. jTag.VType = j.TagType_BOOL
  135. default:
  136. vStr := truncateString(stringify(value))
  137. jTag.VStr = &vStr
  138. jTag.VType = j.TagType_STRING
  139. }
  140. return jTag
  141. }
  142. func buildReferences(references []Reference) []*j.SpanRef {
  143. retMe := make([]*j.SpanRef, 0, len(references))
  144. for _, ref := range references {
  145. if ref.Type == opentracing.ChildOfRef {
  146. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_CHILD_OF))
  147. } else if ref.Type == opentracing.FollowsFromRef {
  148. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_FOLLOWS_FROM))
  149. }
  150. }
  151. return retMe
  152. }
  153. func spanRef(ctx SpanContext, refType j.SpanRefType) *j.SpanRef {
  154. return &j.SpanRef{
  155. RefType: refType,
  156. TraceIdLow: int64(ctx.traceID.Low),
  157. TraceIdHigh: int64(ctx.traceID.High),
  158. SpanId: int64(ctx.spanID),
  159. }
  160. }