From 0c0cf971923e337df15e93d9b8ba59526815c9c6 Mon Sep 17 00:00:00 2001 From: Maxim Korolyov Date: Wed, 16 Oct 2019 00:02:21 +0300 Subject: [PATCH] Add public getters to Span (#446) Missed Getters where added so internal Span data became available outside of the package. Fixes #442. Signed-off-by: Maxim Korolyov --- span.go | 26 ++++++++++++++++++++++- span_context.go | 8 ++++++- span_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/span.go b/span.go index fe0c2842..ce3edca3 100644 --- a/span.go +++ b/span.go @@ -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}) } diff --git a/span_context.go b/span_context.go index 6b330487..c0a59009 100644 --- a/span_context.go +++ b/span_context.go @@ -17,9 +17,10 @@ package jaeger import ( "errors" "fmt" - "go.uber.org/atomic" "strconv" "strings" + + "go.uber.org/atomic" ) const ( @@ -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{} diff --git a/span_test.go b/span_test.go index 23c03355..15d59c10 100644 --- a/span_test.go +++ b/span_test.go @@ -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()) + }) + } +}