diff --git a/ddtrace/tracer/span.go b/ddtrace/tracer/span.go index db37c2e04f..3d6c7de98e 100644 --- a/ddtrace/tracer/span.go +++ b/ddtrace/tracer/span.go @@ -342,6 +342,9 @@ func (s *span) finish(finishTime int64) { if s.Duration == 0 { s.Duration = finishTime - s.Start } + if s.Duration < 0 { + s.Duration = 0 + } s.finished = true keep := true diff --git a/ddtrace/tracer/span_test.go b/ddtrace/tracer/span_test.go index f8d509f21a..accbdbe9e9 100644 --- a/ddtrace/tracer/span_test.go +++ b/ddtrace/tracer/span_test.go @@ -163,6 +163,16 @@ func TestSpanFinishWithTime(t *testing.T) { assert.Equal(duration, span.Duration) } +func TestSpanFinishWithNegativeDuration(t *testing.T) { + assert := assert.New(t) + startTime := time.Now() + finishTime := startTime.Add(-10 * time.Second) + span := newBasicSpan("web.request") + span.Start = startTime.UnixNano() + span.Finish(FinishTime(finishTime)) + assert.Equal(int64(0), span.Duration) +} + func TestSpanFinishWithError(t *testing.T) { assert := assert.New(t) diff --git a/profiler/profiler.go b/profiler/profiler.go index 0f3c65a88a..cae7d0161a 100644 --- a/profiler/profiler.go +++ b/profiler/profiler.go @@ -15,10 +15,10 @@ import ( "sync" "time" - pprofile "github.com/google/pprof/profile" - "gopkg.in/DataDog/dd-trace-go.v1/internal" "gopkg.in/DataDog/dd-trace-go.v1/internal/log" + + pprofile "github.com/google/pprof/profile" ) // outChannelSize specifies the size of the profile output channel.