Skip to content

Commit

Permalink
fix: Do not double sample transactions (#328)
Browse files Browse the repository at this point in the history
* fix: Do not double sample transactions

* docs: Add note about transaction x event sampling
  • Loading branch information
rhcarvalho committed Feb 17, 2021
1 parent fb7df73 commit 7d0542d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
options.SampleRate = 1.0
}

if !sample(options.SampleRate) {
// Transactions are sampled by options.TracesSampleRate or
// options.TracesSampler when they are started. All other events
// (errors, messages) are sampled here.
if event.Type != transactionType && !sample(options.SampleRate) {
Logger.Println("Event dropped due to SampleRate hit.")
return nil
}
Expand Down
28 changes: 28 additions & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -364,3 +366,29 @@ func TestSpanFromContext(t *testing.T) {
// RecorderLen: 1,
// }.Check(t, child)
}

func TestDoubleSampling(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
SampleRate: math.SmallestNonzeroFloat64,
TracesSampleRate: 1.0,
Transport: transport,
})
span := StartSpan(ctx, "op", TransactionName("name"))

// CaptureException should not send any event because of SampleRate.
GetHubFromContext(ctx).CaptureException(errors.New("ignored"))
if got := len(transport.Events()); got != 0 {
t.Fatalf("got %d events, want 0", got)
}

// Finish should send one transaction event, always sampled via
// TracesSampleRate.
span.Finish()
if got := len(transport.Events()); got != 1 {
t.Fatalf("got %d events, want 1", got)
}
if got := transport.Events()[0].Type; got != transactionType {
t.Fatalf("got %v event, want %v", got, transactionType)
}
}

0 comments on commit 7d0542d

Please sign in to comment.