Skip to content

Commit 7095e2a

Browse files
williammartinCopilot
authored andcommitted
Fix SetSampleRate not updating sample_rate dimension
The sample_rate common dimension was set once at service creation time and never updated when SetSampleRate was called later. This caused commands like 'gh skill publish' that override the sample rate via PersistentPreRunE to report the wrong sample_rate in telemetry events (e.g. 1 instead of 100). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6d98648 commit 7095e2a

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

internal/telemetry/telemetry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"runtime"
1616
"slices"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"time"
@@ -283,6 +284,7 @@ func (s *service) SetSampleRate(rate int) {
283284
defer s.mu.Unlock()
284285

285286
s.sampleRate = rate
287+
s.commonDimensions["sample_rate"] = strconv.Itoa(rate)
286288
}
287289

288290
func (s *service) Flush() {

internal/telemetry/telemetry_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,24 @@ func TestServiceSampling(t *testing.T) {
579579
assert.False(t, called, "flusher should not be called after SetSampleRate reduced the rate")
580580
})
581581

582+
t.Run("SetSampleRate updates sample_rate dimension", func(t *testing.T) {
583+
t.Cleanup(stubDeviceID("test-device"))
584+
585+
var captured SendTelemetryPayload
586+
svc := newService(func(p SendTelemetryPayload) { captured = p }, ghtelemetry.Dimensions{
587+
"sample_rate": "1",
588+
})
589+
svc.sampleRate = 1
590+
svc.sampleBucket = 0
591+
592+
svc.SetSampleRate(100)
593+
svc.Record(ghtelemetry.Event{Type: "test"})
594+
svc.Flush()
595+
596+
require.Len(t, captured.Events, 1)
597+
assert.Equal(t, "100", captured.Events[0].Dimensions["sample_rate"])
598+
})
599+
582600
t.Run("WithSampleRate option sets rate on construction", func(t *testing.T) {
583601
t.Cleanup(stubDeviceID("test-device"))
584602

0 commit comments

Comments
 (0)