Skip to content

chore: add CLI invokation telemetry #7589

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 8 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Get to middleware
  • Loading branch information
ammario committed May 16, 2023
commit 0b2d56b0fa658f696b0b30f169519842fc0dd737
4 changes: 2 additions & 2 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ type RootCmd struct {
func telemetryInvocation(i *clibase.Invocation) telemetry.CLIInvocation {
var topts []telemetry.CLIOption
for _, opt := range i.Command.FullOptions() {
if opt.Value.String() == opt.Default {
if opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault {
continue
}
topts = append(topts, telemetry.CLIOption{
Expand Down Expand Up @@ -493,7 +493,7 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
}
err = r.setClient(
client, r.clientURL,
append(r.header, "Coder-CLI-Invokation="+
append(r.header, codersdk.CLITelemetryHeader+"="+
base64.StdEncoding.EncodeToString(byt),
),
)
Expand Down
78 changes: 78 additions & 0 deletions coderd/httpmw/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package httpmw

import (
"encoding/base64"
"encoding/json"
"net/http"
"sync"
"time"

"tailscale.com/tstime/rate"

"cdr.dev/slog"
"github.com/coder/coder/coderd/telemetry"
"github.com/coder/coder/codersdk"
)

func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handler) http.Handler {
var mu sync.Mutex

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

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
defer next.ServeHTTP(rw, r)
payload := r.Header.Get(codersdk.CLITelemetryHeader)

// We do simple checks and processing outside of the goroutine
// to avoid the overhead of an additional goroutine on every
// request.
if payload == "" {
return
}

byt, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
log.Error(
r.Context(),
"base64 decode CLI telemetry header",
slog.F("error", err),
)
return
}

var inv telemetry.CLIInvocation
err = json.Unmarshal(byt, &inv)
if err != nil {
log.Error(
r.Context(),
"unmarshal CLI telemetry header",
slog.Error(err),
)
return
}

go func() {
mu.Lock()
defer mu.Unlock()

queue = append(queue, inv)
if !limiter.Allow() && len(queue) < 1024 {
return
}
rep.Report(&telemetry.Snapshot{
CLIInvocations: queue,
})
log.Debug(
r.Context(),
"reported CLI telemetry", slog.F("count", len(queue)),
)
queue = queue[:0]
}()
})
}
}
6 changes: 3 additions & 3 deletions coderd/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ type Snapshot struct {
WorkspaceBuilds []WorkspaceBuild `json:"workspace_build"`
WorkspaceResources []WorkspaceResource `json:"workspace_resources"`
WorkspaceResourceMetadata []WorkspaceResourceMetadata `json:"workspace_resource_metadata"`
CLIInvokations []CLIInvocation `json:"cli_invocations"`
CLIInvocations []CLIInvocation `json:"cli_invocations"`
}

// Deployment contains information about the host running Coder.
Expand Down Expand Up @@ -881,8 +881,8 @@ type CLIOption struct {
}

type CLIInvocation struct {
Command string `json:"command"`
Options []CLIOption
Command string `json:"command"`
Options []CLIOption `json:"options"`
}

type noopReporter struct{}
Expand Down
10 changes: 10 additions & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ const (
// Only owners can bypass rate limits. This is typically used for scale testing.
// nolint: gosec
BypassRatelimitHeader = "X-Coder-Bypass-Ratelimit"

// Note: the use of X- prefix is deprecated, and we should eventually remove
// it from BypassRatelimitHeader.
//
// See: https://datatracker.ietf.org/doc/html/rfc6648.

// CLIInvokableHeader contains a base64-encoded representation of the CLI
// command that was invoked to produce the request. It is for internal use
// only and should not be relied upon.
CLITelemetryHeader = "Coder-CLI-Telemetry"
)

// loggableMimeTypes is a list of MIME types that are safe to log
Expand Down