@@ -16,6 +16,7 @@ package trace
16
16
17
17
import (
18
18
"context"
19
+ "sync/atomic"
19
20
"time"
20
21
21
22
"google.golang.org/grpc/codes"
@@ -31,14 +32,21 @@ type (
31
32
Tracer interface {
32
33
Start (context.Context , string , ... SpanOption ) (context.Context , Span )
33
34
35
+ // WithSpan wraps the execution of the function body with a span.
36
+ // It starts a new span and sets it as an active span in the context.
37
+ // It then executes the body. It closes the span before returning the execution result.
38
+ // TODO: Should it restore the previous span?
34
39
WithSpan (
35
40
ctx context.Context ,
36
41
operation string ,
37
42
body func (ctx context.Context ) error ,
38
43
) error
39
44
45
+ // TODO: Do we need WithService and WithComponent?
40
46
WithService (name string ) Tracer
41
47
WithComponent (name string ) Tracer
48
+
49
+ // WithResources attaches resource attributes to the Tracer.
42
50
WithResources (res ... core.KeyValue ) Tracer
43
51
44
52
// Note: see https://github.com/opentracing/opentracing-go/issues/127
@@ -53,39 +61,50 @@ type (
53
61
54
62
stats.Interface
55
63
56
- SetError (bool )
57
-
64
+ // Tracer returns tracer used to create this span. Tracer cannot be nil.
58
65
Tracer () Tracer
59
66
67
+ // Finish completes the span. No updates are allowed to span after it
68
+ // finishes. The only exception is setting status of the span.
60
69
Finish ()
61
70
62
71
// AddEvent adds an event to the span.
63
72
AddEvent (ctx context.Context , event event.Event )
64
73
65
- // IsRecordingEvents returns true is the span is active and recording events is enabled.
74
+ // IsRecordingEvents returns true if the span is active and recording events is enabled.
66
75
IsRecordingEvents () bool
67
76
68
77
// SpancContext returns span context of the span. Return SpanContext is usable
69
78
// even after the span is finished.
70
79
SpanContext () core.SpanContext
71
80
81
+ // SetStatus sets the status of the span. The status of the span can be updated
82
+ // even after span is finished.
72
83
SetStatus (codes.Code )
73
84
}
74
85
75
86
Injector interface {
87
+ // Inject serializes span context and tag.Map and inserts them in to
88
+ // carrier associated with the injector. For example in case of http request,
89
+ // span context could added to the request (carrier) as W3C Trace context header.
76
90
Inject (core.SpanContext , tag.Map )
77
91
}
78
92
79
93
// SpanOption apply changes to SpanOptions.
80
94
SpanOption func (* SpanOptions )
81
95
96
+ // SpanOptions provides options to set properties of span at the time of starting
97
+ // a new span.
82
98
SpanOptions struct {
83
- attributes []core.KeyValue
84
- startTime time.Time
85
- reference Reference
86
- recordEvent bool
99
+ Attributes []core.KeyValue
100
+ StartTime time.Time
101
+ Reference Reference
102
+ RecordEvent bool
87
103
}
88
104
105
+ // Reference is used to establish relationship between newly created span and the
106
+ // other span. The other span could be related as a parent or linked or any other
107
+ // future relationship type.
89
108
Reference struct {
90
109
core.SpanContext
91
110
RelationshipType
@@ -94,39 +113,49 @@ type (
94
113
RelationshipType int
95
114
)
96
115
116
+ var (
117
+ // The process global tracer could have process-wide resource
118
+ // tags applied directly, or we can have a SetGlobal tracer to
119
+ // install a default tracer w/ resources.
120
+ global atomic.Value
121
+
122
+ // TODO: create NOOP Tracer and register it instead of creating empty tracer here.
123
+ nt = & noopTracer {}
124
+ )
125
+
97
126
const (
98
127
ChildOfRelationship RelationshipType = iota
99
128
FollowsFromRelationship
100
129
)
101
130
131
+ // GlobalTracer return tracer registered with global registry.
132
+ // If no tracer is registered then an instance of noop Tracer is returned.
102
133
func GlobalTracer () Tracer {
103
134
if t := global .Load (); t != nil {
104
135
return t .(Tracer )
105
136
}
106
- return empty
137
+ return nt
107
138
}
108
139
140
+ // SetGlobalTracer sets provided tracer as a global tracer.
109
141
func SetGlobalTracer (t Tracer ) {
110
142
global .Store (t )
111
143
}
112
144
145
+ // Start starts a new span using registered global tracer.
113
146
func Start (ctx context.Context , name string , opts ... SpanOption ) (context.Context , Span ) {
114
147
return GlobalTracer ().Start (ctx , name , opts ... )
115
148
}
116
149
150
+ // Active returns current span from the context.
117
151
func Active (ctx context.Context ) Span {
118
- span , _ := scope .Active (ctx ).(* span )
152
+ span , _ := scope .Active (ctx ).(Span )
119
153
return span
120
154
}
121
155
122
- func WithSpan (ctx context.Context , name string , body func (context.Context ) error ) error {
123
- return GlobalTracer ().WithSpan (ctx , name , body )
124
- }
125
-
126
- func SetError (ctx context.Context , v bool ) {
127
- Active (ctx ).SetError (v )
128
- }
129
-
156
+ // Inject is convenient function to inject current span context using injector.
157
+ // Injector is expected to serialize span context and inject it in to a carrier.
158
+ // An example of a carrier is http request.
130
159
func Inject (ctx context.Context , injector Injector ) {
131
160
span := Active (ctx )
132
161
if span == nil {
@@ -136,36 +165,46 @@ func Inject(ctx context.Context, injector Injector) {
136
165
span .Tracer ().Inject (ctx , span , injector )
137
166
}
138
167
168
+ // WithStartTime sets the start time of the span to provided time t, when it is started.
169
+ // In absensce of this option, wall clock time is used as start time.
170
+ // This option is typically used when starting of the span is delayed.
139
171
func WithStartTime (t time.Time ) SpanOption {
140
172
return func (o * SpanOptions ) {
141
- o .startTime = t
173
+ o .StartTime = t
142
174
}
143
175
}
144
176
177
+ // WithAttributes sets attributes to span. These attributes provides additional
178
+ // data about the span.
145
179
func WithAttributes (attrs ... core.KeyValue ) SpanOption {
146
180
return func (o * SpanOptions ) {
147
- o .attributes = attrs
181
+ o .Attributes = attrs
148
182
}
149
183
}
150
184
185
+ // WithRecordEvents enables recording of the events while the span is active.
186
+ // In the absence of this option, RecordEvent is set to false, disabling any recording of
187
+ // the events.
151
188
func WithRecordEvents () SpanOption {
152
189
return func (o * SpanOptions ) {
153
- o .recordEvent = true
190
+ o .RecordEvent = true
154
191
}
155
192
}
156
193
194
+ // ChildOf. TODO: do we need this?.
157
195
func ChildOf (sc core.SpanContext ) SpanOption {
158
196
return func (o * SpanOptions ) {
159
- o .reference = Reference {
197
+ o .Reference = Reference {
160
198
SpanContext : sc ,
161
199
RelationshipType : ChildOfRelationship ,
162
200
}
163
201
}
164
202
}
165
203
204
+ // FollowsFrom. TODO: do we need this?.
166
205
func FollowsFrom (sc core.SpanContext ) SpanOption {
167
206
return func (o * SpanOptions ) {
168
- o .reference = Reference {
207
+ o .Reference = Reference {
169
208
SpanContext : sc ,
170
209
RelationshipType : FollowsFromRelationship ,
171
210
}
0 commit comments