You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

599 lines
17 KiB

  1. // Copyright 2017, OpenCensus Authors
  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 trace
  15. import (
  16. "context"
  17. crand "crypto/rand"
  18. "encoding/binary"
  19. "fmt"
  20. "math/rand"
  21. "sync"
  22. "sync/atomic"
  23. "time"
  24. "go.opencensus.io/internal"
  25. "go.opencensus.io/trace/tracestate"
  26. )
  27. // Span represents a span of a trace. It has an associated SpanContext, and
  28. // stores data accumulated while the span is active.
  29. //
  30. // Ideally users should interact with Spans by calling the functions in this
  31. // package that take a Context parameter.
  32. type Span struct {
  33. // data contains information recorded about the span.
  34. //
  35. // It will be non-nil if we are exporting the span or recording events for it.
  36. // Otherwise, data is nil, and the Span is simply a carrier for the
  37. // SpanContext, so that the trace ID is propagated.
  38. data *SpanData
  39. mu sync.Mutex // protects the contents of *data (but not the pointer value.)
  40. spanContext SpanContext
  41. // lruAttributes are capped at configured limit. When the capacity is reached an oldest entry
  42. // is removed to create room for a new entry.
  43. lruAttributes *lruMap
  44. // annotations are stored in FIFO queue capped by configured limit.
  45. annotations *evictedQueue
  46. // messageEvents are stored in FIFO queue capped by configured limit.
  47. messageEvents *evictedQueue
  48. // links are stored in FIFO queue capped by configured limit.
  49. links *evictedQueue
  50. // spanStore is the spanStore this span belongs to, if any, otherwise it is nil.
  51. *spanStore
  52. endOnce sync.Once
  53. executionTracerTaskEnd func() // ends the execution tracer span
  54. }
  55. // IsRecordingEvents returns true if events are being recorded for this span.
  56. // Use this check to avoid computing expensive annotations when they will never
  57. // be used.
  58. func (s *Span) IsRecordingEvents() bool {
  59. if s == nil {
  60. return false
  61. }
  62. return s.data != nil
  63. }
  64. // TraceOptions contains options associated with a trace span.
  65. type TraceOptions uint32
  66. // IsSampled returns true if the span will be exported.
  67. func (sc SpanContext) IsSampled() bool {
  68. return sc.TraceOptions.IsSampled()
  69. }
  70. // setIsSampled sets the TraceOptions bit that determines whether the span will be exported.
  71. func (sc *SpanContext) setIsSampled(sampled bool) {
  72. if sampled {
  73. sc.TraceOptions |= 1
  74. } else {
  75. sc.TraceOptions &= ^TraceOptions(1)
  76. }
  77. }
  78. // IsSampled returns true if the span will be exported.
  79. func (t TraceOptions) IsSampled() bool {
  80. return t&1 == 1
  81. }
  82. // SpanContext contains the state that must propagate across process boundaries.
  83. //
  84. // SpanContext is not an implementation of context.Context.
  85. // TODO: add reference to external Census docs for SpanContext.
  86. type SpanContext struct {
  87. TraceID TraceID
  88. SpanID SpanID
  89. TraceOptions TraceOptions
  90. Tracestate *tracestate.Tracestate
  91. }
  92. type contextKey struct{}
  93. // FromContext returns the Span stored in a context, or nil if there isn't one.
  94. func FromContext(ctx context.Context) *Span {
  95. s, _ := ctx.Value(contextKey{}).(*Span)
  96. return s
  97. }
  98. // NewContext returns a new context with the given Span attached.
  99. func NewContext(parent context.Context, s *Span) context.Context {
  100. return context.WithValue(parent, contextKey{}, s)
  101. }
  102. // All available span kinds. Span kind must be either one of these values.
  103. const (
  104. SpanKindUnspecified = iota
  105. SpanKindServer
  106. SpanKindClient
  107. )
  108. // StartOptions contains options concerning how a span is started.
  109. type StartOptions struct {
  110. // Sampler to consult for this Span. If provided, it is always consulted.
  111. //
  112. // If not provided, then the behavior differs based on whether
  113. // the parent of this Span is remote, local, or there is no parent.
  114. // In the case of a remote parent or no parent, the
  115. // default sampler (see Config) will be consulted. Otherwise,
  116. // when there is a non-remote parent, no new sampling decision will be made:
  117. // we will preserve the sampling of the parent.
  118. Sampler Sampler
  119. // SpanKind represents the kind of a span. If none is set,
  120. // SpanKindUnspecified is used.
  121. SpanKind int
  122. }
  123. // StartOption apply changes to StartOptions.
  124. type StartOption func(*StartOptions)
  125. // WithSpanKind makes new spans to be created with the given kind.
  126. func WithSpanKind(spanKind int) StartOption {
  127. return func(o *StartOptions) {
  128. o.SpanKind = spanKind
  129. }
  130. }
  131. // WithSampler makes new spans to be be created with a custom sampler.
  132. // Otherwise, the global sampler is used.
  133. func WithSampler(sampler Sampler) StartOption {
  134. return func(o *StartOptions) {
  135. o.Sampler = sampler
  136. }
  137. }
  138. // StartSpan starts a new child span of the current span in the context. If
  139. // there is no span in the context, creates a new trace and span.
  140. //
  141. // Returned context contains the newly created span. You can use it to
  142. // propagate the returned span in process.
  143. func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
  144. var opts StartOptions
  145. var parent SpanContext
  146. if p := FromContext(ctx); p != nil {
  147. p.addChild()
  148. parent = p.spanContext
  149. }
  150. for _, op := range o {
  151. op(&opts)
  152. }
  153. span := startSpanInternal(name, parent != SpanContext{}, parent, false, opts)
  154. ctx, end := startExecutionTracerTask(ctx, name)
  155. span.executionTracerTaskEnd = end
  156. return NewContext(ctx, span), span
  157. }
  158. // StartSpanWithRemoteParent starts a new child span of the span from the given parent.
  159. //
  160. // If the incoming context contains a parent, it ignores. StartSpanWithRemoteParent is
  161. // preferred for cases where the parent is propagated via an incoming request.
  162. //
  163. // Returned context contains the newly created span. You can use it to
  164. // propagate the returned span in process.
  165. func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
  166. var opts StartOptions
  167. for _, op := range o {
  168. op(&opts)
  169. }
  170. span := startSpanInternal(name, parent != SpanContext{}, parent, true, opts)
  171. ctx, end := startExecutionTracerTask(ctx, name)
  172. span.executionTracerTaskEnd = end
  173. return NewContext(ctx, span), span
  174. }
  175. func startSpanInternal(name string, hasParent bool, parent SpanContext, remoteParent bool, o StartOptions) *Span {
  176. span := &Span{}
  177. span.spanContext = parent
  178. cfg := config.Load().(*Config)
  179. if !hasParent {
  180. span.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
  181. }
  182. span.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
  183. sampler := cfg.DefaultSampler
  184. if !hasParent || remoteParent || o.Sampler != nil {
  185. // If this span is the child of a local span and no Sampler is set in the
  186. // options, keep the parent's TraceOptions.
  187. //
  188. // Otherwise, consult the Sampler in the options if it is non-nil, otherwise
  189. // the default sampler.
  190. if o.Sampler != nil {
  191. sampler = o.Sampler
  192. }
  193. span.spanContext.setIsSampled(sampler(SamplingParameters{
  194. ParentContext: parent,
  195. TraceID: span.spanContext.TraceID,
  196. SpanID: span.spanContext.SpanID,
  197. Name: name,
  198. HasRemoteParent: remoteParent}).Sample)
  199. }
  200. if !internal.LocalSpanStoreEnabled && !span.spanContext.IsSampled() {
  201. return span
  202. }
  203. span.data = &SpanData{
  204. SpanContext: span.spanContext,
  205. StartTime: time.Now(),
  206. SpanKind: o.SpanKind,
  207. Name: name,
  208. HasRemoteParent: remoteParent,
  209. }
  210. span.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
  211. span.annotations = newEvictedQueue(cfg.MaxAnnotationEventsPerSpan)
  212. span.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
  213. span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
  214. if hasParent {
  215. span.data.ParentSpanID = parent.SpanID
  216. }
  217. if internal.LocalSpanStoreEnabled {
  218. var ss *spanStore
  219. ss = spanStoreForNameCreateIfNew(name)
  220. if ss != nil {
  221. span.spanStore = ss
  222. ss.add(span)
  223. }
  224. }
  225. return span
  226. }
  227. // End ends the span.
  228. func (s *Span) End() {
  229. if s == nil {
  230. return
  231. }
  232. if s.executionTracerTaskEnd != nil {
  233. s.executionTracerTaskEnd()
  234. }
  235. if !s.IsRecordingEvents() {
  236. return
  237. }
  238. s.endOnce.Do(func() {
  239. exp, _ := exporters.Load().(exportersMap)
  240. mustExport := s.spanContext.IsSampled() && len(exp) > 0
  241. if s.spanStore != nil || mustExport {
  242. sd := s.makeSpanData()
  243. sd.EndTime = internal.MonotonicEndTime(sd.StartTime)
  244. if s.spanStore != nil {
  245. s.spanStore.finished(s, sd)
  246. }
  247. if mustExport {
  248. for e := range exp {
  249. e.ExportSpan(sd)
  250. }
  251. }
  252. }
  253. })
  254. }
  255. // makeSpanData produces a SpanData representing the current state of the Span.
  256. // It requires that s.data is non-nil.
  257. func (s *Span) makeSpanData() *SpanData {
  258. var sd SpanData
  259. s.mu.Lock()
  260. sd = *s.data
  261. if s.lruAttributes.simpleLruMap.Len() > 0 {
  262. sd.Attributes = s.lruAttributesToAttributeMap()
  263. sd.DroppedAttributeCount = s.lruAttributes.droppedCount
  264. }
  265. if len(s.annotations.queue) > 0 {
  266. sd.Annotations = s.interfaceArrayToAnnotationArray()
  267. sd.DroppedAnnotationCount = s.annotations.droppedCount
  268. }
  269. if len(s.messageEvents.queue) > 0 {
  270. sd.MessageEvents = s.interfaceArrayToMessageEventArray()
  271. sd.DroppedMessageEventCount = s.messageEvents.droppedCount
  272. }
  273. if len(s.links.queue) > 0 {
  274. sd.Links = s.interfaceArrayToLinksArray()
  275. sd.DroppedLinkCount = s.links.droppedCount
  276. }
  277. s.mu.Unlock()
  278. return &sd
  279. }
  280. // SpanContext returns the SpanContext of the span.
  281. func (s *Span) SpanContext() SpanContext {
  282. if s == nil {
  283. return SpanContext{}
  284. }
  285. return s.spanContext
  286. }
  287. // SetName sets the name of the span, if it is recording events.
  288. func (s *Span) SetName(name string) {
  289. if !s.IsRecordingEvents() {
  290. return
  291. }
  292. s.mu.Lock()
  293. s.data.Name = name
  294. s.mu.Unlock()
  295. }
  296. // SetStatus sets the status of the span, if it is recording events.
  297. func (s *Span) SetStatus(status Status) {
  298. if !s.IsRecordingEvents() {
  299. return
  300. }
  301. s.mu.Lock()
  302. s.data.Status = status
  303. s.mu.Unlock()
  304. }
  305. func (s *Span) interfaceArrayToLinksArray() []Link {
  306. linksArr := make([]Link, 0)
  307. for _, value := range s.links.queue {
  308. linksArr = append(linksArr, value.(Link))
  309. }
  310. return linksArr
  311. }
  312. func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
  313. messageEventArr := make([]MessageEvent, 0)
  314. for _, value := range s.messageEvents.queue {
  315. messageEventArr = append(messageEventArr, value.(MessageEvent))
  316. }
  317. return messageEventArr
  318. }
  319. func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
  320. annotationArr := make([]Annotation, 0)
  321. for _, value := range s.annotations.queue {
  322. annotationArr = append(annotationArr, value.(Annotation))
  323. }
  324. return annotationArr
  325. }
  326. func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
  327. attributes := make(map[string]interface{})
  328. for _, key := range s.lruAttributes.simpleLruMap.Keys() {
  329. value, ok := s.lruAttributes.simpleLruMap.Get(key)
  330. if ok {
  331. keyStr := key.(string)
  332. attributes[keyStr] = value
  333. }
  334. }
  335. return attributes
  336. }
  337. func (s *Span) copyToCappedAttributes(attributes []Attribute) {
  338. for _, a := range attributes {
  339. s.lruAttributes.add(a.key, a.value)
  340. }
  341. }
  342. func (s *Span) addChild() {
  343. if !s.IsRecordingEvents() {
  344. return
  345. }
  346. s.mu.Lock()
  347. s.data.ChildSpanCount++
  348. s.mu.Unlock()
  349. }
  350. // AddAttributes sets attributes in the span.
  351. //
  352. // Existing attributes whose keys appear in the attributes parameter are overwritten.
  353. func (s *Span) AddAttributes(attributes ...Attribute) {
  354. if !s.IsRecordingEvents() {
  355. return
  356. }
  357. s.mu.Lock()
  358. s.copyToCappedAttributes(attributes)
  359. s.mu.Unlock()
  360. }
  361. // copyAttributes copies a slice of Attributes into a map.
  362. func copyAttributes(m map[string]interface{}, attributes []Attribute) {
  363. for _, a := range attributes {
  364. m[a.key] = a.value
  365. }
  366. }
  367. func (s *Span) lazyPrintfInternal(attributes []Attribute, format string, a ...interface{}) {
  368. now := time.Now()
  369. msg := fmt.Sprintf(format, a...)
  370. var m map[string]interface{}
  371. s.mu.Lock()
  372. if len(attributes) != 0 {
  373. m = make(map[string]interface{})
  374. copyAttributes(m, attributes)
  375. }
  376. s.annotations.add(Annotation{
  377. Time: now,
  378. Message: msg,
  379. Attributes: m,
  380. })
  381. s.mu.Unlock()
  382. }
  383. func (s *Span) printStringInternal(attributes []Attribute, str string) {
  384. now := time.Now()
  385. var a map[string]interface{}
  386. s.mu.Lock()
  387. if len(attributes) != 0 {
  388. a = make(map[string]interface{})
  389. copyAttributes(a, attributes)
  390. }
  391. s.annotations.add(Annotation{
  392. Time: now,
  393. Message: str,
  394. Attributes: a,
  395. })
  396. s.mu.Unlock()
  397. }
  398. // Annotate adds an annotation with attributes.
  399. // Attributes can be nil.
  400. func (s *Span) Annotate(attributes []Attribute, str string) {
  401. if !s.IsRecordingEvents() {
  402. return
  403. }
  404. s.printStringInternal(attributes, str)
  405. }
  406. // Annotatef adds an annotation with attributes.
  407. func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
  408. if !s.IsRecordingEvents() {
  409. return
  410. }
  411. s.lazyPrintfInternal(attributes, format, a...)
  412. }
  413. // AddMessageSendEvent adds a message send event to the span.
  414. //
  415. // messageID is an identifier for the message, which is recommended to be
  416. // unique in this span and the same between the send event and the receive
  417. // event (this allows to identify a message between the sender and receiver).
  418. // For example, this could be a sequence id.
  419. func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
  420. if !s.IsRecordingEvents() {
  421. return
  422. }
  423. now := time.Now()
  424. s.mu.Lock()
  425. s.messageEvents.add(MessageEvent{
  426. Time: now,
  427. EventType: MessageEventTypeSent,
  428. MessageID: messageID,
  429. UncompressedByteSize: uncompressedByteSize,
  430. CompressedByteSize: compressedByteSize,
  431. })
  432. s.mu.Unlock()
  433. }
  434. // AddMessageReceiveEvent adds a message receive event to the span.
  435. //
  436. // messageID is an identifier for the message, which is recommended to be
  437. // unique in this span and the same between the send event and the receive
  438. // event (this allows to identify a message between the sender and receiver).
  439. // For example, this could be a sequence id.
  440. func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
  441. if !s.IsRecordingEvents() {
  442. return
  443. }
  444. now := time.Now()
  445. s.mu.Lock()
  446. s.messageEvents.add(MessageEvent{
  447. Time: now,
  448. EventType: MessageEventTypeRecv,
  449. MessageID: messageID,
  450. UncompressedByteSize: uncompressedByteSize,
  451. CompressedByteSize: compressedByteSize,
  452. })
  453. s.mu.Unlock()
  454. }
  455. // AddLink adds a link to the span.
  456. func (s *Span) AddLink(l Link) {
  457. if !s.IsRecordingEvents() {
  458. return
  459. }
  460. s.mu.Lock()
  461. s.links.add(l)
  462. s.mu.Unlock()
  463. }
  464. func (s *Span) String() string {
  465. if s == nil {
  466. return "<nil>"
  467. }
  468. if s.data == nil {
  469. return fmt.Sprintf("span %s", s.spanContext.SpanID)
  470. }
  471. s.mu.Lock()
  472. str := fmt.Sprintf("span %s %q", s.spanContext.SpanID, s.data.Name)
  473. s.mu.Unlock()
  474. return str
  475. }
  476. var config atomic.Value // access atomically
  477. func init() {
  478. gen := &defaultIDGenerator{}
  479. // initialize traceID and spanID generators.
  480. var rngSeed int64
  481. for _, p := range []interface{}{
  482. &rngSeed, &gen.traceIDAdd, &gen.nextSpanID, &gen.spanIDInc,
  483. } {
  484. binary.Read(crand.Reader, binary.LittleEndian, p)
  485. }
  486. gen.traceIDRand = rand.New(rand.NewSource(rngSeed))
  487. gen.spanIDInc |= 1
  488. config.Store(&Config{
  489. DefaultSampler: ProbabilitySampler(defaultSamplingProbability),
  490. IDGenerator: gen,
  491. MaxAttributesPerSpan: DefaultMaxAttributesPerSpan,
  492. MaxAnnotationEventsPerSpan: DefaultMaxAnnotationEventsPerSpan,
  493. MaxMessageEventsPerSpan: DefaultMaxMessageEventsPerSpan,
  494. MaxLinksPerSpan: DefaultMaxLinksPerSpan,
  495. })
  496. }
  497. type defaultIDGenerator struct {
  498. sync.Mutex
  499. // Please keep these as the first fields
  500. // so that these 8 byte fields will be aligned on addresses
  501. // divisible by 8, on both 32-bit and 64-bit machines when
  502. // performing atomic increments and accesses.
  503. // See:
  504. // * https://github.com/census-instrumentation/opencensus-go/issues/587
  505. // * https://github.com/census-instrumentation/opencensus-go/issues/865
  506. // * https://golang.org/pkg/sync/atomic/#pkg-note-BUG
  507. nextSpanID uint64
  508. spanIDInc uint64
  509. traceIDAdd [2]uint64
  510. traceIDRand *rand.Rand
  511. }
  512. // NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
  513. func (gen *defaultIDGenerator) NewSpanID() [8]byte {
  514. var id uint64
  515. for id == 0 {
  516. id = atomic.AddUint64(&gen.nextSpanID, gen.spanIDInc)
  517. }
  518. var sid [8]byte
  519. binary.LittleEndian.PutUint64(sid[:], id)
  520. return sid
  521. }
  522. // NewTraceID returns a non-zero trace ID from a randomly-chosen sequence.
  523. // mu should be held while this function is called.
  524. func (gen *defaultIDGenerator) NewTraceID() [16]byte {
  525. var tid [16]byte
  526. // Construct the trace ID from two outputs of traceIDRand, with a constant
  527. // added to each half for additional entropy.
  528. gen.Lock()
  529. binary.LittleEndian.PutUint64(tid[0:8], gen.traceIDRand.Uint64()+gen.traceIDAdd[0])
  530. binary.LittleEndian.PutUint64(tid[8:16], gen.traceIDRand.Uint64()+gen.traceIDAdd[1])
  531. gen.Unlock()
  532. return tid
  533. }