Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send context for non-sampled transactions #593

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Add support for extracting UUID-like container IDs (#577)
- Introduce transaction/span breakdown metrics (#564)
- Optimised HTTP request body capture (#592)
- 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