8000 feat: Add anonymized telemetry to report product usage by kylecarbs · Pull Request #2273 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Add anonymized telemetry to report product usage #2273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 17, 2022
Prev Previous commit
Next Next commit
Track API key usage
  • Loading branch information
kylecarbs committed Jun 16, 2022
commit b74d12742a9bf2a89ad73dc647dbb6f38668e25d
13 changes: 13 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ func (q *fakeQuerier) GetAPIKeyByID(_ context.Context, id string) (database.APIK
return database.APIKey{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetAPIKeysLastUsedAfter(_ context.Context, after time.Time) ([]database.APIKey, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

apiKeys := make([]database.APIKey, 0)
for _, key := range q.apiKeys {
if key.LastUsed.After(after) {
apiKeys = append(apiKeys, key)
}
}
return apiKeys, nil
}

func (q *fakeQuerier) DeleteAPIKeyByID(_ context.Context, id string) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/database/queries/apikeys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ WHERE
LIMIT
1;

-- name: GetAPIKeysLastUsedAfter :many
SELECT * FROM api_keys WHERE last_used > $1;

-- name: InsertAPIKey :one
INSERT INTO
api_keys (
Expand Down
31 changes: 31 additions & 0 deletions coderd/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
}
)

eg.Go(func() error {
apiKeys, err := r.options.Database.GetAPIKeysLastUsedAfter(ctx, createdAfter)
if err != nil {
return xerrors.Errorf("get api keys last used: %w", err)
}
snapshot.APIKeys = make([]APIKey, 0, len(apiKeys))
for _, apiKey := range apiKeys {
snapshot.APIKeys = append(snapshot.APIKeys, ConvertAPIKey(apiKey))
}
return nil
})
eg.Go(func() error {
schemas, err := r.options.Database.GetParameterSchemasCreatedAfter(ctx, createdAfter)
if err != nil {
Expand Down Expand Up @@ -414,6 +425,17 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
return snapshot, nil
}

// ConvertAPIKey anonymizes an API key.
func ConvertAPIKey(apiKey database.APIKey) APIKey {
return APIKey{
ID: apiKey.ID,
UserID: apiKey.UserID,
CreatedAt: apiKey.CreatedAt,
LastUsed: apiKey.LastUsed,
LoginType: apiKey.LoginType,
}
}

// ConvertWorkspace anonymizes a workspace.
func ConvertWorkspace(workspace database.Workspace) Workspace {
return Workspace{
Expand Down Expand Up @@ -551,6 +573,7 @@ func ConvertTemplateVersion(version database.TemplateVersion) TemplateVersion {
type Snapshot struct {
DeploymentID string `json:"deployment_id"`

APIKeys []APIKey `json:"api_keys"`
ParameterSchemas []ParameterSchema `json:"parameter_schemas"`
ProvisionerJobs []ProvisionerJob `json:"provisioner_jobs"`
Templates []Template `json:"templates"`
Expand Down Expand Up @@ -585,6 +608,14 @@ type Deployment struct {
ShutdownAt *time.Time `json:"shutdown_at"`
}

type APIKey struct {
ID string `json:"id"`
UserID uuid.UUID `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
LastUsed time.Time `json:"last_used"`
LoginType database.LoginType `json:"login_type"`
}

type User struct {
ID uuid.UUID `json:"uuid"`
CreatedAt time.Time `json:"created_at"`
Expand Down
7 changes: 6 additions & 1 deletion coderd/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func TestTelemetry(t *testing.T) {
t.Parallel()
db := databasefake.New()
ctx := context.Background()
_, err := db.InsertParameterSchema(ctx, database.InsertParameterSchemaParams{
_, err := db.InsertAPIKey(ctx, database.InsertAPIKeyParams{
ID: uuid.NewString(),
LastUsed: database.Now(),
})
require.NoError(t, err)
_, err = db.InsertParameterSchema(ctx, database.InsertParameterSchemaParams{
ID: uuid.New(),
CreatedAt: database.Now(),
})
Expand Down
0