Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Pass options along when creating a span with StartSpanFromContext (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwik authored and yurishkuro committed Sep 23, 2016
1 parent ca4c9c0 commit 449a42d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
6 changes: 3 additions & 3 deletions gocontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
28 changes: 26 additions & 2 deletions gocontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package opentracing

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
33 changes: 33 additions & 0 deletions testtracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package opentracing
import (
"strconv"
"strings"
"time"
)

const testHTTPHeaderPrefix = "testprefix-"
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 449a42d

Please sign in to comment.