Skip to content

Commit

Permalink
[opentracing] start time is evaluated in options
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuele Palazzetti committed Dec 4, 2017
1 parent 06e85ee commit a0ba9da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions opentracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (t *Tracer) StartSpan(operationName string, options ...ot.StartSpanOption)

func (t *Tracer) startSpanWithOptions(operationName string, options ot.StartSpanOptions) ot.Span {
if options.StartTime.IsZero() {
// TODO: we should set this value
options.StartTime = time.Now().UTC()
}

Expand Down Expand Up @@ -57,6 +56,8 @@ func (t *Tracer) startSpanWithOptions(operationName string, options ot.StartSpan
span = t.impl.NewChildSpan(operationName, parent.Span)
}

// create an OpenTracing compatible Span; the SpanContext has a
// back-reference that is used for parent-child hierarchy
otSpan := &Span{
Span: span,
context: SpanContext{
Expand All @@ -66,9 +67,11 @@ func (t *Tracer) startSpanWithOptions(operationName string, options ot.StartSpan
sampled: span.Sampled,
},
}

otSpan.context.span = otSpan

// set start time
otSpan.Span.Start = options.StartTime.UnixNano()

if parent != nil {
// propagate baggage items
if l := len(parent.context.baggage); l > 0 {
Expand Down
14 changes: 14 additions & 0 deletions opentracing/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opentracing

import (
"testing"
"time"

opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -89,3 +90,16 @@ func TestTracerSpanTags(t *testing.T) {

assert.Equal("value", span.Span.Meta["key"])
}

func TestTracerSpanStartTime(t *testing.T) {
assert := assert.New(t)

config := NewConfiguration()
tracer, _, _ := NewTracer(config)

startTime := time.Now().Add(-10 * time.Second)
span, ok := tracer.StartSpan("web.request", opentracing.StartTime(startTime)).(*Span)
assert.True(ok)

assert.Equal(startTime.UnixNano(), span.Span.Start)
}

0 comments on commit a0ba9da

Please sign in to comment.