From 449a42da57c0fd743c254a3c7f145fbf67505f06 Mon Sep 17 00:00:00 2001 From: Antonin Amand Date: Fri, 23 Sep 2016 23:40:08 +0200 Subject: [PATCH] Pass options along when creating a span with StartSpanFromContext (#114) --- gocontext.go | 6 +++--- gocontext_test.go | 28 ++++++++++++++++++++++++++-- testtracer_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/gocontext.go b/gocontext.go index 2745016..222a652 100644 --- a/gocontext.go +++ b/gocontext.go @@ -48,10 +48,10 @@ func StartSpanFromContext(ctx context.Context, operationName string, opts ...Sta func startSpanFromContextWithTracer(ctx context.Context, tracer Tracer, operationName string, opts ...StartSpanOption) (Span, context.Context) { var span Span if parentSpan := SpanFromContext(ctx); parentSpan != nil { - span = tracer.StartSpan( - operationName, ChildOf(parentSpan.Context())) + opts = append(opts, ChildOf(parentSpan.Context())) + span = tracer.StartSpan(operationName, opts...) } else { - span = tracer.StartSpan(operationName) + span = tracer.StartSpan(operationName, opts...) } return span, ContextWithSpan(ctx, span) } diff --git a/gocontext_test.go b/gocontext_test.go index 225521e..65c0130 100644 --- a/gocontext_test.go +++ b/gocontext_test.go @@ -2,7 +2,9 @@ package opentracing import ( "testing" + "time" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) @@ -38,7 +40,7 @@ func TestStartSpanFromContext(t *testing.T) { if !childSpan.Context().(testSpanContext).HasParent { t.Errorf("Failed to find parent: %v", childSpan) } - if childSpan != SpanFromContext(childCtx) { + if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) { t.Errorf("Unable to find child span in context: %v", childCtx) } } @@ -50,8 +52,30 @@ func TestStartSpanFromContext(t *testing.T) { if childSpan.Context().(testSpanContext).HasParent { t.Errorf("Should not have found parent: %v", childSpan) } - if childSpan != SpanFromContext(childCtx) { + if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) { t.Errorf("Unable to find child span in context: %v", childCtx) } } } + +func TestStartSpanFromContextOptions(t *testing.T) { + testTracer := testTracer{} + + // Test options are passed to tracer + + startTime := time.Now().Add(-10 * time.Second) // ten seconds ago + span, ctx := startSpanFromContextWithTracer( + context.Background(), testTracer, "parent", StartTime(startTime), Tag{"component", "test"}) + + assert.Equal(t, "test", span.(testSpan).Tags["component"]) + assert.Equal(t, startTime, span.(testSpan).StartTime) + + // Test it also works for a child span + + childStartTime := startTime.Add(3 * time.Second) + childSpan, _ := startSpanFromContextWithTracer( + ctx, testTracer, "child", StartTime(childStartTime)) + + assert.Equal(t, childSpan.(testSpan).Tags["component"], nil) + assert.Equal(t, childSpan.(testSpan).StartTime, childStartTime) +} diff --git a/testtracer_test.go b/testtracer_test.go index cf0b0b0..7d9106e 100644 --- a/testtracer_test.go +++ b/testtracer_test.go @@ -3,6 +3,7 @@ package opentracing import ( "strconv" "strings" + "time" ) const testHTTPHeaderPrefix = "testprefix-" @@ -28,6 +29,35 @@ func (n testSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {} type testSpan struct { spanContext testSpanContext OperationName string + StartTime time.Time + Tags map[string]interface{} +} + +func (n testSpan) Equal(os Span) bool { + other, ok := os.(testSpan) + if !ok { + return false + } + if n.spanContext != other.spanContext { + return false + } + if n.OperationName != other.OperationName { + return false + } + if !n.StartTime.Equal(other.StartTime) { + return false + } + if len(n.Tags) != len(other.Tags) { + return false + } + + for k, v := range n.Tags { + if ov, ok := other.Tags[k]; !ok || ov != v { + return false + } + } + + return true } // testSpan: @@ -57,8 +87,11 @@ func (n testTracer) startSpanWithOptions(name string, opts StartSpanOptions) Spa if len(opts.References) > 0 { fakeID = opts.References[0].ReferencedContext.(testSpanContext).FakeID } + return testSpan{ OperationName: name, + StartTime: opts.StartTime, + Tags: opts.Tags, spanContext: testSpanContext{ HasParent: len(opts.References) > 0, FakeID: fakeID,