jaeger_thrift_span.go 5.0 KB

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