MCPcopy Index your code
hub / github.com/coder/coder / ReportCLITelemetry

Function ReportCLITelemetry

coderd/httpmw/clitelemetry.go:19–91  ·  view source on GitHub ↗
(log slog.Logger, rep telemetry.Reporter)

Source from the content-addressed store, hash-verified

17)
18
19func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handler) http.Handler {
20 var (
21 mu sync.Mutex
22
23 // We send telemetry at most once per minute.
24 limiter = rate.NewLimiter(rate.Every(time.Minute), 1)
25
26 // We map by timestamp to deduplicate invocations, since one invocation
27 // will send multiple requests, each with a duplicate header. It's still
28 // possible for duplicates to reach the telemetry service since requests
29 // can get processed by different coderds, but our analysis tools
30 // will deduplicate by timestamp as well.
31 //
32 // This approach just helps us reduce storage and ingest fees, and doesn't
33 // change the correctness.
34 queue = make(map[string]clitelemetry.Invocation)
35 )
36
37 log = log.Named("cli-telemetry")
38
39 return func(next http.Handler) http.Handler {
40 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
41 // No matter what, we proceed with the request.
42 defer next.ServeHTTP(rw, r)
43
44 payload := r.Header.Get(codersdk.CLITelemetryHeader)
45 if payload == "" {
46 return
47 }
48
49 byt, err := base64.StdEncoding.DecodeString(payload)
50 if err != nil {
51 log.Error(
52 r.Context(),
53 "base64 decode",
54 slog.Error(err),
55 )
56 return
57 }
58
59 var inv clitelemetry.Invocation
60 err = json.Unmarshal(byt, &inv)
61 if err != nil {
62 log.Error(
63 r.Context(),
64 "unmarshal header",
65 slog.Error(err),
66 )
67 return
68 }
69
70 // We do expensive work in a goroutine so we don't block the
71 // request.
72 go func() {
73 mu.Lock()
74 defer mu.Unlock()
75
76 queue[inv.InvokedAt.String()] = inv

Callers

nothing calls this directly

Calls 12

NamedMethod · 0.80
DecodeStringMethod · 0.80
GetMethod · 0.65
ContextMethod · 0.65
ReportMethod · 0.65
ServeHTTPMethod · 0.45
ErrorMethod · 0.45
UnmarshalMethod · 0.45
LockMethod · 0.45
UnlockMethod · 0.45
StringMethod · 0.45
ClearMethod · 0.45

Tested by

no test coverage detected