Permalink
Browse files

Pass options along when creating a span with StartSpanFromContext (#114)

  • Loading branch information...
1 parent ca4c9c0 commit 449a42da57c0fd743c254a3c7f145fbf67505f06 @gwik gwik committed with yurishkuro Sep 23, 2016
Showing with 62 additions and 5 deletions.
  1. +3 −3 gocontext.go
  2. +26 −2 gocontext_test.go
  3. +33 −0 testtracer_test.go
View
@@ -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)
}
View
@@ -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)
+}
View
@@ -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,

0 comments on commit 449a42d

Please sign in to comment.