Visitar URL original
Fix SetSampleRate not updating sample_rate dimension by williammartin · Pull Request #13259 · cli/cli · GitHub
Skip to content
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
2 changes: 2 additions & 0 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -283,6 +284,7 @@ func (s *service) SetSampleRate(rate int) {
defer s.mu.Unlock()

s.sampleRate = rate
s.commonDimensions["sample_rate"] = strconv.Itoa(rate)
}

func (s *service) Flush() {
Expand Down
18 changes: 18 additions & 0 deletions internal/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,24 @@ func TestServiceSampling(t *testing.T) {
assert.False(t, called, "flusher should not be called after SetSampleRate reduced the rate")
})

t.Run("SetSampleRate updates sample_rate dimension", func(t *testing.T) {
t.Cleanup(stubDeviceID("test-device"))

var captured SendTelemetryPayload
svc := newService(func(p SendTelemetryPayload) { captured = p }, ghtelemetry.Dimensions{
"sample_rate": "1",
})
svc.sampleRate = 1
svc.sampleBucket = 0

svc.SetSampleRate(100)
svc.Record(ghtelemetry.Event{Type: "test"})
svc.Flush()

require.Len(t, captured.Events, 1)
assert.Equal(t, "100", captured.Events[0].Dimensions["sample_rate"])
})

t.Run("WithSampleRate option sets rate on construction", func(t *testing.T) {
t.Cleanup(stubDeviceID("test-device"))

Expand Down
Loading