Skip to content

Commit d0a2b4b

Browse files
committed
fix(cli): add backwards compat for old telemetry env and tests
1 parent 78b4967 commit d0a2b4b

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

cli/server_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,40 @@ func TestServer_Production(t *testing.T) {
16201620
require.NoError(t, err)
16211621
}
16221622

1623+
//nolin:tparallel,paralleltest // This test sets environment variables.
1624+
func TestServer_TelemetryDisable(t *testing.T) {
1625+
// Set the default telemetry to true (normally disabled in tests).
1626+
t.Setenv("CODER_TEST_TELEMETRY_DEFAULT_ENABLE", "true")
1627+
1628+
for _, tt := range []struct {
1629+
key string
1630+
val string
1631+
want bool
1632+
}{
1633+
{"", "", true},
1634+
{"CODER_TELEMETRY_ENABLE", "true", true},
1635+
{"CODER_TELEMETRY_ENABLE", "false", false},
1636+
{"CODER_TELEMETRY", "true", true},
1637+
{"CODER_TELEMETRY", "false", false},
1638+
} {
1639+
t.Run(fmt.Sprintf("%s=%s", tt.key, tt.val), func(t *testing.T) {
1640+
t.Parallel()
1641+
var b bytes.Buffer
1642+
inv, _ := clitest.New(t, "server", "--write-config")
1643+
inv.Stdout = &b
1644+
inv.Environ.Set(tt.key, tt.val)
1645+
clitest.Run(t, inv)
1646+
1647+
t.Logf("config:\n%s", b.String())
1648+
1649+
var dv codersdk.DeploymentValues
1650+
err := yaml.Unmarshal(b.Bytes(), &dv)
1651+
require.NoError(t, err)
1652+
assert.Equal(t, tt.want, dv.Telemetry.Enable.Value())
1653+
})
1654+
}
1655+
}
1656+
16231657
//nolint:tparallel,paralleltest // This test cannot be run in parallel due to signal handling.
16241658
func TestServer_InterruptShutdown(t *testing.T) {
16251659
t.Skip("This test issues an interrupt signal which will propagate to the test runner.")

codersdk/deployment.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,16 @@ when required by your organization's security policy.`,
11431143
Group: &deploymentGroupEmailTLS,
11441144
YAML: "certKeyFile",
11451145
}
1146+
telemetryEnable := serpent.Option{
1147+
Name: "Telemetry Enable",
1148+
Description: "Whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product.",
1149+
Flag: "telemetry",
1150+
Env: "CODER_TELEMETRY_ENABLE",
1151+
Default: strconv.FormatBool(flag.Lookup("test.v") == nil || os.Getenv("CODER_TEST_TELEMETRY_DEFAULT_ENABLE") == "true"),
1152+
Value: &c.Telemetry.Enable,
1153+
Group: &deploymentGroupTelemetry,
1154+
YAML: "enable",
1155+
}
11461156
opts := serpent.OptionSet{
11471157
{
11481158
Name: "Access URL",
@@ -1903,15 +1913,19 @@ when required by your organization's security policy.`,
19031913
YAML: "dangerousSkipIssuerChecks",
19041914
},
19051915
// Telemetry settings
1916+
telemetryEnable,
19061917
{
1907-
Name: "Telemetry Enable",
1908-
Description: "Whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product.",
1909-
Flag: "telemetry",
1910-
Env: "CODER_TELEMETRY_ENABLE",
1911-
Default: strconv.FormatBool(flag.Lookup("test.v") == nil),
1912-
Value: &c.Telemetry.Enable,
1913-
Group: &deploymentGroupTelemetry,
1914-
YAML: "enable",
1918+
Hidden: true,
1919+
Name: "Telemetry (backwards compatibility)",
1920+
// Note the flip-flop of flag and env to maintain backwards
1921+
// compatibility and consistency. Inconsistently, the env
1922+
// was renamed to CODER_TELEMETRY_ENABLE in the past, but
1923+
// the flag was not renamed -enable.
1924+
Flag: "telemetry-enable",
1925+
Env: "CODER_TELEMETRY",
1926+
Value: &c.Telemetry.Enable,
1927+
Group: &deploymentGroupTelemetry,
1928+
UseInstead: []serpent.Option{telemetryEnable},
19151929
},
19161930
{
19171931
Name: "Telemetry URL",

0 commit comments

Comments
 (0)