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