- Notifications
You must be signed in to change notification settings - Fork913
chore: track usage of organizations in telemetry#16323
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
5c6578d
report organizations in telemetry, test that all relevant resources r…
hugodutka2a26b4d
add the IDPOrgSync field to telemetry deployment
hugodutka229aac7
add comments in org sync related functions in idpsync
hugodutkaab98f7f
update comment
hugodutka23b8505
fix importing enterprise code from agpl
hugodutka0baa504
update comment
hugodutka0d3a9a6
remove unnecessary dbauthz.AsSystemRestricted
hugodutka07057b1
make the IDPOrgSync field a pointer to ensure backwards compatibility
hugodutkaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionscoderd/idpsync/organization.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletionscoderd/telemetry/telemetry.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,6 +4,7 @@ import ( | ||
"bytes" | ||
"context" | ||
"crypto/sha256" | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
@@ -244,6 +245,11 @@ func (r *remoteReporter) deployment() error { | ||
return xerrors.Errorf("install source must be <=64 chars: %s", installSource) | ||
} | ||
idpOrgSync, err := checkIDPOrgSync(r.ctx, r.options.Database, r.options.DeploymentConfig) | ||
if err != nil { | ||
r.options.Logger.Debug(r.ctx, "check IDP org sync", slog.Error(err)) | ||
} | ||
data, err := json.Marshal(&Deployment{ | ||
ID: r.options.DeploymentID, | ||
Architecture: sysInfo.Architecture, | ||
@@ -263,6 +269,7 @@ func (r *remoteReporter) deployment() error { | ||
MachineID: sysInfo.UniqueID, | ||
StartedAt: r.startedAt, | ||
ShutdownAt: r.shutdownAt, | ||
IDPOrgSync: &idpOrgSync, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("marshal deployment: %w", err) | ||
@@ -284,6 +291,45 @@ func (r *remoteReporter) deployment() error { | ||
return nil | ||
} | ||
// idpOrgSyncConfig is a subset of | ||
// https://github.com/coder/coder/blob/5c6578d84e2940b9cfd04798c45e7c8042c3fe0e/coderd/idpsync/organization.go#L148 | ||
type idpOrgSyncConfig struct { | ||
Field string `json:"field"` | ||
} | ||
// checkIDPOrgSync inspects the server flags and the runtime config. It's based on | ||
// the OrganizationSyncEnabled function from enterprise/coderd/enidpsync/organizations.go. | ||
// It has one distinct difference: it doesn't check if the license entitles to the | ||
// feature, it only checks if the feature is configured. | ||
// | ||
// The above function is not used because it's very hard to make it available in | ||
// the telemetry package due to coder/coder package structure and initialization | ||
// order of the coder server. | ||
// | ||
// We don't check license entitlements because it's also hard to do from the | ||
// telemetry package, and the config check should be sufficient for telemetry purposes. | ||
// | ||
// While this approach duplicates code, it's simpler than the alternative. | ||
// | ||
// See https://github.com/coder/coder/pull/16323 for more details. | ||
func checkIDPOrgSync(ctx context.Context, db database.Store, values *codersdk.DeploymentValues) (bool, error) { | ||
// key based on https://github.com/coder/coder/blob/5c6578d84e2940b9cfd04798c45e7c8042c3fe0e/coderd/idpsync/idpsync.go#L168 | ||
syncConfigRaw, err := db.GetRuntimeConfig(ctx, "organization-sync-settings") | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
// If the runtime config is not set, we check if the deployment config | ||
// has the organization field set. | ||
return values != nil && values.OIDC.OrganizationField != "", nil | ||
} | ||
return false, xerrors.Errorf("get runtime config: %w", err) | ||
} | ||
syncConfig := idpOrgSyncConfig{} | ||
if err := json.Unmarshal([]byte(syncConfigRaw), &syncConfig); err != nil { | ||
return false, xerrors.Errorf("unmarshal runtime config: %w", err) | ||
} | ||
return syncConfig.Field != "", nil | ||
} | ||
// createSnapshot collects a full snapshot from the database. | ||
func (r *remoteReporter) createSnapshot() (*Snapshot, error) { | ||
var ( | ||
@@ -518,6 +564,21 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) { | ||
} | ||
return nil | ||
}) | ||
eg.Go(func() error { | ||
// Warning: When an organization is deleted, it's completely removed from | ||
// the database. It will no longer be reported, and there will be no other | ||
// indicator that it was deleted. This requires special handling when | ||
// interpreting the telemetry data later. | ||
Comment on lines +568 to +571 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We do intend to fix that with soft deletes. | ||
orgs, err := r.options.Database.GetOrganizations(r.ctx, database.GetOrganizationsParams{}) | ||
if err != nil { | ||
return xerrors.Errorf("get organizations: %w", err) | ||
} | ||
snapshot.Organizations = make([]Organization, 0, len(orgs)) | ||
for _, org := range orgs { | ||
snapshot.Organizations = append(snapshot.Organizations, ConvertOrganization(org)) | ||
} | ||
return nil | ||
}) | ||
err := eg.Wait() | ||
if err != nil { | ||
@@ -916,6 +977,14 @@ func ConvertExternalProvisioner(id uuid.UUID, tags map[string]string, provisione | ||
} | ||
} | ||
func ConvertOrganization(org database.Organization) Organization { | ||
return Organization{ | ||
ID: org.ID, | ||
CreatedAt: org.CreatedAt, | ||
IsDefault: org.IsDefault, | ||
} | ||
} | ||
// Snapshot represents a point-in-time anonymized database dump. | ||
// Data is aggregated by latest on the server-side, so partial data | ||
// can be sent without issue. | ||
@@ -942,6 +1011,7 @@ type Snapshot struct { | ||
WorkspaceModules []WorkspaceModule `json:"workspace_modules"` | ||
Workspaces []Workspace `json:"workspaces"` | ||
NetworkEvents []NetworkEvent `json:"network_events"` | ||
Organizations []Organization `json:"organizations"` | ||
} | ||
// Deployment contains information about the host running Coder. | ||
@@ -964,6 +1034,9 @@ type Deployment struct { | ||
MachineID string `json:"machine_id"` | ||
StartedAt time.Time `json:"started_at"` | ||
ShutdownAt *time.Time `json:"shutdown_at"` | ||
// While IDPOrgSync will always be set, it's nullable to make | ||
// the struct backwards compatible with older coder versions. | ||
IDPOrgSync *bool `json:"idp_org_sync"` | ||
} | ||
type APIKey struct { | ||
@@ -1457,6 +1530,12 @@ func NetworkEventFromProto(proto *tailnetproto.TelemetryEvent) (NetworkEvent, er | ||
}, nil | ||
} | ||
type Organization struct { | ||
ID uuid.UUID `json:"id"` | ||
IsDefault bool `json:"is_default"` | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
type noopReporter struct{} | ||
func (*noopReporter) Report(_ *Snapshot) {} | ||
75 changes: 69 additions & 6 deletionscoderd/telemetry/telemetry_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionsenterprise/coderd/enidpsync/organizations.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.