jaeger_thrift_span.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. span.Lock()
  24. defer span.Unlock()
  25. startTime := utils.TimeToMicrosecondsSinceEpochInt64(span.startTime)
  26. duration := span.duration.Nanoseconds() / int64(time.Microsecond)
  27. jaegerSpan := &j.Span{
  28. TraceIdLow: int64(span.context.traceID.Low),
  29. TraceIdHigh: int64(span.context.traceID.High),
  30. SpanId: int64(span.context.spanID),
  31. ParentSpanId: int64(span.context.parentID),
  32. OperationName: span.operationName,
  33. Flags: int32(span.context.flags),
  34. StartTime: startTime,
  35. Duration: duration,
  36. Tags: buildTags(span.tags, span.tracer.options.maxTagValueLength),
  37. Logs: buildLogs(span.logs),
  38. References: buildReferences(span.references),
  39. }
  40. return jaegerSpan
  41. }
  42. // BuildJaegerProcessThrift creates a thrift Process type.
  43. func BuildJaegerProcessThrift(span *Span) *j.Process {
  44. span.Lock()
  45. defer span.Unlock()
  46. return buildJaegerProcessThrift(span.tracer)
  47. }
  48. func buildJaegerProcessThrift(tracer *Tracer) *j.Process {
  49. process := &j.Process{
  50. ServiceName: tracer.serviceName,
  51. Tags: buildTags(tracer.tags, tracer.options.maxTagValueLength),
  52. }
  53. if tracer.process.UUID != "" {
  54. process.Tags = append(process.Tags, &j.Tag{Key: TracerUUIDTagKey, VStr: &tracer.process.UUID, VType: j.TagType_STRING})
  55. }
  56. return process
  57. }
  58. func buildTags(tags []Tag, maxTagValueLength int) []*j.Tag {
  59. jTags := make([]*j.Tag, 0, len(tags))
  60. for _, tag := range tags {
  61. jTag := buildTag(&tag, maxTagValueLength)
  62. jTags = append(jTags, jTag)
  63. }
  64. return jTags
  65. }
  66. func buildLogs(logs []opentracing.LogRecord) []*j.Log {
  67. jLogs := make([]*j.Log, 0, len(logs))
  68. for _, log := range logs {
  69. jLog := &j.Log{
  70. Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(log.Timestamp),
  71. Fields: ConvertLogsToJaegerTags(log.Fields),
  72. }
  73. jLogs = append(jLogs, jLog)
  74. }
  75. return jLogs
  76. }
  77. func buildTag(tag *Tag, maxTagValueLength int) *j.Tag {
  78. jTag := &j.Tag{Key: tag.key}
  79. switch value := tag.value.(type) {
  80. case string:
  81. vStr := truncateString(value, maxTagValueLength)
  82. jTag.VStr = &vStr
  83. jTag.VType = j.TagType_STRING
  84. case []byte:
  85. if len(value) > maxTagValueLength {
  86. value = value[:maxTagValueLength]
  87. }
  88. jTag.VBinary = value
  89. jTag.VType = j.TagType_BINARY
  90. case int:
  91. vLong := int64(value)
  92. jTag.VLong = &vLong
  93. jTag.VType = j.TagType_LONG
  94. case uint:
  95. vLong := int64(value)
  96. jTag.VLong = &vLong
  97. jTag.VType = j.TagType_LONG
  98. case int8:
  99. vLong := int64(value)
  100. jTag.VLong = &vLong
  101. jTag.VType = j.TagType_LONG
  102. case uint8:
  103. vLong := int64(value)
  104. jTag.VLong = &vLong
  105. jTag.VType = j.TagType_LONG
  106. case int16:
  107. vLong := int64(value)
  108. jTag.VLong = &vLong
  109. jTag.VType = j.TagType_LONG
  110. case uint16:
  111. vLong := int64(value)
  112. jTag.VLong = &vLong
  113. jTag.VType = j.TagType_LONG
  114. case int32:
  115. vLong := int64(value)
  116. jTag.VLong = &vLong
  117. jTag.VType = j.TagType_LONG
  118. case uint32:
  119. vLong := int64(value)
  120. jTag.VLong = &vLong
  121. jTag.VType = j.TagType_LONG
  122. case int64:
  123. vLong := int64(value)
  124. jTag.VLong = &vLong
  125. jTag.VType = j.TagType_LONG
  126. case uint64:
  127. vLong := int64(value)
  128. jTag.VLong = &vLong
  129. jTag.VType = j.TagType_LONG
  130. case float32:
  131. vDouble := float64(value)
  132. jTag.VDouble = &vDouble
  133. jTag.VType = j.TagType_DOUBLE
  134. case float64:
  135. vDouble := float64(value)
  136. jTag.VDouble = &vDouble
  137. jTag.VType = j.TagType_DOUBLE
  138. case bool:
  139. vBool := value
  140. jTag.VBool = &vBool
  141. jTag.VType = j.TagType_BOOL
  142. default:
  143. vStr := truncateString(stringify(value), maxTagValueLength)
  144. jTag.VStr = &vStr
  145. jTag.VType = j.TagType_STRING
  146. }
  147. return jTag
  148. }
  149. func buildReferences(references []Reference) []*j.SpanRef {
  150. retMe := make([]*j.SpanRef, 0, len(references))
  151. for _, ref := range references {
  152. if ref.Type == opentracing.ChildOfRef {
  153. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_CHILD_OF))
  154. } else if ref.Type == opentracing.FollowsFromRef {
  155. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_FOLLOWS_FROM))
  156. }
  157. }
  158. return retMe
  159. }
  160. func spanRef(ctx SpanContext, refType j.SpanRefType) *j.SpanRef {
  161. return &j.SpanRef{
  162. RefType: refType,
  163. TraceIdLow: int64(ctx.traceID.Low),
  164. TraceIdHigh: int64(ctx.traceID.High),
  165. SpanId: int64(ctx.spanID),
  166. }
  167. }