forked from encoredev/encore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add telemetry support for usage analysis (encoredev#1222)
Enables support for collecting usage telemetry and adding CLI commands and ENV vars for enabling/disabling telemetry ``` $ encoredev telemetry -h Reports the current telemetry status Usage: encore telemetry [flags] encore telemetry [command] Available Commands: disable Disables telemetry reporting enable Enables telemetry reporting Flags: -h, --help help for telemetry Global Flags: --trace string file to write execution trace data to -v, --verbose count verbose output Use "encore telemetry [command] --help" for more information about a command. ```
- Loading branch information
Showing
15 changed files
with
1,111 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/logrusorgru/aurora/v3" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"encr.dev/cli/cmd/encore/cmdutil" | ||
"encr.dev/cli/cmd/encore/root" | ||
"encr.dev/cli/internal/telemetry" | ||
"encr.dev/pkg/fns" | ||
daemonpb "encr.dev/proto/encore/daemon" | ||
) | ||
|
||
var TelemetryDisabledByEnvVar = os.Getenv("DISABLE_ENCORE_TELEMETRY") == "1" | ||
var TelemetryDebugByEnvVar = os.Getenv("ENCORE_TELEMETRY_DEBUG") == "1" | ||
|
||
func printTelemetryStatus() { | ||
status := aurora.Green("Enabled").String() | ||
if !telemetry.IsEnabled() { | ||
status = aurora.Red("Disabled").String() | ||
} | ||
fmt.Println(aurora.Sprintf("%s\n", aurora.Bold("Encore Telemetry"))) | ||
items := [][2]string{ | ||
{"Status", status}, | ||
} | ||
if root.Verbosity > 0 { | ||
items = append(items, [2]string{"Install ID", telemetry.GetAnonID()}) | ||
} | ||
if telemetry.IsDebug() { | ||
items = append(items, [2]string{"Debug", aurora.Green("Enabled").String()}) | ||
} | ||
maxKeyLen := fns.Max(items, func(entry [2]string) int { return len(entry[0]) }) | ||
for _, item := range items { | ||
spacing := strings.Repeat(" ", maxKeyLen-len(item[0])) | ||
fmt.Printf("%s: %s%s\n", item[0], spacing, item[1]) | ||
} | ||
fmt.Println(aurora.Sprintf("\nLearn more: %s", aurora.Underline("https://encore.dev/docs/telemetry"))) | ||
} | ||
|
||
func updateTelemetry(ctx context.Context) { | ||
// Update the telemetry config on the daemon if it is running | ||
if cmdutil.IsDaemonRunning(ctx) { | ||
daemon := cmdutil.ConnectDaemon(ctx) | ||
_, err := daemon.Telemetry(ctx, &daemonpb.TelemetryConfig{ | ||
AnonId: telemetry.GetAnonID(), | ||
Enabled: telemetry.IsEnabled(), | ||
Debug: telemetry.IsDebug(), | ||
}) | ||
if err != nil { | ||
log.Debug().Err(err).Msgf("could not update daemon telemetry: %s", err) | ||
} | ||
} | ||
if err := telemetry.SaveConfig(); err != nil { | ||
log.Debug().Err(err).Msgf("could not save telemetry: %s", err) | ||
} | ||
} | ||
|
||
var telemetryCommand = &cobra.Command{ | ||
Use: "telemetry", | ||
Short: "Reports the current telemetry status", | ||
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
printTelemetryStatus() | ||
}, | ||
} | ||
|
||
var telemetryEnableCommand = &cobra.Command{ | ||
Use: "enable", | ||
Short: "Enables telemetry reporting", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if telemetry.SetEnabled(true) { | ||
updateTelemetry(cmd.Context()) | ||
} | ||
printTelemetryStatus() | ||
}, | ||
} | ||
|
||
var telemetryDisableCommand = &cobra.Command{ | ||
Use: "disable", | ||
Short: "Disables telemetry reporting", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if telemetry.SetEnabled(false) { | ||
updateTelemetry(cmd.Context()) | ||
} | ||
printTelemetryStatus() | ||
}, | ||
} | ||
|
||
func init() { | ||
telemetryCommand.AddCommand(telemetryEnableCommand, telemetryDisableCommand) | ||
rootCmd.AddCommand(telemetryCommand) | ||
root.AddPreRun(func(cmd *cobra.Command, args []string) { | ||
update := false | ||
if TelemetryDisabledByEnvVar { | ||
update = telemetry.SetEnabled(false) | ||
} | ||
if cmd.Use == "daemon" { | ||
return | ||
} | ||
update = update || telemetry.SetDebug(TelemetryDebugByEnvVar) | ||
if update { | ||
go updateTelemetry(cmd.Context()) | ||
} | ||
if telemetry.ShouldShowWarning() && cmd.Use != "version" { | ||
fmt.Println() | ||
fmt.Println(aurora.Sprintf("%s: This CLI tool collects usage data to help us improve Encore.", aurora.Bold("Note"))) | ||
fmt.Println(aurora.Sprintf(" You can disable this by running '%s'.\n", aurora.Yellow("encore telemetry disable"))) | ||
telemetry.SetShownWarning() | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package daemon | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/protobuf/types/known/emptypb" | ||
|
||
"encr.dev/cli/internal/telemetry" | ||
daemonpb "encr.dev/proto/encore/daemon" | ||
) | ||
|
||
func (s *Server) Telemetry(ctx context.Context, req *daemonpb.TelemetryConfig) (*emptypb.Empty, error) { | ||
telemetry.UpdateConfig(req.AnonId, req.Enabled, req.Debug) | ||
return new(emptypb.Empty), nil | ||
} |
Oops, something went wrong.