Skip to content

hotfix(cli): correctly report telemetry even when transport replaced #7670

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 3 commits into from
May 31, 2023
Merged
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
Use functions
  • Loading branch information
ammario committed May 24, 2023
commit 796cd2037c4bdcc0e0f483faf03b037ccabef723
58 changes: 31 additions & 27 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ type RootCmd struct {
noFeatureWarning bool
}

func telemetryInvocation(i *clibase.Invocation) telemetry.CLIInvocation {
func addTelemetryHeader(client *codersdk.Client, inv *clibase.Invocation) {
var topts []telemetry.CLIOption
for _, opt := range i.Command.FullOptions() {
for _, opt := range inv.Command.FullOptions() {
if opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault {
continue
}
Expand All @@ -440,11 +440,29 @@ func telemetryInvocation(i *clibase.Invocation) telemetry.CLIInvocation {
ValueSource: string(opt.ValueSource),
})
}
return telemetry.CLIInvocation{
Command: i.Command.FullName(),
ti := telemetry.CLIInvocation{
Command: inv.Command.FullName(),
Options: topts,
InvokedAt: time.Now(),
}

byt, err := json.Marshal(ti)
if err != nil {
// Should be impossible
panic(err)
}

// Per https://stackoverflow.com/questions/686217/maximum-on-http-header-values,
// we don't want to send headers that are too long.
s := base64.StdEncoding.EncodeToString(byt)
if len(s) > 4096 {
return
}

client.ExtraHeaders.Set(
codersdk.CLITelemetryHeader,
s,
)
}

// InitClient sets client to a new client.
Expand All @@ -457,7 +475,7 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
panic("root is nil")
}
return func(next clibase.HandlerFunc) clibase.HandlerFunc {
return func(i *clibase.Invocation) error {
return func(inv *clibase.Invocation) error {
conf := r.createConfig()
var err error
if r.clientURL == nil || r.clientURL.String() == "" {
Expand Down Expand Up @@ -493,21 +511,7 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
return err
}

// Add telemetry headers:
telemInv := telemetryInvocation(i)
byt, err := json.Marshal(telemInv)
if err != nil {
// Should be impossible
panic(err)
}
if s := base64.StdEncoding.EncodeToString(byt); len(s) < 4096 {
// Per https://stackoverflow.com/questions/686217/maximum-on-http-header-values,
// we don't want to send headers that are too long.
client.ExtraHeaders.Set(
codersdk.CLITelemetryHeader,
s,
)
}
addTelemetryHeader(client, inv)

client.SetSessionToken(r.token)

Expand All @@ -522,31 +526,31 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
warningErr = make(chan error)
)
go func() {
versionErr <- r.checkVersions(i, client)
versionErr <- r.checkVersions(inv, client)
close(versionErr)
}()

go func() {
warningErr <- r.checkWarnings(i, client)
warningErr <- r.checkWarnings(inv, client)
close(warningErr)
}()

if err = <-versionErr; err != nil {
// Just log the error here. We never want to fail a command
// due to a pre-run.
_, _ = fmt.Fprintf(i.Stderr,
_, _ = fmt.Fprintf(inv.Stderr,
cliui.Styles.Warn.Render("check versions error: %s"), err)
_, _ = fmt.Fprintln(i.Stderr)
_, _ = fmt.Fprintln(inv.Stderr)
}

if err = <-warningErr; err != nil {
// Same as above
_, _ = fmt.Fprintf(i.Stderr,
_, _ = fmt.Fprintf(inv.Stderr,
cliui.Styles.Warn.Render("check entitlement warnings error: %s"), err)
_, _ = fmt.Fprintln(i.Stderr)
_, _ = fmt.Fprintln(inv.Stderr)
}

return next(i)
return next(inv)
}
}
}
Expand Down