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

Commit

Permalink
allow replaceing trace SDK (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Oct 29, 2020
1 parent de37041 commit 0f7c5f5
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 74 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
10 changes: 10 additions & 0 deletions trace/basetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ type Attribute struct {
value interface{}
}

// Key returns the attribute's key
func (a *Attribute) Key() string {
return a.key
}

// Value returns the attribute's value
func (a *Attribute) Value() interface{} {
return a.value
}

// BoolAttribute returns a bool-valued attribute.
func BoolAttribute(key string, value bool) Attribute {
return Attribute{key: key, value: value}
Expand Down
6 changes: 4 additions & 2 deletions trace/spanstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData {
var out []*SpanData
s.mu.Lock()
defer s.mu.Unlock()
for span := range s.active {
out = append(out, span.makeSpanData())
for activeSpan := range s.active {
if s, ok := activeSpan.internal.(*span); ok {
out = append(out, s.makeSpanData())
}
}
return out
}
Expand Down
112 changes: 60 additions & 52 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ import (
"go.opencensus.io/trace/tracestate"
)

type tracer struct{}

var _ Tracer = &tracer{}

// Span represents a span of a trace. It has an associated SpanContext, and
// stores data accumulated while the span is active.
//
// Ideally users should interact with Spans by calling the functions in this
// package that take a Context parameter.
type Span struct {
type span struct {
// data contains information recorded about the span.
//
// It will be non-nil if we are exporting the span or recording events for it.
Expand Down Expand Up @@ -66,7 +70,7 @@ type Span struct {
// IsRecordingEvents returns true if events are being recorded for this span.
// Use this check to avoid computing expensive annotations when they will never
// be used.
func (s *Span) IsRecordingEvents() bool {
func (s *span) IsRecordingEvents() bool {
if s == nil {
return false
}
Expand Down Expand Up @@ -109,13 +113,13 @@ type SpanContext struct {
type contextKey struct{}

// FromContext returns the Span stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Span {
func (t *tracer) FromContext(ctx context.Context) *Span {
s, _ := ctx.Value(contextKey{}).(*Span)
return s
}

// NewContext returns a new context with the given Span attached.
func NewContext(parent context.Context, s *Span) context.Context {
func (t *tracer) NewContext(parent context.Context, s *Span) context.Context {
return context.WithValue(parent, contextKey{}, s)
}

Expand Down Expand Up @@ -166,12 +170,14 @@ func WithSampler(sampler Sampler) StartOption {
//
// Returned context contains the newly created span. You can use it to
// propagate the returned span in process.
func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
func (t *tracer) StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) {
var opts StartOptions
var parent SpanContext
if p := FromContext(ctx); p != nil {
p.addChild()
parent = p.spanContext
if p := t.FromContext(ctx); p != nil {
if ps, ok := p.internal.(*span); ok {
ps.addChild()
}
parent = p.SpanContext()
}
for _, op := range o {
op(&opts)
Expand All @@ -180,7 +186,8 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont

ctx, end := startExecutionTracerTask(ctx, name)
span.executionTracerTaskEnd = end
return NewContext(ctx, span), span
extSpan := NewSpan(span)
return t.NewContext(ctx, extSpan), extSpan
}

// StartSpanWithRemoteParent starts a new child span of the span from the given parent.
Expand All @@ -190,20 +197,21 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont
//
// Returned context contains the newly created span. You can use it to
// propagate the returned span in process.
func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
func (t *tracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) {
var opts StartOptions
for _, op := range o {
op(&opts)
}
span := startSpanInternal(name, parent != SpanContext{}, parent, true, opts)
ctx, end := startExecutionTracerTask(ctx, name)
span.executionTracerTaskEnd = end
return NewContext(ctx, span), span
extSpan := NewSpan(span)
return t.NewContext(ctx, extSpan), extSpan
}

func startSpanInternal(name string, hasParent bool, parent SpanContext, remoteParent bool, o StartOptions) *Span {
span := &Span{}
span.spanContext = parent
func startSpanInternal(name string, hasParent bool, parent SpanContext, remoteParent bool, o StartOptions) *span {
s := &span{}
s.spanContext = parent

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

if !hasParent {
span.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
s.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
}
span.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
s.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
sampler := cfg.DefaultSampler

if !hasParent || remoteParent || o.Sampler != nil {
Expand All @@ -226,47 +234,47 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
if o.Sampler != nil {
sampler = o.Sampler
}
span.spanContext.setIsSampled(sampler(SamplingParameters{
s.spanContext.setIsSampled(sampler(SamplingParameters{
ParentContext: parent,
TraceID: span.spanContext.TraceID,
SpanID: span.spanContext.SpanID,
TraceID: s.spanContext.TraceID,
SpanID: s.spanContext.SpanID,
Name: name,
HasRemoteParent: remoteParent}).Sample)
}

if !internal.LocalSpanStoreEnabled && !span.spanContext.IsSampled() {
return span
if !internal.LocalSpanStoreEnabled && !s.spanContext.IsSampled() {
return s
}

span.data = &SpanData{
SpanContext: span.spanContext,
s.data = &SpanData{
SpanContext: s.spanContext,
StartTime: time.Now(),
SpanKind: o.SpanKind,
Name: name,
HasRemoteParent: remoteParent,
}
span.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
span.annotations = newEvictedQueue(cfg.MaxAnnotationEventsPerSpan)
span.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
s.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
s.annotations = newEvictedQueue(cfg.MaxAnnotationEventsPerSpan)
s.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
s.links = newEvictedQueue(cfg.MaxLinksPerSpan)

if hasParent {
span.data.ParentSpanID = parent.SpanID
s.data.ParentSpanID = parent.SpanID
}
if internal.LocalSpanStoreEnabled {
var ss *spanStore
ss = spanStoreForNameCreateIfNew(name)
if ss != nil {
span.spanStore = ss
ss.add(span)
s.spanStore = ss
ss.add(NewSpan(s))
}
}

return span
return s
}

// End ends the span.
func (s *Span) End() {
func (s *span) End() {
if s == nil {
return
}
Expand All @@ -283,7 +291,7 @@ func (s *Span) End() {
sd := s.makeSpanData()
sd.EndTime = internal.MonotonicEndTime(sd.StartTime)
if s.spanStore != nil {
s.spanStore.finished(s, sd)
s.spanStore.finished(NewSpan(s), sd)
}
if mustExport {
for e := range exp {
Expand All @@ -296,7 +304,7 @@ func (s *Span) End() {

// makeSpanData produces a SpanData representing the current state of the Span.
// It requires that s.data is non-nil.
func (s *Span) makeSpanData() *SpanData {
func (s *span) makeSpanData() *SpanData {
var sd SpanData
s.mu.Lock()
sd = *s.data
Expand All @@ -321,15 +329,15 @@ func (s *Span) makeSpanData() *SpanData {
}

// SpanContext returns the SpanContext of the span.
func (s *Span) SpanContext() SpanContext {
func (s *span) SpanContext() SpanContext {
if s == nil {
return SpanContext{}
}
return s.spanContext
}

// SetName sets the name of the span, if it is recording events.
func (s *Span) SetName(name string) {
func (s *span) SetName(name string) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -339,7 +347,7 @@ func (s *Span) SetName(name string) {
}

// SetStatus sets the status of the span, if it is recording events.
func (s *Span) SetStatus(status Status) {
func (s *span) SetStatus(status Status) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -348,31 +356,31 @@ func (s *Span) SetStatus(status Status) {
s.mu.Unlock()
}

func (s *Span) interfaceArrayToLinksArray() []Link {
func (s *span) interfaceArrayToLinksArray() []Link {
linksArr := make([]Link, 0, len(s.links.queue))
for _, value := range s.links.queue {
linksArr = append(linksArr, value.(Link))
}
return linksArr
}

func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
func (s *span) interfaceArrayToMessageEventArray() []MessageEvent {
messageEventArr := make([]MessageEvent, 0, len(s.messageEvents.queue))
for _, value := range s.messageEvents.queue {
messageEventArr = append(messageEventArr, value.(MessageEvent))
}
return messageEventArr
}

func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
func (s *span) interfaceArrayToAnnotationArray() []Annotation {
annotationArr := make([]Annotation, 0, len(s.annotations.queue))
for _, value := range s.annotations.queue {
annotationArr = append(annotationArr, value.(Annotation))
}
return annotationArr
}

func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
func (s *span) lruAttributesToAttributeMap() map[string]interface{} {
attributes := make(map[string]interface{}, s.lruAttributes.len())
for _, key := range s.lruAttributes.keys() {
value, ok := s.lruAttributes.get(key)
Expand All @@ -384,13 +392,13 @@ func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
return attributes
}

func (s *Span) copyToCappedAttributes(attributes []Attribute) {
func (s *span) copyToCappedAttributes(attributes []Attribute) {
for _, a := range attributes {
s.lruAttributes.add(a.key, a.value)
}
}

func (s *Span) addChild() {
func (s *span) addChild() {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -402,7 +410,7 @@ func (s *Span) addChild() {
// AddAttributes sets attributes in the span.
//
// Existing attributes whose keys appear in the attributes parameter are overwritten.
func (s *Span) AddAttributes(attributes ...Attribute) {
func (s *span) AddAttributes(attributes ...Attribute) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -418,7 +426,7 @@ func copyAttributes(m map[string]interface{}, attributes []Attribute) {
}
}

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

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

// Annotate adds an annotation with attributes.
// Attributes can be nil.
func (s *Span) Annotate(attributes []Attribute, str string) {
func (s *span) Annotate(attributes []Attribute, str string) {
if !s.IsRecordingEvents() {
return
}
s.printStringInternal(attributes, str)
}

// Annotatef adds an annotation with attributes.
func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
func (s *span) Annotatef(attributes []Attribute, format string, a ...interface{}) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -474,7 +482,7 @@ func (s *Span) Annotatef(attributes []Attribute, format string, a ...interface{}
// unique in this span and the same between the send event and the receive
// event (this allows to identify a message between the sender and receiver).
// For example, this could be a sequence id.
func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
func (s *span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -496,7 +504,7 @@ func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedBy
// unique in this span and the same between the send event and the receive
// event (this allows to identify a message between the sender and receiver).
// For example, this could be a sequence id.
func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
func (s *span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -513,7 +521,7 @@ func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compresse
}

// AddLink adds a link to the span.
func (s *Span) AddLink(l Link) {
func (s *span) AddLink(l Link) {
if !s.IsRecordingEvents() {
return
}
Expand All @@ -522,7 +530,7 @@ func (s *Span) AddLink(l Link) {
s.mu.Unlock()
}

func (s *Span) String() string {
func (s *span) String() string {
if s == nil {
return "<nil>"
}
Expand Down
Loading

0 comments on commit 0f7c5f5

Please sign in to comment.