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
This commit is contained in:
Mathias Fredriksson 2023-09-04 21:42:45 +03:00 committed by GitHub
parent b240799f47
commit adba421524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 19 deletions

View File

@ -36,7 +36,7 @@ import (
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/cli/config"
"github.com/coder/coder/v2/cli/gitauth"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/cli/telemetry"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
)
@ -467,17 +467,17 @@ func addTelemetryHeader(client *codersdk.Client, inv *clibase.Invocation) {
client.HTTPClient.Transport = transport
}
var topts []telemetry.CLIOption
var topts []telemetry.Option
for _, opt := range inv.Command.FullOptions() {
if opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault {
continue
}
topts = append(topts, telemetry.CLIOption{
topts = append(topts, telemetry.Option{
Name: opt.Name,
ValueSource: string(opt.ValueSource),
})
}
ti := telemetry.CLIInvocation{
ti := telemetry.Invocation{
Command: inv.Command.FullName(),
Options: topts,
InvokedAt: time.Now(),

View File

@ -0,0 +1,15 @@
package telemetry
import "time"
type Option struct {
Name string `json:"name"`
ValueSource string `json:"value_source"`
}
type Invocation struct {
Command string `json:"command"`
Options []Option `json:"options"`
// InvokedAt is provided for deduplication purposes.
InvokedAt time.Time `json:"invoked_at"`
}

View File

@ -11,6 +11,7 @@ import (
"tailscale.com/tstime/rate"
"cdr.dev/slog"
clitelemetry "github.com/coder/coder/v2/cli/telemetry"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/codersdk"
)
@ -30,7 +31,7 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
//
// This approach just helps us reduce storage and ingest fees, and doesn't
// change the correctness.
queue = make(map[string]telemetry.CLIInvocation)
queue = make(map[string]clitelemetry.Invocation)
)
log = log.Named("cli-telemetry")
@ -55,7 +56,7 @@ func ReportCLITelemetry(log slog.Logger, rep telemetry.Reporter) func(http.Handl
return
}
var inv telemetry.CLIInvocation
var inv clitelemetry.Invocation
err = json.Unmarshal(byt, &inv)
if err != nil {
log.Error(

View File

@ -23,6 +23,7 @@ import (
"cdr.dev/slog"
"github.com/coder/coder/v2/buildinfo"
clitelemetry "github.com/coder/coder/v2/cli/telemetry"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
)
@ -714,7 +715,7 @@ type Snapshot struct {
WorkspaceResources []WorkspaceResource `json:"workspace_resources"`
WorkspaceResourceMetadata []WorkspaceResourceMetadata `json:"workspace_resource_metadata"`
WorkspaceProxies []WorkspaceProxy `json:"workspace_proxies"`
CLIInvocations []CLIInvocation `json:"cli_invocations"`
CLIInvocations []clitelemetry.Invocation `json:"cli_invocations"`
}
// Deployment contains information about the host running Coder.
@ -890,18 +891,6 @@ type License struct {
UUID uuid.UUID `json:"uuid"`
}
type CLIOption struct {
Name string `json:"name"`
ValueSource string `json:"value_source"`
}
type CLIInvocation struct {
Command string `json:"command"`
Options []CLIOption `json:"options"`
// InvokedAt is provided for deduplication purposes.
InvokedAt time.Time `json:"invoked_at"`
}
type WorkspaceProxy struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`