Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 0f7c5f5

Browse files
authored
allow replaceing trace SDK (#1238)
1 parent de37041 commit 0f7c5f5

6 files changed

Lines changed: 360 additions & 74 deletions

File tree

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
4848
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4949
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
5050
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
51+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
52+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5153
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5254
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5355
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

trace/basetypes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ type Attribute struct {
4949
value interface{}
5050
}
5151

52+
// Key returns the attribute's key
53+
func (a *Attribute) Key() string {
54+
return a.key
55+
}
56+
57+
// Value returns the attribute's value
58+
func (a *Attribute) Value() interface{} {
59+
return a.value
60+
}
61+
5262
// BoolAttribute returns a bool-valued attribute.
5363
func BoolAttribute(key string, value bool) Attribute {
5464
return Attribute{key: key, value: value}

trace/spanstore.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData {
4848
var out []*SpanData
4949
s.mu.Lock()
5050
defer s.mu.Unlock()
51-
for span := range s.active {
52-
out = append(out, span.makeSpanData())
51+
for activeSpan := range s.active {
52+
if s, ok := activeSpan.internal.(*span); ok {
53+
out = append(out, s.makeSpanData())
54+
}
5355
}
5456
return out
5557
}

trace/trace.go

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ import (
2828
"go.opencensus.io/trace/tracestate"
2929
)
3030

31+
type tracer struct{}
32+
33+
var _ Tracer = &tracer{}
34+
3135
// Span represents a span of a trace. It has an associated SpanContext, and
3236
// stores data accumulated while the span is active.
3337
//
3438
// Ideally users should interact with Spans by calling the functions in this
3539
// package that take a Context parameter.
36-
type Span struct {
40+
type span struct {
3741
// data contains information recorded about the span.
3842
//
3943
// It will be non-nil if we are exporting the span or recording events for it.
@@ -66,7 +70,7 @@ type Span struct {
6670
// IsRecordingEvents returns true if events are being recorded for this span.
6771
// Use this check to avoid computing expensive annotations when they will never
6872
// be used.
69-
func (s *Span) IsRecordingEvents() bool {
73+
func (s *span) IsRecordingEvents() bool {
7074
if s == nil {
7175
return false
7276
}
@@ -109,13 +113,13 @@ type SpanContext struct {
109113
type contextKey struct{}
110114

111115
// FromContext returns the Span stored in a context, or nil if there isn't one.
112-
func FromContext(ctx context.Context) *Span {
116+
func (t *tracer) FromContext(ctx context.Context) *Span {
113117
s, _ := ctx.Value(contextKey{}).(*Span)
114118
return s
115119
}
116120

117121
// NewContext returns a new context with the given Span attached.
118-
func NewContext(parent context.Context, s *Span) context.Context {
122+
func (t *tracer) NewContext(parent context.Context, s *Span) context.Context {
119123
return context.WithValue(parent, contextKey{}, s)
120124
}
121125

@@ -166,12 +170,14 @@ func WithSampler(sampler Sampler) StartOption {
166170
//
167171
// Returned context contains the newly created span. You can use it to
168172
// propagate the returned span in process.
169-
func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
173+
func (t *tracer) StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
170174
var opts StartOptions
171175
var parent SpanContext
172-
if p := FromContext(ctx); p != nil {
173-
p.addChild()
174-
parent = p.spanContext
176+
if p := t.FromContext(ctx); p != nil {
177+
if ps, ok := p.internal.(*span); ok {
178+
ps.addChild()
179+
}
180+
parent = p.SpanContext()
175181
}
176182
for _, op := range o {
177183
op(&opts)
@@ -180,7 +186,8 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont
180186

181187
ctx, end := startExecutionTracerTask(ctx, name)
182188
span.executionTracerTaskEnd = end
183-
return NewContext(ctx, span), span
189+
extSpan := NewSpan(span)
190+
return t.NewContext(ctx, extSpan), extSpan
184191
}
185192

186193
// StartSpanWithRemoteParent starts a new child span of the span from the given parent.
@@ -190,20 +197,21 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont
190197
//
191198
// Returned context contains the newly created span. You can use it to
192199
// propagate the returned span in process.
193-
func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
200+
func (t *tracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
194201
var opts StartOptions
195202
for _, op := range o {
196203
op(&opts)
197204
}
198205
span := startSpanInternal(name, parent != SpanContext{}, parent, true, opts)
199206
ctx, end := startExecutionTracerTask(ctx, name)
200207
span.executionTracerTaskEnd = end
201-
return NewContext(ctx, span), span
208+
extSpan := NewSpan(span)
209+
return t.NewContext(ctx, extSpan), extSpan
202210
}
203211

204-
func startSpanInternal(name string, hasParent bool, parent SpanContext, remoteParent bool, o StartOptions) *Span {
205-
span := &Span{}
206-
span.spanContext = parent
212+
func startSpanInternal(name string, hasParent bool, parent SpanContext, remoteParent bool, o StartOptions) *span {
213+
s := &span{}
214+
s.spanContext = parent
207215

208216
cfg := config.Load().(*Config)
209217
if gen, ok := cfg.IDGenerator.(*defaultIDGenerator); ok {
@@ -212,9 +220,9 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
212220
}
213221

214222
if !hasParent {
215-
span.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
223+
s.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
216224
}
217-
span.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
225+
s.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
218226
sampler := cfg.DefaultSampler
219227

220228
if !hasParent || remoteParent || o.Sampler != nil {
@@ -226,47 +234,47 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
226234
if o.Sampler != nil {
227235
sampler = o.Sampler
228236
}
229-
span.spanContext.setIsSampled(sampler(SamplingParameters{
237+
s.spanContext.setIsSampled(sampler(SamplingParameters{
230238
ParentContext: parent,
231-
TraceID: span.spanContext.TraceID,
232-
SpanID: span.spanContext.SpanID,
239+
TraceID: s.spanContext.TraceID,
240+
SpanID: s.spanContext.SpanID,
233241
Name: name,
234242
HasRemoteParent: remoteParent}).Sample)
235243
}
236244

237-
if !internal.LocalSpanStoreEnabled && !span.spanContext.IsSampled() {
238-
return span
245+
if !internal.LocalSpanStoreEnabled && !s.spanContext.IsSampled() {
246+
return s
239247
}
240248

241-
span.data = &SpanData{
242-
SpanContext: span.spanContext,
249+
s.data = &SpanData{
250+
SpanContext: s.spanContext,
243251
StartTime: time.Now(),
244252
SpanKind: o.SpanKind,
245253
Name: name,
246254
HasRemoteParent: remoteParent,
247255
}
248-
span.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
249-
span.annotations = newEvictedQueue(cfg.MaxAnnotationEventsPerSpan)
250-
span.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
251-
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
256+
s.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
257+
s.annotations = newEvictedQueue(cfg.MaxAnnotationEventsPerSpan)
258+
s.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
259+
s.links = newEvictedQueue(cfg.MaxLinksPerSpan)
252260

253261
if hasParent {
254-
span.data.ParentSpanID = parent.SpanID
262+
s.data.ParentSpanID = parent.SpanID
255263
}
256264
if internal.LocalSpanStoreEnabled {
257265
var ss *spanStore
258266
ss = spanStoreForNameCreateIfNew(name)
259267
if ss != nil {
260-
span.spanStore = ss
261-
ss.add(span)
268+
s.spanStore = ss
269+
ss.add(NewSpan(s))
262270
}
263271
}
264272

265-
return span
273+
return s
266274
}
267275

268276
// End ends the span.
269-
func (s *Span) End() {
277+
func (s *span) End() {
270278
if s == nil {
271279
return
272280
}
@@ -283,7 +291,7 @@ func (s *Span) End() {
283291
sd := s.makeSpanData()
284292
sd.EndTime = internal.MonotonicEndTime(sd.StartTime)
285293
if s.spanStore != nil {
286-
s.spanStore.finished(s, sd)
294+
s.spanStore.finished(NewSpan(s), sd)
287295
}
288296
if mustExport {
289297
for e := range exp {
@@ -296,7 +304,7 @@ func (s *Span) End() {
296304

297305
// makeSpanData produces a SpanData representing the current state of the Span.
298306
// It requires that s.data is non-nil.
299-
func (s *Span) makeSpanData() *SpanData {
307+
func (s *span) makeSpanData() *SpanData {
300308
var sd SpanData
301309
s.mu.Lock()
302310
sd = *s.data
@@ -321,15 +329,15 @@ func (s *Span) makeSpanData() *SpanData {
321329
}
322330

323331
// SpanContext returns the SpanContext of the span.
324-
func (s *Span) SpanContext() SpanContext {
332+
func (s *span) SpanContext() SpanContext {
325333
if s == nil {
326334
return SpanContext{}
327335
}
328336
return s.spanContext
329337
}
330338

331339
// SetName sets the name of the span, if it is recording events.
332-
func (s *Span) SetName(name string) {
340+
func (s *span) SetName(name string) {
333341
if !s.IsRecordingEvents() {
334342
return
335343
}
@@ -339,7 +347,7 @@ func (s *Span) SetName(name string) {
339347
}
340348

341349
// SetStatus sets the status of the span, if it is recording events.
342-
func (s *Span) SetStatus(status Status) {
350+
func (s *span) SetStatus(status Status) {
343351
if !s.IsRecordingEvents() {
344352
return
345353
}
@@ -348,31 +356,31 @@ func (s *Span) SetStatus(status Status) {
348356
s.mu.Unlock()
349357
}
350358

351-
func (s *Span) interfaceArrayToLinksArray() []Link {
359+
func (s *span) interfaceArrayToLinksArray() []Link {
352360
linksArr := make([]Link, 0, len(s.links.queue))
353361
for _, value := range s.links.queue {
354362
linksArr = append(linksArr, value.(Link))
355363
}
356364
return linksArr
357365
}
358366

359-
func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
367+
func (s *span) interfaceArrayToMessageEventArray() []MessageEvent {
360368
messageEventArr := make([]MessageEvent, 0, len(s.messageEvents.queue))
361369
for _, value := range s.messageEvents.queue {
362370
messageEventArr = append(messageEventArr, value.(MessageEvent))
363371
}
364372
return messageEventArr
365373
}
366374

367-
func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
375+
func (s *span) interfaceArrayToAnnotationArray() []Annotation {
368376
annotationArr := make([]Annotation, 0, len(s.annotations.queue))
369377
for _, value := range s.annotations.queue {
370378
annotationArr = append(annotationArr, value.(Annotation))
371379
}
372380
return annotationArr
373381
}
374382

375-
func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
383+
func (s *span) lruAttributesToAttributeMap() map[string]interface{} {
376384
attributes := make(map[string]interface{}, s.lruAttributes.len())
377385
for _, key := range s.lruAttributes.keys() {
378386
value, ok := s.lruAttributes.get(key)
@@ -384,13 +392,13 @@ func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
384392
return attributes
385393
}
386394

387-
func (s *Span) copyToCappedAttributes(attributes []Attribute) {
395+
func (s *span) copyToCappedAttributes(attributes []Attribute) {
388396
for _, a := range attributes {
389397
s.lruAttributes.add(a.key, a.value)
390398
}
391399
}
392400

393-
func (s *Span) addChild() {
401+
func (s *span) addChild() {
394402
if !s.IsRecordingEvents() {
395403
return
396404
}
@@ -402,7 +410,7 @@ func (s *Span) addChild() {
402410
// AddAttributes sets attributes in the span.
403411
//
404412
// Existing attributes whose keys appear in the attributes parameter are overwritten.
405-
func (s *Span) AddAttributes(attributes ...Attribute) {
413+
func (s *span) AddAttributes(attributes ...Attribute) {
406414
if !s.IsRecordingEvents() {
407415
return
408416
}
@@ -418,7 +426,7 @@ func copyAttributes(m map[string]interface{}, attributes []Attribute) {
418426
}
419427
}
420428

421-
func (s *Span) lazyPrintfInternal(attributes []Attribute, format string, a ...interface{}) {
429+
func (s *span) lazyPrintfInternal(attributes []Attribute, format string, a ...interface{}) {
422430
now := time.Now()
423431
msg := fmt.Sprintf(format, a...)
424432
var m map[string]interface{}
@@ -435,7 +443,7 @@ func (s *Span) lazyPrintfInternal(attributes []Attribute, format string, a ...in
435443
s.mu.Unlock()
436444
}
437445

438-
func (s *Span) printStringInternal(attributes []Attribute, str string) {
446+
func (s *span) printStringInternal(attributes []Attribute, str string) {
439447
now := time.Now()
440448
var a map[string]interface{}
441449
s.mu.Lock()
@@ -453,15 +461,15 @@ func (s *Span) printStringInternal(attributes []Attribute, str string) {
453461

454462
// Annotate adds an annotation with attributes.
455463
// Attributes can be nil.
456-
func (s *Span) Annotate(attributes []Attribute, str string) {
464+
func (s *span) Annotate(attributes []Attribute, str string) {
457465
if !s.IsRecordingEvents() {
458466
return
459467
}
460468
s.printStringInternal(attributes, str)
461469
}
462470

463471
// Annotatef adds an annotation with attributes.
464-
func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
472+
func (s *span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
465473
if !s.IsRecordingEvents() {
466474
return
467475
}
@@ -474,7 +482,7 @@ func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}
474482
// unique in this span and the same between the send event and the receive
475483
// event (this allows to identify a message between the sender and receiver).
476484
// For example, this could be a sequence id.
477-
func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
485+
func (s *span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
478486
if !s.IsRecordingEvents() {
479487
return
480488
}
@@ -496,7 +504,7 @@ func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedBy
496504
// unique in this span and the same between the send event and the receive
497505
// event (this allows to identify a message between the sender and receiver).
498506
// For example, this could be a sequence id.
499-
func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
507+
func (s *span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
500508
if !s.IsRecordingEvents() {
501509
return
502510
}
@@ -513,7 +521,7 @@ func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compresse
513521
}
514522

515523
// AddLink adds a link to the span.
516-
func (s *Span) AddLink(l Link) {
524+
func (s *span) AddLink(l Link) {
517525
if !s.IsRecordingEvents() {
518526
return
519527
}
@@ -522,7 +530,7 @@ func (s *Span) AddLink(l Link) {
522530
s.mu.Unlock()
523531
}
524532

525-
func (s *Span) String() string {
533+
func (s *span) String() string {
526534
if s == nil {
527535
return "<nil>"
528536
}

0 commit comments

Comments
 (0)