trace_api.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2020, 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. )
  18. // DefaultTracer is the tracer used when package-level exported functions are invoked.
  19. var DefaultTracer Tracer = &tracer{}
  20. // Tracer can start spans and access context functions.
  21. type Tracer interface {
  22. // StartSpan starts a new child span of the current span in the context. If
  23. // there is no span in the context, creates a new trace and span.
  24. //
  25. // Returned context contains the newly created span. You can use it to
  26. // propagate the returned span in process.
  27. StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span)
  28. // StartSpanWithRemoteParent starts a new child span of the span from the given parent.
  29. //
  30. // If the incoming context contains a parent, it ignores. StartSpanWithRemoteParent is
  31. // preferred for cases where the parent is propagated via an incoming request.
  32. //
  33. // Returned context contains the newly created span. You can use it to
  34. // propagate the returned span in process.
  35. StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span)
  36. // FromContext returns the Span stored in a context, or nil if there isn't one.
  37. FromContext(ctx context.Context) *Span
  38. // NewContext returns a new context with the given Span attached.
  39. NewContext(parent context.Context, s *Span) context.Context
  40. }
  41. // StartSpan starts a new child span of the current span in the context. If
  42. // there is no span in the context, creates a new trace and span.
  43. //
  44. // Returned context contains the newly created span. You can use it to
  45. // propagate the returned span in process.
  46. func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
  47. return DefaultTracer.StartSpan(ctx, name, o...)
  48. }
  49. // StartSpanWithRemoteParent starts a new child span of the span from the given parent.
  50. //
  51. // If the incoming context contains a parent, it ignores. StartSpanWithRemoteParent is
  52. // preferred for cases where the parent is propagated via an incoming request.
  53. //
  54. // Returned context contains the newly created span. You can use it to
  55. // propagate the returned span in process.
  56. func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
  57. return DefaultTracer.StartSpanWithRemoteParent(ctx, name, parent, o...)
  58. }
  59. // FromContext returns the Span stored in a context, or a Span that is not
  60. // recording events if there isn't one.
  61. func FromContext(ctx context.Context) *Span {
  62. return DefaultTracer.FromContext(ctx)
  63. }
  64. // NewContext returns a new context with the given Span attached.
  65. func NewContext(parent context.Context, s *Span) context.Context {
  66. return DefaultTracer.NewContext(parent, s)
  67. }
  68. // SpanInterface represents a span of a trace. It has an associated SpanContext, and
  69. // stores data accumulated while the span is active.
  70. //
  71. // Ideally users should interact with Spans by calling the functions in this
  72. // package that take a Context parameter.
  73. type SpanInterface interface {
  74. // IsRecordingEvents returns true if events are being recorded for this span.
  75. // Use this check to avoid computing expensive annotations when they will never
  76. // be used.
  77. IsRecordingEvents() bool
  78. // End ends the span.
  79. End()
  80. // SpanContext returns the SpanContext of the span.
  81. SpanContext() SpanContext
  82. // SetName sets the name of the span, if it is recording events.
  83. SetName(name string)
  84. // SetStatus sets the status of the span, if it is recording events.
  85. SetStatus(status Status)
  86. // AddAttributes sets attributes in the span.
  87. //
  88. // Existing attributes whose keys appear in the attributes parameter are overwritten.
  89. AddAttributes(attributes ...Attribute)
  90. // Annotate adds an annotation with attributes.
  91. // Attributes can be nil.
  92. Annotate(attributes []Attribute, str string)
  93. // Annotatef adds an annotation with attributes.
  94. Annotatef(attributes []Attribute, format string, a ...interface{})
  95. // AddMessageSendEvent adds a message send event to the span.
  96. //
  97. // messageID is an identifier for the message, which is recommended to be
  98. // unique in this span and the same between the send event and the receive
  99. // event (this allows to identify a message between the sender and receiver).
  100. // For example, this could be a sequence id.
  101. AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64)
  102. // AddMessageReceiveEvent adds a message receive event to the span.
  103. //
  104. // messageID is an identifier for the message, which is recommended to be
  105. // unique in this span and the same between the send event and the receive
  106. // event (this allows to identify a message between the sender and receiver).
  107. // For example, this could be a sequence id.
  108. AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64)
  109. // AddLink adds a link to the span.
  110. AddLink(l Link)
  111. // String prints a string representation of a span.
  112. String() string
  113. }
  114. // NewSpan is a convenience function for creating a *Span out of a *span
  115. func NewSpan(s SpanInterface) *Span {
  116. return &Span{internal: s}
  117. }
  118. // Span is a struct wrapper around the SpanInt interface, which allows correctly handling
  119. // nil spans, while also allowing the SpanInterface implementation to be swapped out.
  120. type Span struct {
  121. internal SpanInterface
  122. }
  123. // Internal returns the underlying implementation of the Span
  124. func (s *Span) Internal() SpanInterface {
  125. return s.internal
  126. }
  127. // IsRecordingEvents returns true if events are being recorded for this span.
  128. // Use this check to avoid computing expensive annotations when they will never
  129. // be used.
  130. func (s *Span) IsRecordingEvents() bool {
  131. if s == nil {
  132. return false
  133. }
  134. return s.internal.IsRecordingEvents()
  135. }
  136. // End ends the span.
  137. func (s *Span) End() {
  138. if s == nil {
  139. return
  140. }
  141. s.internal.End()
  142. }
  143. // SpanContext returns the SpanContext of the span.
  144. func (s *Span) SpanContext() SpanContext {
  145. if s == nil {
  146. return SpanContext{}
  147. }
  148. return s.internal.SpanContext()
  149. }
  150. // SetName sets the name of the span, if it is recording events.
  151. func (s *Span) SetName(name string) {
  152. if !s.IsRecordingEvents() {
  153. return
  154. }
  155. s.internal.SetName(name)
  156. }
  157. // SetStatus sets the status of the span, if it is recording events.
  158. func (s *Span) SetStatus(status Status) {
  159. if !s.IsRecordingEvents() {
  160. return
  161. }
  162. s.internal.SetStatus(status)
  163. }
  164. // AddAttributes sets attributes in the span.
  165. //
  166. // Existing attributes whose keys appear in the attributes parameter are overwritten.
  167. func (s *Span) AddAttributes(attributes ...Attribute) {
  168. if !s.IsRecordingEvents() {
  169. return
  170. }
  171. s.internal.AddAttributes(attributes...)
  172. }
  173. // Annotate adds an annotation with attributes.
  174. // Attributes can be nil.
  175. func (s *Span) Annotate(attributes []Attribute, str string) {
  176. if !s.IsRecordingEvents() {
  177. return
  178. }
  179. s.internal.Annotate(attributes, str)
  180. }
  181. // Annotatef adds an annotation with attributes.
  182. func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
  183. if !s.IsRecordingEvents() {
  184. return
  185. }
  186. s.internal.Annotatef(attributes, format, a...)
  187. }
  188. // AddMessageSendEvent adds a message send event to the span.
  189. //
  190. // messageID is an identifier for the message, which is recommended to be
  191. // unique in this span and the same between the send event and the receive
  192. // event (this allows to identify a message between the sender and receiver).
  193. // For example, this could be a sequence id.
  194. func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
  195. if !s.IsRecordingEvents() {
  196. return
  197. }
  198. s.internal.AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize)
  199. }
  200. // AddMessageReceiveEvent adds a message receive event to the span.
  201. //
  202. // messageID is an identifier for the message, which is recommended to be
  203. // unique in this span and the same between the send event and the receive
  204. // event (this allows to identify a message between the sender and receiver).
  205. // For example, this could be a sequence id.
  206. func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
  207. if !s.IsRecordingEvents() {
  208. return
  209. }
  210. s.internal.AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize)
  211. }
  212. // AddLink adds a link to the span.
  213. func (s *Span) AddLink(l Link) {
  214. if !s.IsRecordingEvents() {
  215. return
  216. }
  217. s.internal.AddLink(l)
  218. }
  219. // String prints a string representation of a span.
  220. func (s *Span) String() string {
  221. if s == nil {
  222. return "<nil>"
  223. }
  224. return s.internal.String()
  225. }