Skip to content

chore: deduplicate CLI telemetry reports #7669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2023
Merged
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
18 changes: 14 additions & 4 deletions coderd/httpmw/clitelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"golang.org/x/exp/maps"
"tailscale.com/tstime/rate"

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

// We send telemetry at most once per minute.
limiter = rate.NewLimiter(rate.Every(time.Minute), 1)
queue []telemetry.CLIInvocation

// We map by timestamp to deduplicate invocations, since one invocation
// will send multiple requests, each with a duplicate header. It's still
// possible for duplicates to reach the telemetry service since requests
// can get processed by different coderds, but our analysis tools
// will deduplicate by timestamp as well.
//
// This approach just helps us reduce storage and ingest fees, and doesn't
// change the correctness.
queue = make(map[string]telemetry.CLIInvocation)
)

log = log.Named("cli-telemetry")
Expand Down Expand Up @@ -62,18 +72,18 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
mu.Lock()
defer mu.Unlock()

queue = append(queue, inv)
queue[inv.InvokedAt.String()] = inv
if !limiter.Allow() && len(queue) < 1024 {
return
}
rep.Report(&telemetry.Snapshot{
CLIInvocations: queue,
CLIInvocations: maps.Values(queue),
})
log.Debug(
r.Context(),
"report sent", slog.F("count", len(queue)),
)
queue = queue[:0]
maps.Clear(queue)
}()
})
}
Expand Down