span.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (c) 2017-2018 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. "sync"
  17. "time"
  18. "github.com/opentracing/opentracing-go"
  19. "github.com/opentracing/opentracing-go/ext"
  20. "github.com/opentracing/opentracing-go/log"
  21. )
  22. // Span implements opentracing.Span
  23. type Span struct {
  24. sync.RWMutex
  25. tracer *Tracer
  26. context SpanContext
  27. // The name of the "operation" this span is an instance of.
  28. // Known as a "span name" in some implementations.
  29. operationName string
  30. // firstInProcess, if true, indicates that this span is the root of the (sub)tree
  31. // of spans in the current process. In other words it's true for the root spans,
  32. // and the ingress spans when the process joins another trace.
  33. firstInProcess bool
  34. // startTime is the timestamp indicating when the span began, with microseconds precision.
  35. startTime time.Time
  36. // duration returns duration of the span with microseconds precision.
  37. // Zero value means duration is unknown.
  38. duration time.Duration
  39. // tags attached to this span
  40. tags []Tag
  41. // The span's "micro-log"
  42. logs []opentracing.LogRecord
  43. // references for this span
  44. references []Reference
  45. observer ContribSpanObserver
  46. }
  47. // Tag is a simple key value wrapper.
  48. // TODO deprecate in the next major release, use opentracing.Tag instead.
  49. type Tag struct {
  50. key string
  51. value interface{}
  52. }
  53. // SetOperationName sets or changes the operation name.
  54. func (s *Span) SetOperationName(operationName string) opentracing.Span {
  55. s.Lock()
  56. defer s.Unlock()
  57. if s.context.IsSampled() {
  58. s.operationName = operationName
  59. }
  60. s.observer.OnSetOperationName(operationName)
  61. return s
  62. }
  63. // SetTag implements SetTag() of opentracing.Span
  64. func (s *Span) SetTag(key string, value interface{}) opentracing.Span {
  65. s.observer.OnSetTag(key, value)
  66. if key == string(ext.SamplingPriority) && !setSamplingPriority(s, value) {
  67. return s
  68. }
  69. s.Lock()
  70. defer s.Unlock()
  71. if s.context.IsSampled() {
  72. s.setTagNoLocking(key, value)
  73. }
  74. return s
  75. }
  76. func (s *Span) setTagNoLocking(key string, value interface{}) {
  77. s.tags = append(s.tags, Tag{key: key, value: value})
  78. }
  79. // LogFields implements opentracing.Span API
  80. func (s *Span) LogFields(fields ...log.Field) {
  81. s.Lock()
  82. defer s.Unlock()
  83. if !s.context.IsSampled() {
  84. return
  85. }
  86. s.logFieldsNoLocking(fields...)
  87. }
  88. // this function should only be called while holding a Write lock
  89. func (s *Span) logFieldsNoLocking(fields ...log.Field) {
  90. lr := opentracing.LogRecord{
  91. Fields: fields,
  92. Timestamp: time.Now(),
  93. }
  94. s.appendLog(lr)
  95. }
  96. // LogKV implements opentracing.Span API
  97. func (s *Span) LogKV(alternatingKeyValues ...interface{}) {
  98. s.RLock()
  99. sampled := s.context.IsSampled()
  100. s.RUnlock()
  101. if !sampled {
  102. return
  103. }
  104. fields, err := log.InterleavedKVToFields(alternatingKeyValues...)
  105. if err != nil {
  106. s.LogFields(log.Error(err), log.String("function", "LogKV"))
  107. return
  108. }
  109. s.LogFields(fields...)
  110. }
  111. // LogEvent implements opentracing.Span API
  112. func (s *Span) LogEvent(event string) {
  113. s.Log(opentracing.LogData{Event: event})
  114. }
  115. // LogEventWithPayload implements opentracing.Span API
  116. func (s *Span) LogEventWithPayload(event string, payload interface{}) {
  117. s.Log(opentracing.LogData{Event: event, Payload: payload})
  118. }
  119. // Log implements opentracing.Span API
  120. func (s *Span) Log(ld opentracing.LogData) {
  121. s.Lock()
  122. defer s.Unlock()
  123. if s.context.IsSampled() {
  124. if ld.Timestamp.IsZero() {
  125. ld.Timestamp = s.tracer.timeNow()
  126. }
  127. s.appendLog(ld.ToLogRecord())
  128. }
  129. }
  130. // this function should only be called while holding a Write lock
  131. func (s *Span) appendLog(lr opentracing.LogRecord) {
  132. // TODO add logic to limit number of logs per span (issue #46)
  133. s.logs = append(s.logs, lr)
  134. }
  135. // SetBaggageItem implements SetBaggageItem() of opentracing.SpanContext
  136. func (s *Span) SetBaggageItem(key, value string) opentracing.Span {
  137. s.Lock()
  138. defer s.Unlock()
  139. s.tracer.setBaggage(s, key, value)
  140. return s
  141. }
  142. // BaggageItem implements BaggageItem() of opentracing.SpanContext
  143. func (s *Span) BaggageItem(key string) string {
  144. s.RLock()
  145. defer s.RUnlock()
  146. return s.context.baggage[key]
  147. }
  148. // Finish implements opentracing.Span API
  149. func (s *Span) Finish() {
  150. s.FinishWithOptions(opentracing.FinishOptions{})
  151. }
  152. // FinishWithOptions implements opentracing.Span API
  153. func (s *Span) FinishWithOptions(options opentracing.FinishOptions) {
  154. if options.FinishTime.IsZero() {
  155. options.FinishTime = s.tracer.timeNow()
  156. }
  157. s.observer.OnFinish(options)
  158. s.Lock()
  159. if s.context.IsSampled() {
  160. s.duration = options.FinishTime.Sub(s.startTime)
  161. // Note: bulk logs are not subject to maxLogsPerSpan limit
  162. if options.LogRecords != nil {
  163. s.logs = append(s.logs, options.LogRecords...)
  164. }
  165. for _, ld := range options.BulkLogData {
  166. s.logs = append(s.logs, ld.ToLogRecord())
  167. }
  168. }
  169. s.Unlock()
  170. // call reportSpan even for non-sampled traces, to return span to the pool
  171. s.tracer.reportSpan(s)
  172. }
  173. // Context implements opentracing.Span API
  174. func (s *Span) Context() opentracing.SpanContext {
  175. s.Lock()
  176. defer s.Unlock()
  177. return s.context
  178. }
  179. // Tracer implements opentracing.Span API
  180. func (s *Span) Tracer() opentracing.Tracer {
  181. return s.tracer
  182. }
  183. func (s *Span) String() string {
  184. s.RLock()
  185. defer s.RUnlock()
  186. return s.context.String()
  187. }
  188. // OperationName allows retrieving current operation name.
  189. func (s *Span) OperationName() string {
  190. s.RLock()
  191. defer s.RUnlock()
  192. return s.operationName
  193. }
  194. func (s *Span) serviceName() string {
  195. return s.tracer.serviceName
  196. }
  197. // setSamplingPriority returns true if the flag was updated successfully, false otherwise.
  198. func setSamplingPriority(s *Span, value interface{}) bool {
  199. s.Lock()
  200. defer s.Unlock()
  201. val, ok := value.(uint16)
  202. if !ok {
  203. return false
  204. }
  205. if val == 0 {
  206. s.context.flags = s.context.flags & (^flagSampled)
  207. return true
  208. }
  209. if s.tracer.isDebugAllowed(s.operationName) {
  210. s.context.flags = s.context.flags | flagDebug | flagSampled
  211. return true
  212. }
  213. return false
  214. }