Skip to content

Commit e55e8ee

Browse files
authored
fix(cli): add backwards compat for old telemetry env and tests (coder#15476)
1 parent 99dd13d commit e55e8ee

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

cli/server_test.go

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

1623+
//nolint: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+
//nolint:paralleltest // No need to reinitialise the variable tt (Go version).
1629+
for _, tt := range []struct {
1630+
key string
1631+
val string
1632+
want bool
1633+
}{
1634+
{"", "", true},
1635+
{"CODER_TELEMETRY_ENABLE", "true", true},
1636+
{"CODER_TELEMETRY_ENABLE", "false", false},
1637+
{"CODER_TELEMETRY", "true", true},
1638+
{"CODER_TELEMETRY", "false", false},
1639+
} {
1640+
t.Run(fmt.Sprintf("%s=%s", tt.key, tt.val), func(t *testing.T) {
1641+
t.Parallel()
1642+
var b bytes.Buffer
1643+
inv, _ := clitest.New(t, "server", "--write-config")
1644+
inv.Stdout = &b
1645+
inv.Environ.Set(tt.key, tt.val)
1646+
clitest.Run(t, inv)
1647+
1648+
var dv codersdk.DeploymentValues
1649+
err := yaml.Unmarshal(b.Bytes(), &dv)
1650+
require.NoError(t, err)
1651+
assert.Equal(t, tt.want, dv.Telemetry.Enable.Value())
1652+
})
1653+
}
1654+
}
1655+
16231656
//nolint:tparallel,paralleltest // This test cannot be run in parallel due to signal handling.
16241657
func TestServer_InterruptShutdown(t *testing.T) {
16251658
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",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ require (
204204
github.com/charmbracelet/bubbles v0.20.0
205205
github.com/charmbracelet/bubbletea v1.2.1
206206
github.com/charmbracelet/lipgloss v1.0.0
207-
github.com/coder/serpent v0.8.0
207+
github.com/coder/serpent v0.9.0
208208
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
209209
github.com/emersion/go-smtp v0.21.2
210210
github.com/go-jose/go-jose/v4 v4.0.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ github.com/coder/quartz v0.1.2 h1:PVhc9sJimTdKd3VbygXtS4826EOCpB1fXoRlLnCrE+s=
228228
github.com/coder/quartz v0.1.2/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
229229
github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
230230
github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY=
231-
github.com/coder/serpent v0.8.0 h1:6OR+k6fekhSeEDmwwzBgnSjaa7FfGGrMlc3GoAEH9dg=
232-
github.com/coder/serpent v0.8.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q=
231+
github.com/coder/serpent v0.9.0 h1:gfoTNOUitVdO1ZeZKk8xHiFjBvjzCQXxcMpZ7Dg41Qg=
232+
github.com/coder/serpent v0.9.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q=
233233
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuOD6a/zVP3rcxezNsoDseTUw=
234234
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
235235
github.com/coder/tailscale v1.1.1-0.20241003034647-02286e537fc2 h1:mBbPFyJ2i9o490IwWGvWgtG0qmvIk45R7GWJpoaXotI=

0 commit comments

Comments
 (0)