Skip to content

Commit e72bbcf

Browse files
committed
chore: deduplicate CLI telemetry reports
1 parent ec117e8 commit e72bbcf

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

coderd/httpmw/clitelemetry.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"time"
99

10+
"golang.org/x/exp/maps"
1011
"tailscale.com/tstime/rate"
1112

1213
"cdr.dev/slog"
@@ -20,7 +21,12 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
2021

2122
// We send telemetry at most once per minute.
2223
limiter = rate.NewLimiter(rate.Every(time.Minute), 1)
23-
queue []telemetry.CLIInvocation
24+
// We map by timestamp to deduplicate invocations, since one invocation
25+
// will send multiple requests, each with a duplicate header. It's still
26+
// possible for duplicates to reach the telemetry server since requests
27+
// can get processed by different servers, but our analysis server
28+
// can deduplicate them as well.
29+
queue = make(map[string]telemetry.CLIInvocation)
2430
)
2531

2632
log = log.Named("cli-telemetry")
@@ -62,18 +68,18 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
6268
mu.Lock()
6369
defer mu.Unlock()
6470

65-
queue = append(queue, inv)
71+
queue[inv.InvokedAt.String()] = inv
6672
if !limiter.Allow() && len(queue) < 1024 {
6773
return
6874
}
6975
rep.Report(&telemetry.Snapshot{
70-
CLIInvocations: queue,
76+
CLIInvocations: maps.Values(queue),
7177
})
7278
log.Debug(
7379
r.Context(),
7480
"report sent", slog.F("count", len(queue)),
7581
)
76-
queue = queue[:0]
82+
maps.Clear(queue)
7783
}()
7884
})
7985
}

0 commit comments

Comments
 (0)