Skip to content

Commit

Permalink
Merge pull request #640 from axw/max-spans-zero
Browse files Browse the repository at this point in the history
ELASTIC_APM_TRANSACTION_MAX_SPANS=0 disables spans
  • Loading branch information
axw committed Sep 26, 2019
2 parents 3e10f18 + 9b6b257 commit f19dd2c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- module/apmgoredis: add Client.RedisClient (#613)
- Introduce apm.TraceFormatter, for formatting trace IDs (#635)
- Report error cause(s), add support for errors.Unwrap (#638)
- Setting `ELASTIC_APM_TRANSACTION_MAX_SPANS` to 0 now disables all spans (#640)

## [v1.5.0](https://github.com/elastic/apm-agent-go/releases/tag/v1.5.0)

Expand Down
2 changes: 1 addition & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (tx *Transaction) StartSpanOptions(name, spanType string, opts SpanOptions)
defer tx.TransactionData.mu.Unlock()
if !span.traceContext.Options.Recorded() {
span.tracer = nil // span is dropped
} else if tx.maxSpans > 0 && tx.spansCreated >= tx.maxSpans {
} else if tx.maxSpans >= 0 && tx.spansCreated >= tx.maxSpans {
span.tracer = nil // span is dropped
tx.spansDropped++
} else {
Expand Down
6 changes: 4 additions & 2 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,10 @@ func (t *Tracer) SetSampler(s Sampler) {
}

// SetMaxSpans sets the maximum number of spans that will be added
// to a transaction before dropping spans. If set to a non-positive
// value, the number of spans is unlimited.
// to a transaction before dropping spans.
//
// Passing in zero will disable all spans, while negative values will
// permit an unlimited number of spans.
func (t *Tracer) SetMaxSpans(n int) {
t.maxSpansMu.Lock()
t.maxSpans = n
Expand Down
49 changes: 27 additions & 22 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,34 @@ func TestTracerFlushEmpty(t *testing.T) {
}

func TestTracerMaxSpans(t *testing.T) {
tracer, r := transporttest.NewRecorderTracer()
defer tracer.Close()

tracer.SetMaxSpans(2)
tx := tracer.StartTransaction("name", "type")
// SetMaxSpans only affects transactions started
// after the call.
tracer.SetMaxSpans(99)

s0 := tx.StartSpan("name", "type", nil)
s1 := tx.StartSpan("name", "type", nil)
s2 := tx.StartSpan("name", "type", nil)
tx.End()

assert.False(t, s0.Dropped())
assert.False(t, s1.Dropped())
assert.True(t, s2.Dropped())
s0.End()
s1.End()
s2.End()
test := func(n int) {
t.Run(fmt.Sprint(n), func(t *testing.T) {
tracer, r := transporttest.NewRecorderTracer()
defer tracer.Close()

tracer.SetMaxSpans(n)
tx := tracer.StartTransaction("name", "type")
defer tx.End()

// SetMaxSpans only affects transactions started
// after the call.
tracer.SetMaxSpans(99)

for i := 0; i < n; i++ {
span := tx.StartSpan("name", "type", nil)
assert.False(t, span.Dropped())
span.End()
}
span := tx.StartSpan("name", "type", nil)
assert.True(t, span.Dropped())
span.End()

tracer.Flush(nil)
assert.Len(t, r.Payloads().Spans, 2)
tracer.Flush(nil)
assert.Len(t, r.Payloads().Spans, n)
})
}
test(0)
test(23)
}

func TestTracerErrors(t *testing.T) {
Expand Down

0 comments on commit f19dd2c

Please sign in to comment.