span.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package trace // import "go.opentelemetry.io/otel/trace"
  4. import (
  5. "context"
  6. "go.opentelemetry.io/otel/attribute"
  7. "go.opentelemetry.io/otel/codes"
  8. "go.opentelemetry.io/otel/trace/embedded"
  9. )
  10. // Span is the individual component of a trace. It represents a single named
  11. // and timed operation of a workflow that is traced. A Tracer is used to
  12. // create a Span and it is then up to the operation the Span represents to
  13. // properly end the Span when the operation itself ends.
  14. //
  15. // Warning: Methods may be added to this interface in minor releases. See
  16. // package documentation on API implementation for information on how to set
  17. // default behavior for unimplemented methods.
  18. type Span interface {
  19. // Users of the interface can ignore this. This embedded type is only used
  20. // by implementations of this interface. See the "API Implementations"
  21. // section of the package documentation for more information.
  22. embedded.Span
  23. // End completes the Span. The Span is considered complete and ready to be
  24. // delivered through the rest of the telemetry pipeline after this method
  25. // is called. Therefore, updates to the Span are not allowed after this
  26. // method has been called.
  27. End(options ...SpanEndOption)
  28. // AddEvent adds an event with the provided name and options.
  29. AddEvent(name string, options ...EventOption)
  30. // AddLink adds a link.
  31. // Adding links at span creation using WithLinks is preferred to calling AddLink
  32. // later, for contexts that are available during span creation, because head
  33. // sampling decisions can only consider information present during span creation.
  34. AddLink(link Link)
  35. // IsRecording returns the recording state of the Span. It will return
  36. // true if the Span is active and events can be recorded.
  37. IsRecording() bool
  38. // RecordError will record err as an exception span event for this span. An
  39. // additional call to SetStatus is required if the Status of the Span should
  40. // be set to Error, as this method does not change the Span status. If this
  41. // span is not being recorded or err is nil then this method does nothing.
  42. RecordError(err error, options ...EventOption)
  43. // SpanContext returns the SpanContext of the Span. The returned SpanContext
  44. // is usable even after the End method has been called for the Span.
  45. SpanContext() SpanContext
  46. // SetStatus sets the status of the Span in the form of a code and a
  47. // description, provided the status hasn't already been set to a higher
  48. // value before (OK > Error > Unset). The description is only included in a
  49. // status when the code is for an error.
  50. SetStatus(code codes.Code, description string)
  51. // SetName sets the Span name.
  52. SetName(name string)
  53. // SetAttributes sets kv as attributes of the Span. If a key from kv
  54. // already exists for an attribute of the Span it will be overwritten with
  55. // the value contained in kv.
  56. SetAttributes(kv ...attribute.KeyValue)
  57. // TracerProvider returns a TracerProvider that can be used to generate
  58. // additional Spans on the same telemetry pipeline as the current Span.
  59. TracerProvider() TracerProvider
  60. }
  61. // Link is the relationship between two Spans. The relationship can be within
  62. // the same Trace or across different Traces.
  63. //
  64. // For example, a Link is used in the following situations:
  65. //
  66. // 1. Batch Processing: A batch of operations may contain operations
  67. // associated with one or more traces/spans. Since there can only be one
  68. // parent SpanContext, a Link is used to keep reference to the
  69. // SpanContext of all operations in the batch.
  70. // 2. Public Endpoint: A SpanContext for an in incoming client request on a
  71. // public endpoint should be considered untrusted. In such a case, a new
  72. // trace with its own identity and sampling decision needs to be created,
  73. // but this new trace needs to be related to the original trace in some
  74. // form. A Link is used to keep reference to the original SpanContext and
  75. // track the relationship.
  76. type Link struct {
  77. // SpanContext of the linked Span.
  78. SpanContext SpanContext
  79. // Attributes describe the aspects of the link.
  80. Attributes []attribute.KeyValue
  81. }
  82. // LinkFromContext returns a link encapsulating the SpanContext in the provided
  83. // ctx.
  84. func LinkFromContext(ctx context.Context, attrs ...attribute.KeyValue) Link {
  85. return Link{
  86. SpanContext: SpanContextFromContext(ctx),
  87. Attributes: attrs,
  88. }
  89. }
  90. // SpanKind is the role a Span plays in a Trace.
  91. type SpanKind int
  92. // As a convenience, these match the proto definition, see
  93. // https://github.com/open-telemetry/opentelemetry-proto/blob/30d237e1ff3ab7aa50e0922b5bebdd93505090af/opentelemetry/proto/trace/v1/trace.proto#L101-L129
  94. //
  95. // The unspecified value is not a valid `SpanKind`. Use `ValidateSpanKind()`
  96. // to coerce a span kind to a valid value.
  97. const (
  98. // SpanKindUnspecified is an unspecified SpanKind and is not a valid
  99. // SpanKind. SpanKindUnspecified should be replaced with SpanKindInternal
  100. // if it is received.
  101. SpanKindUnspecified SpanKind = 0
  102. // SpanKindInternal is a SpanKind for a Span that represents an internal
  103. // operation within an application.
  104. SpanKindInternal SpanKind = 1
  105. // SpanKindServer is a SpanKind for a Span that represents the operation
  106. // of handling a request from a client.
  107. SpanKindServer SpanKind = 2
  108. // SpanKindClient is a SpanKind for a Span that represents the operation
  109. // of client making a request to a server.
  110. SpanKindClient SpanKind = 3
  111. // SpanKindProducer is a SpanKind for a Span that represents the operation
  112. // of a producer sending a message to a message broker. Unlike
  113. // SpanKindClient and SpanKindServer, there is often no direct
  114. // relationship between this kind of Span and a SpanKindConsumer kind. A
  115. // SpanKindProducer Span will end once the message is accepted by the
  116. // message broker which might not overlap with the processing of that
  117. // message.
  118. SpanKindProducer SpanKind = 4
  119. // SpanKindConsumer is a SpanKind for a Span that represents the operation
  120. // of a consumer receiving a message from a message broker. Like
  121. // SpanKindProducer Spans, there is often no direct relationship between
  122. // this Span and the Span that produced the message.
  123. SpanKindConsumer SpanKind = 5
  124. )
  125. // ValidateSpanKind returns a valid span kind value. This will coerce
  126. // invalid values into the default value, SpanKindInternal.
  127. func ValidateSpanKind(spanKind SpanKind) SpanKind {
  128. switch spanKind {
  129. case SpanKindInternal,
  130. SpanKindServer,
  131. SpanKindClient,
  132. SpanKindProducer,
  133. SpanKindConsumer:
  134. // valid
  135. return spanKind
  136. default:
  137. return SpanKindInternal
  138. }
  139. }
  140. // String returns the specified name of the SpanKind in lower-case.
  141. func (sk SpanKind) String() string {
  142. switch sk {
  143. case SpanKindInternal:
  144. return "internal"
  145. case SpanKindServer:
  146. return "server"
  147. case SpanKindClient:
  148. return "client"
  149. case SpanKindProducer:
  150. return "producer"
  151. case SpanKindConsumer:
  152. return "consumer"
  153. default:
  154. return "unspecified"
  155. }
  156. }