jaeger_thrift_span.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package jaeger
  21. import (
  22. "time"
  23. "github.com/opentracing/opentracing-go"
  24. j "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  25. "github.com/uber/jaeger-client-go/utils"
  26. )
  27. // BuildJaegerThrift builds jaeger span based on internal span.
  28. func BuildJaegerThrift(span *Span) *j.Span {
  29. startTime := utils.TimeToMicrosecondsSinceEpochInt64(span.startTime)
  30. duration := span.duration.Nanoseconds() / int64(time.Microsecond)
  31. jaegerSpan := &j.Span{
  32. TraceIdLow: int64(span.context.traceID.Low),
  33. TraceIdHigh: int64(span.context.traceID.High),
  34. SpanId: int64(span.context.spanID),
  35. ParentSpanId: int64(span.context.parentID),
  36. OperationName: span.operationName,
  37. Flags: int32(span.context.flags),
  38. StartTime: startTime,
  39. Duration: duration,
  40. Tags: buildTags(span.tags),
  41. Logs: buildLogs(span.logs),
  42. References: buildReferences(span.references),
  43. }
  44. return jaegerSpan
  45. }
  46. // BuildJaegerProcessThrift creates a thrift Process type.
  47. func BuildJaegerProcessThrift(span *Span) *j.Process {
  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),
  54. }
  55. return process
  56. }
  57. func buildTags(tags []Tag) []*j.Tag {
  58. jTags := make([]*j.Tag, 0, len(tags))
  59. for _, tag := range tags {
  60. jTag := buildTag(&tag)
  61. jTags = append(jTags, jTag)
  62. }
  63. return jTags
  64. }
  65. func buildLogs(logs []opentracing.LogRecord) []*j.Log {
  66. jLogs := make([]*j.Log, 0, len(logs))
  67. for _, log := range logs {
  68. jLog := &j.Log{
  69. Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(log.Timestamp),
  70. Fields: ConvertLogsToJaegerTags(log.Fields),
  71. }
  72. jLogs = append(jLogs, jLog)
  73. }
  74. return jLogs
  75. }
  76. func buildTag(tag *Tag) *j.Tag {
  77. jTag := &j.Tag{Key: tag.key}
  78. switch value := tag.value.(type) {
  79. case string:
  80. vStr := truncateString(value)
  81. jTag.VStr = &vStr
  82. jTag.VType = j.TagType_STRING
  83. case []byte:
  84. if len(value) > maxAnnotationLength {
  85. value = value[:maxAnnotationLength]
  86. }
  87. jTag.VBinary = value
  88. jTag.VType = j.TagType_BINARY
  89. case int:
  90. vLong := int64(value)
  91. jTag.VLong = &vLong
  92. jTag.VType = j.TagType_LONG
  93. case uint:
  94. vLong := int64(value)
  95. jTag.VLong = &vLong
  96. jTag.VType = j.TagType_LONG
  97. case int8:
  98. vLong := int64(value)
  99. jTag.VLong = &vLong
  100. jTag.VType = j.TagType_LONG
  101. case uint8:
  102. vLong := int64(value)
  103. jTag.VLong = &vLong
  104. jTag.VType = j.TagType_LONG
  105. case int16:
  106. vLong := int64(value)
  107. jTag.VLong = &vLong
  108. jTag.VType = j.TagType_LONG
  109. case uint16:
  110. vLong := int64(value)
  111. jTag.VLong = &vLong
  112. jTag.VType = j.TagType_LONG
  113. case int32:
  114. vLong := int64(value)
  115. jTag.VLong = &vLong
  116. jTag.VType = j.TagType_LONG
  117. case uint32:
  118. vLong := int64(value)
  119. jTag.VLong = &vLong
  120. jTag.VType = j.TagType_LONG
  121. case int64:
  122. vLong := int64(value)
  123. jTag.VLong = &vLong
  124. jTag.VType = j.TagType_LONG
  125. case uint64:
  126. vLong := int64(value)
  127. jTag.VLong = &vLong
  128. jTag.VType = j.TagType_LONG
  129. case float32:
  130. vDouble := float64(value)
  131. jTag.VDouble = &vDouble
  132. jTag.VType = j.TagType_DOUBLE
  133. case float64:
  134. vDouble := float64(value)
  135. jTag.VDouble = &vDouble
  136. jTag.VType = j.TagType_DOUBLE
  137. case bool:
  138. vBool := value
  139. jTag.VBool = &vBool
  140. jTag.VType = j.TagType_BOOL
  141. default:
  142. vStr := truncateString(stringify(value))
  143. jTag.VStr = &vStr
  144. jTag.VType = j.TagType_STRING
  145. }
  146. return jTag
  147. }
  148. func buildReferences(references []Reference) []*j.SpanRef {
  149. retMe := make([]*j.SpanRef, 0, len(references))
  150. for _, ref := range references {
  151. if ref.Type == opentracing.ChildOfRef {
  152. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_CHILD_OF))
  153. } else if ref.Type == opentracing.FollowsFromRef {
  154. retMe = append(retMe, spanRef(ref.Context, j.SpanRefType_FOLLOWS_FROM))
  155. }
  156. }
  157. return retMe
  158. }
  159. func spanRef(ctx SpanContext, refType j.SpanRefType) *j.SpanRef {
  160. return &j.SpanRef{
  161. RefType: refType,
  162. TraceIdLow: int64(ctx.traceID.Low),
  163. TraceIdHigh: int64(ctx.traceID.High),
  164. SpanId: int64(ctx.spanID),
  165. }
  166. }