Skip to content

Commit

Permalink
Add public getters to Span (jaegertracing#446)
Browse files Browse the repository at this point in the history
Missed Getters where added so internal Span data
became available outside of the package. Fixes jaegertracing#442.

Signed-off-by: Maxim Korolyov <korolyov.maxim@gmail.com>
  • Loading branch information
mkorolyov authored and yurishkuro committed Oct 15, 2019
1 parent 57d24fb commit 0c0cf97
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
26 changes: 25 additions & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,37 @@ func (s *Span) Duration() time.Duration {
func (s *Span) Tags() opentracing.Tags {
s.Lock()
defer s.Unlock()
var result = make(opentracing.Tags)
var result = make(opentracing.Tags, len(s.tags))
for _, tag := range s.tags {
result[tag.key] = tag.value
}
return result
}

// Logs returns micro logs for span
func (s *Span) Logs() []opentracing.LogRecord {
s.Lock()
defer s.Unlock()

return append([]opentracing.LogRecord(nil), s.logs...)
}

// References returns references for this span
func (s *Span) References() []opentracing.SpanReference {
s.Lock()
defer s.Unlock()

if s.references == nil || len(s.references) == 0 {
return nil
}

result := make([]opentracing.SpanReference, len(s.references))
for i, r := range s.references {
result[i] = opentracing.SpanReference{Type: r.Type, ReferencedContext: r.Context}
}
return result
}

func (s *Span) appendTagNoLocking(key string, value interface{}) {
s.tags = append(s.tags, Tag{key: key, value: value})
}
Expand Down
8 changes: 7 additions & 1 deletion span_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package jaeger
import (
"errors"
"fmt"
"go.uber.org/atomic"
"strconv"
"strings"

"go.uber.org/atomic"
)

const (
Expand Down Expand Up @@ -224,6 +225,11 @@ func (c SpanContext) ParentID() SpanID {
return c.parentID
}

// Flags returns the bitmap containing such bits as 'sampled' and 'debug'.
func (c SpanContext) Flags() byte {
return c.samplingState.flags()
}

// NewSpanContext creates a new instance of SpanContext
func NewSpanContext(traceID TraceID, spanID, parentID SpanID, sampled bool, baggage map[string]string) SpanContext {
samplingState := &samplingState{}
Expand Down
55 changes: 55 additions & 0 deletions span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,58 @@ func TestSpanLifecycle(t *testing.T) {
sp1.Release() // Now we will kill the object and return it in the pool
assert.True(t, sp1.tracer == nil, "span must be released")
}

func TestSpan_Logs(t *testing.T) {
tests := []struct {
logs []opentracing.LogRecord
name string
want []opentracing.LogRecord
}{
{
name: "if span logs is return nil",
},
{
name: "if span logs is zero len slice return nil",
logs: []opentracing.LogRecord{},
},
{
name: "if span logs len more than 0 return a copy",
logs: []opentracing.LogRecord{{}},
want: []opentracing.LogRecord{{}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Span{logs: tt.logs}
assert.Equal(t, tt.want, s.Logs())
})
}
}

func TestSpan_References(t *testing.T) {
tests := []struct {
name string
references []Reference
want []opentracing.SpanReference
}{

{
name: "if span references is nil return nil",
},
{
name: "if span references is zero len slice return nil",
references: []Reference{},
},
{
name: "if span references len more than 0 return a copy",
references: []Reference{{}},
want: []opentracing.SpanReference{{ReferencedContext: SpanContext{}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Span{references: tt.references}
assert.Equal(t, tt.want, s.References())
})
}
}

0 comments on commit 0c0cf97

Please sign in to comment.