Skip to content

Commit

Permalink
Don't send context for non-sampled transactions
Browse files Browse the repository at this point in the history
Fix transaction encoding to drop any accumulated
context for non-sampled transactions.

We've been relying on instrumentation to not
set context for non-sampled transactions. If
some instrumentation (e.g. custom code) sets
context, it still shouldn't be sent to the
server for non-sampled transactions.
  • Loading branch information
axw committed Jul 22, 2019
1 parent e92cea0 commit 337de7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add Context.SetCustom (#581)
- Add support for extracting UUID-like container IDs (#577)
- Introduce transaction/span breakdown metrics (#564)
- Fixed transaction encoding to drop tags (and other context) for non-sampled transactions (#593)

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

Expand Down
7 changes: 5 additions & 2 deletions modelwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (w *modelWriter) writeMetrics(m *Metrics) {
func (w *modelWriter) buildModelTransaction(out *model.Transaction, tx *Transaction, td *TransactionData) {
out.ID = model.SpanID(tx.traceContext.Span)
out.TraceID = model.TraceID(tx.traceContext.Trace)
if !tx.traceContext.Options.Recorded() {
sampled := tx.traceContext.Options.Recorded()
if !sampled {
out.Sampled = &notSampled
}

Expand All @@ -117,8 +118,10 @@ func (w *modelWriter) buildModelTransaction(out *model.Transaction, tx *Transact
out.Duration = td.Duration.Seconds() * 1000
out.SpanCount.Started = td.spansCreated
out.SpanCount.Dropped = td.spansDropped
if sampled {
out.Context = td.Context.build()
}

out.Context = td.Context.build()
if len(w.cfg.sanitizedFieldNames) != 0 && out.Context != nil {
if out.Context.Request != nil {
sanitizeRequest(out.Context.Request, w.cfg.sanitizedFieldNames)
Expand Down
16 changes: 16 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/stretchr/testify/require"

"go.elastic.co/apm"
"go.elastic.co/apm/apmtest"
"go.elastic.co/apm/model"
"go.elastic.co/apm/transport"
"go.elastic.co/apm/transport/transporttest"
Expand Down Expand Up @@ -140,6 +141,21 @@ func TestTransactionEnsureParent(t *testing.T) {
assert.Equal(t, model.SpanID(parentSpan), payloads.Transactions[0].ParentID)
}

func TestTransactionContextNotSampled(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
tracer.SetSampler(samplerFunc(func(apm.TraceContext) bool { return false }))

tx := tracer.StartTransaction("name", "type")
tx.Context.SetTag("foo", "bar")
tx.End()
tracer.Flush(nil)

payloads := tracer.Payloads()
require.Len(t, payloads.Transactions, 1)
assert.Nil(t, payloads.Transactions[0].Context)
}

func BenchmarkTransaction(b *testing.B) {
tracer, err := apm.NewTracer("service", "")
require.NoError(b, err)
Expand Down

0 comments on commit 337de7f

Please sign in to comment.