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>
This commit is contained in:
William Martin 2026-04-22 13:48:51 +02:00 committed by Babak K. Shandiz
parent 6d986480b8
commit 7095e2a4fc
No known key found for this signature in database
GPG key ID: 9472CAEFF56C742E
2 changed files with 20 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
@ -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() {

View file

@ -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"))