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