Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab95ae8

Browse files
authored
feat(coderd): add enabled experiments to telemetry (coder#12656)
1 parent f0f9569 commit ab95ae8

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

cli/cliui/provisionerjob_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/coder/coder/v2/testutil"
1514
"github.com/stretchr/testify/assert"
1615

16+
"github.com/coder/coder/v2/testutil"
17+
1718
"github.com/coder/coder/v2/cli/cliui"
1819
"github.com/coder/coder/v2/coderd/database/dbtime"
1920
"github.com/coder/coder/v2/codersdk"

cli/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
819819
Prometheus: vals.Prometheus.Enable.Value(),
820820
STUN: len(vals.DERP.Server.STUNAddresses) != 0,
821821
Tunnel: tunnel != nil,
822+
Experiments: vals.Experiments.Value(),
822823
ParseLicenseJWT: func(lic *telemetry.License) error {
823824
// This will be nil when running in AGPL-only mode.
824825
if options.ParseLicenseClaims == nil {

coderd/telemetry/telemetry.go

+20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Options struct {
5454
SnapshotFrequency time.Duration
5555
Tunnel bool
5656
ParseLicenseJWT func(lic *License) error
57+
Experiments []string
5758
}
5859

5960
// New constructs a reporter for telemetry data.
@@ -480,6 +481,10 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
480481
}
481482
return nil
482483
})
484+
eg.Go(func() error {
485+
snapshot.Experiments = ConvertExperiments(r.options.Experiments)
486+
return nil
487+
})
483488

484489
err := eg.Wait()
485490
if err != nil {
@@ -741,6 +746,16 @@ func ConvertExternalProvisioner(id uuid.UUID, tags map[string]string, provisione
741746
}
742747
}
743748

749+
func ConvertExperiments(experiments []string) []Experiment {
750+
var out []Experiment
751+
752+
for _, exp := range experiments {
753+
out = append(out, Experiment{Name: exp})
754+
}
755+
756+
return out
757+
}
758+
744759
// Snapshot represents a point-in-time anonymized database dump.
745760
// Data is aggregated by latest on the server-side, so partial data
746761
// can be sent without issue.
@@ -763,6 +778,7 @@ type Snapshot struct {
763778
WorkspaceResourceMetadata []WorkspaceResourceMetadata `json:"workspace_resource_metadata"`
764779
WorkspaceResources []WorkspaceResource `json:"workspace_resources"`
765780
Workspaces []Workspace `json:"workspaces"`
781+
Experiments []Experiment `json:"experiments"`
766782
}
767783

768784
// Deployment contains information about the host running Coder.
@@ -971,6 +987,10 @@ type ExternalProvisioner struct {
971987
ShutdownAt *time.Time `json:"shutdown_at"`
972988
}
973989

990+
type Experiment struct {
991+
Name string `json:"name"`
992+
}
993+
974994
type noopReporter struct{}
975995

976996
func (*noopReporter) Report(_ *Snapshot) {}

coderd/telemetry/telemetry_test.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestTelemetry(t *testing.T) {
8585
assert.NoError(t, err)
8686
_, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
8787

88-
_, snapshot := collectSnapshot(t, db)
88+
_, snapshot := collectSnapshot(t, db, nil)
8989
require.Len(t, snapshot.ProvisionerJobs, 1)
9090
require.Len(t, snapshot.Licenses, 1)
9191
require.Len(t, snapshot.Templates, 1)
@@ -110,21 +110,32 @@ func TestTelemetry(t *testing.T) {
110110
_ = dbgen.User(t, db, database.User{
111111
112112
})
113-
_, snapshot := collectSnapshot(t, db)
113+
_, snapshot := collectSnapshot(t, db, nil)
114114
require.Len(t, snapshot.Users, 1)
115115
require.Equal(t, snapshot.Users[0].EmailHashed, "bb44bf07cf9a2db0554bba63a03d822c927deae77df101874496df5a6a3e896d@coder.com")
116116
})
117+
t.Run("Experiments", func(t *testing.T) {
118+
t.Parallel()
119+
120+
const expName = "my-experiment"
121+
exps := []string{expName}
122+
_, snapshot := collectSnapshot(t, dbmem.New(), func(opts telemetry.Options) telemetry.Options {
123+
opts.Experiments = exps
124+
return opts
125+
})
126+
require.Equal(t, []telemetry.Experiment{{Name: expName}}, snapshot.Experiments)
127+
})
117128
}
118129

119130
// nolint:paralleltest
120131
func TestTelemetryInstallSource(t *testing.T) {
121132
t.Setenv("CODER_TELEMETRY_INSTALL_SOURCE", "aws_marketplace")
122133
db := dbmem.New()
123-
deployment, _ := collectSnapshot(t, db)
134+
deployment, _ := collectSnapshot(t, db, nil)
124135
require.Equal(t, "aws_marketplace", deployment.InstallSource)
125136
}
126137

127-
func collectSnapshot(t *testing.T, db database.Store) (*telemetry.Deployment, *telemetry.Snapshot) {
138+
func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts telemetry.Options) telemetry.Options) (*telemetry.Deployment, *telemetry.Snapshot) {
128139
t.Helper()
129140
deployment := make(chan *telemetry.Deployment, 64)
130141
snapshot := make(chan *telemetry.Snapshot, 64)
@@ -149,12 +160,17 @@ func collectSnapshot(t *testing.T, db database.Store) (*telemetry.Deployment, *t
149160
t.Cleanup(server.Close)
150161
serverURL, err := url.Parse(server.URL)
151162
require.NoError(t, err)
152-
reporter, err := telemetry.New(telemetry.Options{
163+
options := telemetry.Options{
153164
Database: db,
154165
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
155166
URL: serverURL,
156167
DeploymentID: uuid.NewString(),
157-
})
168+
}
169+
if addOptionsFn != nil {
170+
options = addOptionsFn(options)
171+
}
172+
173+
reporter, err := telemetry.New(options)
158174
require.NoError(t, err)
159175
t.Cleanup(reporter.Close)
160176
return <-deployment, <-snapshot

0 commit comments

Comments
 (0)