diff --git a/cli/server_test.go b/cli/server_test.go index 83a7f7171c6f5..9cf466c6c35aa 100644 --- a/cli/server_test.go +++ b/cli/server_test.go @@ -1620,6 +1620,39 @@ func TestServer_Production(t *testing.T) { require.NoError(t, err) } +//nolint:tparallel,paralleltest // This test sets environment variables. +func TestServer_TelemetryDisable(t *testing.T) { + // Set the default telemetry to true (normally disabled in tests). + t.Setenv("CODER_TEST_TELEMETRY_DEFAULT_ENABLE", "true") + + //nolint:paralleltest // No need to reinitialise the variable tt (Go version). + for _, tt := range []struct { + key string + val string + want bool + }{ + {"", "", true}, + {"CODER_TELEMETRY_ENABLE", "true", true}, + {"CODER_TELEMETRY_ENABLE", "false", false}, + {"CODER_TELEMETRY", "true", true}, + {"CODER_TELEMETRY", "false", false}, + } { + t.Run(fmt.Sprintf("%s=%s", tt.key, tt.val), func(t *testing.T) { + t.Parallel() + var b bytes.Buffer + inv, _ := clitest.New(t, "server", "--write-config") + inv.Stdout = &b + inv.Environ.Set(tt.key, tt.val) + clitest.Run(t, inv) + + var dv codersdk.DeploymentValues + err := yaml.Unmarshal(b.Bytes(), &dv) + require.NoError(t, err) + assert.Equal(t, tt.want, dv.Telemetry.Enable.Value()) + }) + } +} + //nolint:tparallel,paralleltest // This test cannot be run in parallel due to signal handling. func TestServer_InterruptShutdown(t *testing.T) { t.Skip("This test issues an interrupt signal which will propagate to the test runner.") diff --git a/codersdk/deployment.go b/codersdk/deployment.go index 5959c35fa9d95..0a97da7d3958f 100644 --- a/codersdk/deployment.go +++ b/codersdk/deployment.go @@ -1143,6 +1143,16 @@ when required by your organization's security policy.`, Group: &deploymentGroupEmailTLS, YAML: "certKeyFile", } + telemetryEnable := serpent.Option{ + Name: "Telemetry Enable", + Description: "Whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product.", + Flag: "telemetry", + Env: "CODER_TELEMETRY_ENABLE", + Default: strconv.FormatBool(flag.Lookup("test.v") == nil || os.Getenv("CODER_TEST_TELEMETRY_DEFAULT_ENABLE") == "true"), + Value: &c.Telemetry.Enable, + Group: &deploymentGroupTelemetry, + YAML: "enable", + } opts := serpent.OptionSet{ { Name: "Access URL", @@ -1903,15 +1913,19 @@ when required by your organization's security policy.`, YAML: "dangerousSkipIssuerChecks", }, // Telemetry settings + telemetryEnable, { - Name: "Telemetry Enable", - Description: "Whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product.", - Flag: "telemetry", - Env: "CODER_TELEMETRY_ENABLE", - Default: strconv.FormatBool(flag.Lookup("test.v") == nil), - Value: &c.Telemetry.Enable, - Group: &deploymentGroupTelemetry, - YAML: "enable", + Hidden: true, + Name: "Telemetry (backwards compatibility)", + // Note the flip-flop of flag and env to maintain backwards + // compatibility and consistency. Inconsistently, the env + // was renamed to CODER_TELEMETRY_ENABLE in the past, but + // the flag was not renamed -enable. + Flag: "telemetry-enable", + Env: "CODER_TELEMETRY", + Value: &c.Telemetry.Enable, + Group: &deploymentGroupTelemetry, + UseInstead: []serpent.Option{telemetryEnable}, }, { Name: "Telemetry URL", diff --git a/go.mod b/go.mod index 49603fe69e8a3..3fd95bc456df2 100644 --- a/go.mod +++ b/go.mod @@ -204,7 +204,7 @@ require ( github.com/charmbracelet/bubbles v0.20.0 github.com/charmbracelet/bubbletea v1.1.0 github.com/charmbracelet/lipgloss v1.0.0 - github.com/coder/serpent v0.8.0 + github.com/coder/serpent v0.9.0 github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/emersion/go-smtp v0.21.2 github.com/go-jose/go-jose/v4 v4.0.2 diff --git a/go.sum b/go.sum index 5f7ad74b2e704..c625d5d82b060 100644 --- a/go.sum +++ b/go.sum @@ -228,8 +228,8 @@ github.com/coder/quartz v0.1.2 h1:PVhc9sJimTdKd3VbygXtS4826EOCpB1fXoRlLnCrE+s= github.com/coder/quartz v0.1.2/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA= github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc= github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY= -github.com/coder/serpent v0.8.0 h1:6OR+k6fekhSeEDmwwzBgnSjaa7FfGGrMlc3GoAEH9dg= -github.com/coder/serpent v0.8.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q= +github.com/coder/serpent v0.9.0 h1:gfoTNOUitVdO1ZeZKk8xHiFjBvjzCQXxcMpZ7Dg41Qg= +github.com/coder/serpent v0.9.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q= github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuOD6a/zVP3rcxezNsoDseTUw= github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ= github.com/coder/tailscale v1.1.1-0.20241003034647-02286e537fc2 h1:mBbPFyJ2i9o490IwWGvWgtG0qmvIk45R7GWJpoaXotI=