Skip to content

Commit

Permalink
chore: Added events on runner
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Aug 1, 2023
1 parent 051386b commit 4837c99
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/runner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/spf13/cobra"

"github.com/CircleCI-Public/circleci-cli/api/runner"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/cmd/validator"
"github.com/CircleCI-Public/circleci-cli/telemetry"
)

func newRunnerInstanceCommand(o *runnerOpts, preRunE validator.Validator) *cobra.Command {
Expand All @@ -26,6 +28,14 @@ func newRunnerInstanceCommand(o *runnerOpts, preRunE validator.Validator) *cobra
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
var err error

telemetryClient := o.createTelemetry()
defer (func() {
_ = telemetryClient.Track(telemetry.CreateRunnerInstanceEvent(create_telemetry.GetCommandInformation(cmd, true), err))
_ = telemetryClient.Close()
})()

runners, err := o.r.GetRunnerInstances(args[0])
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions cmd/runner/resource_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (
"github.com/spf13/cobra"

"github.com/CircleCI-Public/circleci-cli/api/runner"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/cmd/validator"
"github.com/CircleCI-Public/circleci-cli/telemetry"
)

func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra.Command {
cmd := &cobra.Command{
Use: "resource-class",
Short: "Operate on runner resource-classes",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
telemetryClient := o.createTelemetry()
defer telemetryClient.Close()
telemetryClient.Track(telemetry.CreateRunnerResourceClassEvent(create_telemetry.GetCommandInformation(cmd, true)))
},
}

genToken := false
Expand Down
3 changes: 2 additions & 1 deletion cmd/runner/resource_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"gotest.tools/v3/assert/cmp"

"github.com/CircleCI-Public/circleci-cli/api/runner"
"github.com/CircleCI-Public/circleci-cli/telemetry"
)

func Test_ResourceClass(t *testing.T) {
runner := runnerMock{}
cmd := newResourceClassCommand(&runnerOpts{r: &runner}, nil)
cmd := newResourceClassCommand(&runnerOpts{r: &runner, createTelemetry: telemetry.CreateNullClient}, nil)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.SetOut(stdout)
Expand Down
8 changes: 7 additions & 1 deletion cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (

"github.com/CircleCI-Public/circleci-cli/api/rest"
"github.com/CircleCI-Public/circleci-cli/api/runner"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/cmd/validator"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
)

type runnerOpts struct {
r running
r running
createTelemetry func() telemetry.Client
}

func NewCommand(config *settings.Config, preRunE validator.Validator) *cobra.Command {
Expand All @@ -28,6 +31,9 @@ func NewCommand(config *settings.Config, preRunE validator.Validator) *cobra.Com
host = config.Host
}
opts.r = runner.New(rest.NewFromConfig(host, config))
opts.createTelemetry = func() telemetry.Client {
return create_telemetry.CreateTelemetry(config)
}
},
}

Expand Down
56 changes: 56 additions & 0 deletions cmd/runner/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package runner

import (
"bytes"
"testing"

"github.com/CircleCI-Public/circleci-cli/telemetry"
"gotest.tools/v3/assert"
)

type testTelemetryClient struct {
events []telemetry.Event
}

func (c *testTelemetryClient) Track(event telemetry.Event) error {
c.events = append(c.events, event)
return nil
}

func (c *testTelemetryClient) Close() error { return nil }

func Test_RunnerTelemetry(t *testing.T) {
t.Run("resource-class", func(t *testing.T) {
telemetryClient := &testTelemetryClient{make([]telemetry.Event, 0)}
runner := runnerMock{}
cmd := newResourceClassCommand(&runnerOpts{r: &runner, createTelemetry: func() telemetry.Client { return telemetryClient }}, nil)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.SetOut(stdout)
cmd.SetErr(stderr)

defer runner.reset()
defer stdout.Reset()
defer stderr.Reset()

cmd.SetArgs([]string{
"create",
"my-namespace/my-other-resource-class",
"my-description",
"--generate-token",
})

err := cmd.Execute()
assert.NilError(t, err)

assert.DeepEqual(t, telemetryClient.events, []telemetry.Event{
telemetry.CreateRunnerResourceClassEvent(telemetry.CommandInfo{
Name: "create",
LocalArgs: map[string]string{
"generate-token": "true",
"help": "false",
},
}),
})
})
}
7 changes: 7 additions & 0 deletions cmd/runner/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package runner
import (
"time"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/cmd/validator"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
Expand All @@ -12,6 +14,11 @@ func newTokenCommand(o *runnerOpts, preRunE validator.Validator) *cobra.Command
cmd := &cobra.Command{
Use: "token",
Short: "Operate on runner tokens",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
telemetryClient := o.createTelemetry()
defer telemetryClient.Close()
telemetryClient.Track(telemetry.CreateRunnerResourceClassEvent(create_telemetry.GetCommandInformation(cmd, true)))
},
}

cmd.AddCommand(&cobra.Command{
Expand Down
16 changes: 16 additions & 0 deletions telemetry/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ func CreateOrbEvent(cmdInfo CommandInfo) Event {
func CreatePolicyEvent(cmdInfo CommandInfo) Event {
return createEventFromCommandInfo("policy", cmdInfo)
}

func CreateRunnerInstanceEvent(cmdInfo CommandInfo, err error) Event {
event := createEventFromCommandInfo("runner-instance", cmdInfo)
if err != nil {
event.Properties["error"] = err.Error()
}
return event
}

func CreateRunnerResourceClassEvent(cmdInfo CommandInfo) Event {
return createEventFromCommandInfo("runner-resource-class", cmdInfo)
}

func CreateRunnerToken(cmdInfo CommandInfo) Event {
return createEventFromCommandInfo("runner-resource-class", cmdInfo)
}
4 changes: 4 additions & 0 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func SendTelemetryApproval(user User, approval Approval) error {
// Null client
// Used when telemetry is disabled

func CreateNullClient() Client {
return nullClient{}
}

type nullClient struct{}

func (cli nullClient) Close() error { return nil }
Expand Down

0 comments on commit 4837c99

Please sign in to comment.