trace.go 17 KB

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