span.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package opentracing
  2. import (
  3. "time"
  4. "github.com/opentracing/opentracing-go/log"
  5. )
  6. // SpanContext represents Span state that must propagate to descendant Spans and across process
  7. // boundaries (e.g., a <trace_id, span_id, sampled> tuple).
  8. type SpanContext interface {
  9. // ForeachBaggageItem grants access to all baggage items stored in the
  10. // SpanContext.
  11. // The handler function will be called for each baggage key/value pair.
  12. // The ordering of items is not guaranteed.
  13. //
  14. // The bool return value indicates if the handler wants to continue iterating
  15. // through the rest of the baggage items; for example if the handler is trying to
  16. // find some baggage item by pattern matching the name, it can return false
  17. // as soon as the item is found to stop further iterations.
  18. ForeachBaggageItem(handler func(k, v string) bool)
  19. }
  20. // Span represents an active, un-finished span in the OpenTracing system.
  21. //
  22. // Spans are created by the Tracer interface.
  23. type Span interface {
  24. // Sets the end timestamp and finalizes Span state.
  25. //
  26. // With the exception of calls to Context() (which are always allowed),
  27. // Finish() must be the last call made to any span instance, and to do
  28. // otherwise leads to undefined behavior.
  29. Finish()
  30. // FinishWithOptions is like Finish() but with explicit control over
  31. // timestamps and log data.
  32. FinishWithOptions(opts FinishOptions)
  33. // Context() yields the SpanContext for this Span. Note that the return
  34. // value of Context() is still valid after a call to Span.Finish(), as is
  35. // a call to Span.Context() after a call to Span.Finish().
  36. Context() SpanContext
  37. // Sets or changes the operation name.
  38. //
  39. // Returns a reference to this Span for chaining.
  40. SetOperationName(operationName string) Span
  41. // Adds a tag to the span.
  42. //
  43. // If there is a pre-existing tag set for `key`, it is overwritten.
  44. //
  45. // Tag values can be numeric types, strings, or bools. The behavior of
  46. // other tag value types is undefined at the OpenTracing level. If a
  47. // tracing system does not know how to handle a particular value type, it
  48. // may ignore the tag, but shall not panic.
  49. //
  50. // Returns a reference to this Span for chaining.
  51. SetTag(key string, value interface{}) Span
  52. // LogFields is an efficient and type-checked way to record key:value
  53. // logging data about a Span, though the programming interface is a little
  54. // more verbose than LogKV(). Here's an example:
  55. //
  56. // span.LogFields(
  57. // log.String("event", "soft error"),
  58. // log.String("type", "cache timeout"),
  59. // log.Int("waited.millis", 1500))
  60. //
  61. // Also see Span.FinishWithOptions() and FinishOptions.BulkLogData.
  62. LogFields(fields ...log.Field)
  63. // LogKV is a concise, readable way to record key:value logging data about
  64. // a Span, though unfortunately this also makes it less efficient and less
  65. // type-safe than LogFields(). Here's an example:
  66. //
  67. // span.LogKV(
  68. // "event", "soft error",
  69. // "type", "cache timeout",
  70. // "waited.millis", 1500)
  71. //
  72. // For LogKV (as opposed to LogFields()), the parameters must appear as
  73. // key-value pairs, like
  74. //
  75. // span.LogKV(key1, val1, key2, val2, key3, val3, ...)
  76. //
  77. // The keys must all be strings. The values may be strings, numeric types,
  78. // bools, Go error instances, or arbitrary structs.
  79. //
  80. // (Note to implementors: consider the log.InterleavedKVToFields() helper)
  81. LogKV(alternatingKeyValues ...interface{})
  82. // SetBaggageItem sets a key:value pair on this Span and its SpanContext
  83. // that also propagates to descendants of this Span.
  84. //
  85. // SetBaggageItem() enables powerful functionality given a full-stack
  86. // opentracing integration (e.g., arbitrary application data from a mobile
  87. // app can make it, transparently, all the way into the depths of a storage
  88. // system), and with it some powerful costs: use this feature with care.
  89. //
  90. // IMPORTANT NOTE #1: SetBaggageItem() will only propagate baggage items to
  91. // *future* causal descendants of the associated Span.
  92. //
  93. // IMPORTANT NOTE #2: Use this thoughtfully and with care. Every key and
  94. // value is copied into every local *and remote* child of the associated
  95. // Span, and that can add up to a lot of network and cpu overhead.
  96. //
  97. // Returns a reference to this Span for chaining.
  98. SetBaggageItem(restrictedKey, value string) Span
  99. // Gets the value for a baggage item given its key. Returns the empty string
  100. // if the value isn't found in this Span.
  101. BaggageItem(restrictedKey string) string
  102. // Provides access to the Tracer that created this Span.
  103. Tracer() Tracer
  104. // Deprecated: use LogFields or LogKV
  105. LogEvent(event string)
  106. // Deprecated: use LogFields or LogKV
  107. LogEventWithPayload(event string, payload interface{})
  108. // Deprecated: use LogFields or LogKV
  109. Log(data LogData)
  110. }
  111. // LogRecord is data associated with a single Span log. Every LogRecord
  112. // instance must specify at least one Field.
  113. type LogRecord struct {
  114. Timestamp time.Time
  115. Fields []log.Field
  116. }
  117. // FinishOptions allows Span.FinishWithOptions callers to override the finish
  118. // timestamp and provide log data via a bulk interface.
  119. type FinishOptions struct {
  120. // FinishTime overrides the Span's finish time, or implicitly becomes
  121. // time.Now() if FinishTime.IsZero().
  122. //
  123. // FinishTime must resolve to a timestamp that's >= the Span's StartTime
  124. // (per StartSpanOptions).
  125. FinishTime time.Time
  126. // LogRecords allows the caller to specify the contents of many LogFields()
  127. // calls with a single slice. May be nil.
  128. //
  129. // None of the LogRecord.Timestamp values may be .IsZero() (i.e., they must
  130. // be set explicitly). Also, they must be >= the Span's start timestamp and
  131. // <= the FinishTime (or time.Now() if FinishTime.IsZero()). Otherwise the
  132. // behavior of FinishWithOptions() is undefined.
  133. //
  134. // If specified, the caller hands off ownership of LogRecords at
  135. // FinishWithOptions() invocation time.
  136. //
  137. // If specified, the (deprecated) BulkLogData must be nil or empty.
  138. LogRecords []LogRecord
  139. // BulkLogData is DEPRECATED.
  140. BulkLogData []LogData
  141. }
  142. // LogData is DEPRECATED
  143. type LogData struct {
  144. Timestamp time.Time
  145. Event string
  146. Payload interface{}
  147. }
  148. // ToLogRecord converts a deprecated LogData to a non-deprecated LogRecord
  149. func (ld *LogData) ToLogRecord() LogRecord {
  150. var literalTimestamp time.Time
  151. if ld.Timestamp.IsZero() {
  152. literalTimestamp = time.Now()
  153. } else {
  154. literalTimestamp = ld.Timestamp
  155. }
  156. rval := LogRecord{
  157. Timestamp: literalTimestamp,
  158. }
  159. if ld.Payload == nil {
  160. rval.Fields = []log.Field{
  161. log.String("event", ld.Event),
  162. }
  163. } else {
  164. rval.Fields = []log.Field{
  165. log.String("event", ld.Event),
  166. log.Object("payload", ld.Payload),
  167. }
  168. }
  169. return rval
  170. }