Skip to content

Commit

Permalink
ddtrace/tracer: Add normalization of span duration (#973)
Browse files Browse the repository at this point in the history
Duration could be negative, and that can cause the negative sketch to be
non empty, which is not desirable. This makes sure any span with negative
duration is given a duration of zero.
  • Loading branch information
piochelepiotr authored Sep 1, 2021
1 parent 5588d16 commit dd69403
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit dd69403

Please sign in to comment.