Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit df81990

Browse files
committed
Add function to initialize telemetry
This provides a workaround to remove all telemetry info logs during a command's initialization. This is needed by the kube secret command because only the raw YAML output should be printed to stdout.
1 parent 5acfe06 commit df81990

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ var (
7474
)
7575

7676
func preRun(cmd *cobra.Command, args []string) {
77+
telemetry.Init(true)
78+
7779
switch logFormatFlag {
7880
case "json":
7981
printer.SwitchToJSON()

telemetry/telemetry.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func (_ nullClient) Close() error {
5050
return nil
5151
}
5252

53-
func init() {
53+
// Initialize the telemetry client.
54+
// This should be called once at startup either from the root command or from a subcommand that overrides the default PersistentPreRun.
55+
func Init(isLoggingEnabled bool) {
5456
// Opt-out mechanism
5557
disableTelemetry := os.Getenv("AKITA_DISABLE_TELEMETRY")
5658
if disableTelemetry != "" {
@@ -70,31 +72,37 @@ func init() {
7072
segmentKey = defaultSegmentKey
7173
}
7274
if segmentKey == "" {
73-
printer.Infof("Telemetry unavailable; no Segment key configured.\n")
74-
printer.Infof("This is caused by building from source rather than using an official build.\n")
75+
if isLoggingEnabled {
76+
printer.Infof("Telemetry unavailable; no Segment key configured.\n")
77+
printer.Infof("This is caused by building from source rather than using an official build.\n")
78+
}
7579
analyticsClient = nullClient{}
7680
return
7781
}
7882

7983
var err error
80-
analyticsClient, err = analytics.NewClient(analytics.Config{
81-
WriteKey: segmentKey,
82-
SegmentEndpoint: segmentEndpoint,
83-
App: analytics.AppInfo{
84-
Name: "akita-cli",
85-
Version: version.ReleaseVersion().String(),
86-
Build: version.GitVersion(),
87-
Namespace: "",
84+
analyticsClient, err = analytics.NewClient(
85+
analytics.Config{
86+
WriteKey: segmentKey,
87+
SegmentEndpoint: segmentEndpoint,
88+
App: analytics.AppInfo{
89+
Name: "akita-cli",
90+
Version: version.ReleaseVersion().String(),
91+
Build: version.GitVersion(),
92+
Namespace: "",
93+
},
94+
// No output from the Segment library
95+
IsLoggingEnabled: false,
96+
// IsMixpanelEnabled: false, -- irrelevant for us, leaving at default value
97+
BatchSize: 1, // disable batching
8898
},
89-
// No output from the Segment library
90-
IsLoggingEnabled: false,
91-
// IsMixpanelEnabled: false, -- irrelevant for us, leaving at default value
92-
BatchSize: 1, // disable batching
93-
})
99+
)
94100
if err != nil {
95-
printer.Infof("Telemetry unavailable; error setting up Segment client: %v\n", err)
96-
printer.Infof("Akita support will not be able to see any errors you encounter.\n")
97-
printer.Infof("Please send this log message to support@akitasoftware.com.\n")
101+
if isLoggingEnabled {
102+
printer.Infof("Telemetry unavailable; error setting up Segment client: %v\n", err)
103+
printer.Infof("Akita support will not be able to see any errors you encounter.\n")
104+
printer.Infof("Please send this log message to support@akitasoftware.com.\n")
105+
}
98106
analyticsClient = nullClient{}
99107
} else {
100108
analyticsEnabled = true

0 commit comments

Comments
 (0)