Skip to content

Commit

Permalink
refactor: move telemetry command to compute metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Oct 2, 2023
1 parent 59eb687 commit de810cb
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .fastly/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ config_version = 4
[fastly]
api_endpoint = "https://api.fastly.com"

[telemetry]
[wasm-metadata]
build_info = "enable"
machine_info = "enable"
package_info = "enable"
Expand Down
5 changes: 2 additions & 3 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import (
"github.com/fastly/cli/pkg/commands/serviceversion"
"github.com/fastly/cli/pkg/commands/shellcomplete"
"github.com/fastly/cli/pkg/commands/stats"
"github.com/fastly/cli/pkg/commands/telemetry"
tlsConfig "github.com/fastly/cli/pkg/commands/tls/config"
tlsCustom "github.com/fastly/cli/pkg/commands/tls/custom"
tlsCustomActivation "github.com/fastly/cli/pkg/commands/tls/custom/activation"
Expand Down Expand Up @@ -116,6 +115,7 @@ func defineCommands(
computeHashFiles := compute.NewHashFilesCommand(computeCmdRoot.CmdClause, g, computeBuild, m)
computeHashsum := compute.NewHashsumCommand(computeCmdRoot.CmdClause, g, computeBuild, m)
computeInit := compute.NewInitCommand(computeCmdRoot.CmdClause, g, m)
computeMetadata := compute.NewMetadataCommand(computeCmdRoot.CmdClause, g)
computePack := compute.NewPackCommand(computeCmdRoot.CmdClause, g, m)
computePublish := compute.NewPublishCommand(computeCmdRoot.CmdClause, g, computeBuild, computeDeploy, m)
computeServe := compute.NewServeCommand(computeCmdRoot.CmdClause, g, computeBuild, opts.Versioners.Viceroy, m)
Expand Down Expand Up @@ -384,7 +384,6 @@ func defineCommands(
statsHistorical := stats.NewHistoricalCommand(statsCmdRoot.CmdClause, g, m)
statsRealtime := stats.NewRealtimeCommand(statsCmdRoot.CmdClause, g, m)
statsRegions := stats.NewRegionsCommand(statsCmdRoot.CmdClause, g)
telemetryCmdRoot := telemetry.NewRootCommand(app, g)
tlsConfigCmdRoot := tlsConfig.NewRootCommand(app, g)
tlsConfigDescribe := tlsConfig.NewDescribeCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsConfigList := tlsConfig.NewListCommand(tlsConfigCmdRoot.CmdClause, g, m)
Expand Down Expand Up @@ -481,6 +480,7 @@ func defineCommands(
computeHashFiles,
computeHashsum,
computeInit,
computeMetadata,
computePack,
computePublish,
computeServe,
Expand Down Expand Up @@ -745,7 +745,6 @@ func defineCommands(
statsHistorical,
statsRealtime,
statsRegions,
telemetryCmdRoot,
tlsConfigCmdRoot,
tlsConfigDescribe,
tlsConfigList,
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func Run(opts RunOpts) error {
return nil
}

if !g.Config.CLI.TelemetryNoticeDisplayed && commandCollectsData(commandName) {
if !g.Config.CLI.MetadataNoticeDisplayed && commandCollectsData(commandName) {
// FIXME: We need the actual URL to point users to.
text.Important(g.Output, "The Fastly CLI is configured to collect data related to Wasm builds (e.g. compilation times, resource usage, and other non-identifying data). To learn more about what data is being collected, why, and how to disable it: https://www.fastly.com/")
text.Break(g.Output)
g.Config.CLI.TelemetryNoticeDisplayed = true
g.Config.CLI.MetadataNoticeDisplayed = true
err := g.Config.Write(g.ConfigPath)
if err != nil {
return fmt.Errorf("failed to persist change to telemetry notice: %w", err)
return fmt.Errorf("failed to persist change to metadata notice: %w", err)
}
time.Sleep(5 * time.Second) // this message is only displayed once so give the user a chance to see it before it possibly scrolls off screen
}
Expand Down
1 change: 0 additions & 1 deletion pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ service
service-auth
service-version
stats
telemetry
tls-config
tls-custom
tls-platform
Expand Down
95 changes: 95 additions & 0 deletions pkg/commands/compute/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package compute

import (
"fmt"
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)

// MetadataCommand controls what metadata is collected for a Wasm binary.
type MetadataCommand struct {
cmd.Base

disable bool
disableBuild bool
disableMachine bool
disablePackage bool
enable bool
enableBuild bool
enableMachine bool
enablePackage bool
}

// NewMetadataCommand returns a new command registered in the parent.
func NewMetadataCommand(parent cmd.Registerer, g *global.Data) *MetadataCommand {
var c MetadataCommand
c.Globals = g
c.CmdClause = parent.Command("metadata", "Control what metadata is collected")
c.CmdClause.Flag("disable", "Disable all metadata").BoolVar(&c.disable)
c.CmdClause.Flag("disable-build", "Disable metadata for information regarding the time taken for builds and compilation processes").BoolVar(&c.disableBuild)
c.CmdClause.Flag("disable-machine", "Disable metadata for general, non-identifying system specifications (CPU, RAM, operating system)").BoolVar(&c.disableMachine)
c.CmdClause.Flag("disable-package", "Disable metadata for packages and libraries utilized in your source code").BoolVar(&c.disablePackage)
c.CmdClause.Flag("enable", "Enable all metadata").BoolVar(&c.enable)
c.CmdClause.Flag("enable-build", "Enable metadata for information regarding the time taken for builds and compilation processes").BoolVar(&c.enableBuild)
c.CmdClause.Flag("enable-machine", "Enable metadata for general, non-identifying system specifications (CPU, RAM, operating system)").BoolVar(&c.enableMachine)
c.CmdClause.Flag("enable-package", "Enable metadata for packages and libraries utilized in your source code").BoolVar(&c.enablePackage)
return &c
}

// Exec implements the command interface.
func (c *MetadataCommand) Exec(_ io.Reader, out io.Writer) error {
if c.disable && c.enable {
return fsterr.ErrInvalidMetadataEnableDisableCombo
}
if c.disable {
c.Globals.Config.WasmMetadata = toggleAll("disable")
}
if c.enable {
c.Globals.Config.WasmMetadata = toggleAll("enable")
}
if c.disable && (c.enableBuild || c.enableMachine || c.enablePackage) {
text.Info(out, "We will disable all metadata except for the specified `--enable-*` flags")
text.Break(out)
}
if c.enable && (c.disableBuild || c.disableMachine || c.disablePackage) {
text.Info(out, "We will enable all metadata except for the specified `--disable-*` flags")
text.Break(out)
}
if c.enableBuild {
c.Globals.Config.WasmMetadata.BuildInfo = "enable"
}
if c.enableMachine {
c.Globals.Config.WasmMetadata.MachineInfo = "enable"
}
if c.enablePackage {
c.Globals.Config.WasmMetadata.PackageInfo = "enable"
}
if c.disableBuild {
c.Globals.Config.WasmMetadata.BuildInfo = "disable"
}
if c.disableMachine {
c.Globals.Config.WasmMetadata.MachineInfo = "disable"
}
if c.disablePackage {
c.Globals.Config.WasmMetadata.PackageInfo = "disable"
}
err := c.Globals.Config.Write(c.Globals.ConfigPath)
if err != nil {
return fmt.Errorf("failed to persist metadata choices to disk: %w", err)
}
text.Success(out, "configuration updated (see: `fastly config`)")
return nil
}

func toggleAll(state string) config.WasmMetadata {
var t config.WasmMetadata
t.BuildInfo = state
t.MachineInfo = state
t.PackageInfo = state
return t
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package telemetry_test
package compute_test

import (
"bytes"
Expand All @@ -23,7 +23,7 @@ type Scenario struct {
ExpectedConfig config.WasmMetadata
}

func TestTelemetry(t *testing.T) {
func TestMetadata(t *testing.T) {
var (
configPath string
data []byte
Expand All @@ -37,7 +37,7 @@ func TestTelemetry(t *testing.T) {
}

// Read the test config.toml data
path, err := filepath.Abs(filepath.Join("./", "testdata", "config.toml"))
path, err := filepath.Abs(filepath.Join("./", "testdata", "metadata", "config.toml"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestTelemetry(t *testing.T) {
scenarios := []Scenario{
{
TestScenario: testutil.TestScenario{
Args: args("telemetry --enable"),
Args: args("compute metadata --enable"),
WantOutput: "SUCCESS: configuration updated (see: `fastly config`)",
},
ExpectedConfig: config.WasmMetadata{
Expand All @@ -77,7 +77,7 @@ func TestTelemetry(t *testing.T) {
},
{
TestScenario: testutil.TestScenario{
Args: args("telemetry --disable"),
Args: args("compute metadata --disable"),
WantOutput: "SUCCESS: configuration updated (see: `fastly config`)",
},
ExpectedConfig: config.WasmMetadata{
Expand All @@ -88,9 +88,9 @@ func TestTelemetry(t *testing.T) {
},
{
TestScenario: testutil.TestScenario{
Args: args("telemetry --enable --disable-build"),
Args: args("compute metadata --enable --disable-build"),
WantOutputs: []string{
"INFO: We will enable all telemetry except for the specified `--disable-*` flags",
"INFO: We will enable all metadata except for the specified `--disable-*` flags",
"SUCCESS: configuration updated (see: `fastly config`)",
},
},
Expand All @@ -102,9 +102,9 @@ func TestTelemetry(t *testing.T) {
},
{
TestScenario: testutil.TestScenario{
Args: args("telemetry --disable --enable-machine"),
Args: args("compute metadata --disable --enable-machine"),
WantOutputs: []string{
"INFO: We will disable all telemetry except for the specified `--enable-*` flags",
"INFO: We will disable all metadata except for the specified `--enable-*` flags",
"SUCCESS: configuration updated (see: `fastly config`)",
},
},
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions pkg/commands/telemetry/doc.go

This file was deleted.

96 changes: 0 additions & 96 deletions pkg/commands/telemetry/root.go

This file was deleted.

18 changes: 9 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Fastly struct {
APIEndpoint string `toml:"api_endpoint"`
}

// WasmMetadata represents what telemetry data will be recorded.
// WasmMetadata represents what metadata will be collected.
type WasmMetadata struct {
// BuildInfo represents information regarding the time taken for builds and
// compilation processes, helping us identify bottlenecks and optimize
Expand All @@ -72,17 +72,17 @@ type WasmMetadata struct {
// RAM, operating system) to better understand the hardware landscape our CLI
// operates in (enable/disable).
MachineInfo string `toml:"machine_info"`
// PackageInfo represents packages and libraries utilized in your source code,
// PackageInfo represents packages and libraries utilized by your source code,
// enabling us to prioritize support for the most commonly used components
// (enable/disable).
PackageInfo string `toml:"package_info"`
}

// CLI represents CLI specific configuration.
type CLI struct {
// TelemetryNoticeDisplayed indicates if the user has been notified of the
// telemetry behaviours being enabled by default and how they can opt-out.
TelemetryNoticeDisplayed bool `toml:"telemetry_notice_displayed"`
// MetadataNoticeDisplayed indicates if the user has been notified of the
// metadata behaviours being enabled by default and how they can opt-out.
MetadataNoticeDisplayed bool `toml:"metadata_notice_displayed"`
// Version indicates the CLI configuration version.
// It is updated each time a change is made to the config structure.
Version string `toml:"version"`
Expand Down Expand Up @@ -424,16 +424,16 @@ func (f *File) Write(path string) error {
// Environment represents all of the configuration parameters that can come
// from environment variables.
type Environment struct {
Token string
Endpoint string
TelemetryDisable string
Token string
Endpoint string
WasmMetadataDisable string
}

// Read populates the fields from the provided environment.
func (e *Environment) Read(state map[string]string) {
e.Token = state[env.Token]
e.Endpoint = state[env.Endpoint]
e.TelemetryDisable = state[env.TelemetryDisable]
e.WasmMetadataDisable = state[env.WasmMetadataDisable]
}

// invalidStaticConfigErr generates an error to alert the user to an issue with
Expand Down
Loading

0 comments on commit de810cb

Please sign in to comment.