From 402bec9e6ead856e972bfa449c9c5d6b8965aef1 Mon Sep 17 00:00:00 2001 From: Curtis Allen Date: Fri, 5 Jul 2019 18:00:40 -0400 Subject: [PATCH] Expose span data to custom reporters [fixes #394] (#399) * fixes #394 Signed-off-by: Curtis Allen * use mutexs Signed-off-by: Curtis Allen --- span.go | 32 ++++++++++++++++++++++++++++++++ span_test.go | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/span.go b/span.go index 89195f14..2b5cd37e 100644 --- a/span.go +++ b/span.go @@ -96,6 +96,38 @@ func (s *Span) SetTag(key string, value interface{}) opentracing.Span { return s } +// SpanContext returns span context +func (s *Span) SpanContext() SpanContext { + s.Lock() + defer s.Unlock() + return s.context +} + +// StartTime returns span start time +func (s *Span) StartTime() time.Time { + s.Lock() + defer s.Unlock() + return s.startTime +} + +// Duration returns span duration +func (s *Span) Duration() time.Duration { + s.Lock() + defer s.Unlock() + return s.duration +} + +// Tags returns tags for span +func (s *Span) Tags() opentracing.Tags { + s.Lock() + defer s.Unlock() + var result = make(opentracing.Tags) + for _, tag := range s.tags { + result[tag.key] = tag.value + } + return result +} + func (s *Span) setTagNoLocking(key string, value interface{}) { s.tags = append(s.tags, Tag{key: key, value: value}) } diff --git a/span_test.go b/span_test.go index 89d6c83d..1dd781d8 100644 --- a/span_test.go +++ b/span_test.go @@ -79,8 +79,18 @@ func TestSpanProperties(t *testing.T) { defer closer.Close() sp1 := tracer.StartSpan("s1").(*Span) + sp1.SetTag("foo", "bar") + var expectedTags = make(opentracing.Tags) + expectedTags["foo"] = "bar" + expectedTags["sampler.type"] = "const" + expectedTags["sampler.param"] = true + assert.Equal(t, tracer, sp1.Tracer()) assert.NotNil(t, sp1.Context()) + assert.Equal(t, sp1.context, sp1.SpanContext()) + assert.Equal(t, sp1.startTime, sp1.StartTime()) + assert.Equal(t, sp1.duration, sp1.Duration()) + assert.Equal(t, sp1.Tags(), expectedTags) } func TestSpanOperationName(t *testing.T) {