Skip to content

Commit adba421

Browse files
authored
refactor(coderd/telemetry): move CLI telemetry to cli/telemetry (#9517)
This change removes an indirect import of `coderd/database` from the slim binary. No size change (yet). Ref: #9380
1 parent b240799 commit adba421

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

cli/root.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
"github.com/coder/coder/v2/cli/cliui"
3737
"github.com/coder/coder/v2/cli/config"
3838
"github.com/coder/coder/v2/cli/gitauth"
39-
"github.com/coder/coder/v2/coderd/telemetry"
39+
"github.com/coder/coder/v2/cli/telemetry"
4040
"github.com/coder/coder/v2/codersdk"
4141
"github.com/coder/coder/v2/codersdk/agentsdk"
4242
)
@@ -467,17 +467,17 @@ func addTelemetryHeader(client *codersdk.Client, inv *clibase.Invocation) {
467467
client.HTTPClient.Transport = transport
468468
}
469469

470-
var topts []telemetry.CLIOption
470+
var topts []telemetry.Option
471471
for _, opt := range inv.Command.FullOptions() {
472472
if opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault {
473473
continue
474474
}
475-
topts = append(topts, telemetry.CLIOption{
475+
topts = append(topts, telemetry.Option{
476476
Name: opt.Name,
477477
ValueSource: string(opt.ValueSource),
478478
})
479479
}
480-
ti := telemetry.CLIInvocation{
480+
ti := telemetry.Invocation{
481481
Command: inv.Command.FullName(),
482482
Options: topts,
483483
InvokedAt: time.Now(),

cli/telemetry/telemetry.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package telemetry
2+
3+
import "time"
4+
5+
type Option struct {
6+
Name string `json:"name"`
7+
ValueSource string `json:"value_source"`
8+
}
9+
10+
type Invocation struct {
11+
Command string `json:"command"`
12+
Options []Option `json:"options"`
13+
// InvokedAt is provided for deduplication purposes.
14+
InvokedAt time.Time `json:"invoked_at"`
15+
}

coderd/httpmw/clitelemetry.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"tailscale.com/tstime/rate"
1212

1313
"cdr.dev/slog"
14+
clitelemetry "github.com/coder/coder/v2/cli/telemetry"
1415
"github.com/coder/coder/v2/coderd/telemetry"
1516
"github.com/coder/coder/v2/codersdk"
1617
)
@@ -30,7 +31,7 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
3031
//
3132
// This approach just helps us reduce storage and ingest fees, and doesn't
3233
// change the correctness.
33-
queue = make(map[string]telemetry.CLIInvocation)
34+
queue = make(map[string]clitelemetry.Invocation)
3435
)
3536

3637
log = log.Named("cli-telemetry")
@@ -55,7 +56,7 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
5556
return
5657
}
5758

58-
var inv telemetry.CLIInvocation
59+
var inv clitelemetry.Invocation
5960
err = json.Unmarshal(byt, &inv)
6061
if err != nil {
6162
log.Error(

coderd/telemetry/telemetry.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"cdr.dev/slog"
2525
"github.com/coder/coder/v2/buildinfo"
26+
clitelemetry "github.com/coder/coder/v2/cli/telemetry"
2627
"github.com/coder/coder/v2/coderd/database"
2728
"github.com/coder/coder/v2/coderd/database/dbtime"
2829
)
@@ -714,7 +715,7 @@ type Snapshot struct {
714715
WorkspaceResources []WorkspaceResource `json:"workspace_resources"`
715716
WorkspaceResourceMetadata []WorkspaceResourceMetadata `json:"workspace_resource_metadata"`
716717
WorkspaceProxies []WorkspaceProxy `json:"workspace_proxies"`
717-
CLIInvocations []CLIInvocation `json:"cli_invocations"`
718+
CLIInvocations []clitelemetry.Invocation `json:"cli_invocations"`
718719
}
719720

720721
// Deployment contains information about the host running Coder.
@@ -890,18 +891,6 @@ type License struct {
890891
UUID uuid.UUID `json:"uuid"`
891892
}
892893

893-
type CLIOption struct {
894-
Name string `json:"name"`
895-
ValueSource string `json:"value_source"`
896-
}
897-
898-
type CLIInvocation struct {
899-
Command string `json:"command"`
900-
Options []CLIOption `json:"options"`
901-
// InvokedAt is provided for deduplication purposes.
902-
InvokedAt time.Time `json:"invoked_at"`
903-
}
904-
905894
type WorkspaceProxy struct {
906895
ID uuid.UUID `json:"id"`
907896
Name string `json:"name"`

0 commit comments

Comments
 (0)