Skip to content
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

telemetry: start tracking flags usage #6168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pkg/odo/genericclioptions/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
klog.V(4).Infof("WARNING: debug telemetry, if enabled, will be logged in %s", debugTelemetry)
}

scontext.SetFlags(cmd.Context(), cmd.Flags())
// set value for telemetry status in context so that we do not need to call IsTelemetryEnabled every time to check its status
scontext.SetTelemetryStatus(cmd.Context(), segment.IsTelemetryEnabled(cfg))

Expand Down
19 changes: 19 additions & 0 deletions pkg/segment/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/spf13/pflag"

dfutil "github.com/devfile/library/pkg/util"

Expand All @@ -22,6 +24,7 @@ const (
ProjectType = "projectType"
NOTFOUND = "not-found"
InteractiveMode = "interactive"
Flags = "flags"
)

type contextKey struct{}
Expand Down Expand Up @@ -111,6 +114,22 @@ func SetInteractive(ctx context.Context, interactive bool) {
setContextProperty(ctx, InteractiveMode, interactive)
}

// SetFlags sets flags property for telemetry to record what flags were used
func SetFlags(ctx context.Context, flags *pflag.FlagSet) {
var changedFlags []string
flags.VisitAll(func(f *pflag.Flag) {
if f.Changed {
if f.Name == "logtostderr" {
// skip "logtostderr" flag, for some reason it is showing as changed even when it is not
return
}
changedFlags = append(changedFlags, f.Name)
}
})
// the flags can't have spaces, so the output is space separated list of the flag names
setContextProperty(ctx, Flags, strings.Join(changedFlags, " "))
rm3l marked this conversation as resolved.
Show resolved Hide resolved
}

// GetTelemetryStatus gets the telemetry status that is set before a command is run
func GetTelemetryStatus(ctx context.Context) bool {
isEnabled, ok := GetContextProperties(ctx)[TelemetryStatus]
Expand Down
78 changes: 78 additions & 0 deletions pkg/segment/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"reflect"
"testing"

"github.com/spf13/pflag"
)

func TestGetContextProperties(t *testing.T) {
Expand Down Expand Up @@ -123,3 +125,79 @@ func TestSetTelemetryStatus(t *testing.T) {
// }
// fakeClient.SetDiscoveryInterface(fd)
//}

func TestSetFlags(t *testing.T) {
type args struct {
ctx context.Context
flags *pflag.FlagSet
}
tests := []struct {
name string
args args
want string
}{
{
name: "no flags",
args: args{
ctx: NewContext(context.Background()),
flags: &pflag.FlagSet{},
},
want: "",
},
{
name: "one changed flag",
args: args{
ctx: NewContext(context.Background()),
flags: func() *pflag.FlagSet {
f := &pflag.FlagSet{}
f.String("flag1", "", "")
//nolint:errcheck
f.Set("flag1", "value1")
return f
}(),
},
want: "flag1",
},
{
name: "one changed flag, one unchanged flag",
args: args{
ctx: NewContext(context.Background()),
flags: func() *pflag.FlagSet {
f := &pflag.FlagSet{}
f.String("flag1", "", "")
f.String("flag2", "", "")
//nolint:errcheck
f.Set("flag2", "value1")
return f
}(),
},
want: "flag2",
},
{
name: "two changed flags",
args: args{
ctx: NewContext(context.Background()),
flags: func() *pflag.FlagSet {
f := &pflag.FlagSet{}
f.String("flag1", "", "")
f.String("flag2", "", "")
//nolint:errcheck
f.Set("flag1", "value1")
//nolint:errcheck
f.Set("flag2", "value1")
return f
}(),
},
want: "flag1 flag2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetFlags(tt.args.ctx, tt.args.flags)
got := GetContextProperties(tt.args.ctx)[Flags]
if got != tt.want {
t.Errorf("SetFlags() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions tests/integration/cmd_devfile_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ ComponentSettings:
Expect(td.Properties.CmdProperties[segment.ComponentType]).To(ContainSubstring("nodejs"))
Expect(td.Properties.CmdProperties[segment.Language]).To(ContainSubstring("javascript"))
Expect(td.Properties.CmdProperties[segment.ProjectType]).To(ContainSubstring("nodejs"))
Expect(td.Properties.CmdProperties[segment.Flags]).To(BeEmpty())

})
})

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/cmd_devfile_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ var _ = Describe("odo devfile init command tests", func() {
Expect(td.Properties.CmdProperties[segment.ComponentType]).To(ContainSubstring("Go"))
Expect(td.Properties.CmdProperties[segment.Language]).To(ContainSubstring("Go"))
Expect(td.Properties.CmdProperties[segment.ProjectType]).To(ContainSubstring("Go"))
Expect(td.Properties.CmdProperties[segment.Flags]).To(ContainSubstring("devfile name"))

})
})
})