MCPcopy Index your code
hub / github.com/coder/coder / collectBoundaryUsageSummary

Method collectBoundaryUsageSummary

coderd/telemetry/telemetry.go:918–965  ·  view source on GitHub ↗

collectBoundaryUsageSummary collects boundary usage statistics from all replicas and resets the stats for the next telemetry period. Returns nil if another replica has already collected for this period.

(ctx context.Context)

Source from the content-addressed store, hash-verified

916// replicas and resets the stats for the next telemetry period. Returns nil if
917// another replica has already collected for this period.
918func (r *remoteReporter) collectBoundaryUsageSummary(ctx context.Context) (*BoundaryUsageSummary, error) {
919 // Use twice the snapshot frequency as the staleness limit to ensure we
920 // capture data from replicas that may have slightly different flush times.
921 maxStaleness := r.options.SnapshotFrequency * 2
922 //nolint:gocritic // This is the actual collection of boundary usage tracking.
923 boundaryCtx := dbauthz.AsBoundaryUsageTracker(ctx)
924
925 // Claim the telemetry lock for this period. Use snapshot frequency so each
926 // telemetry snapshot period gets exactly one collection.
927 now := dbtime.Time(r.options.Clock.Now()).UTC()
928 periodEndingAt := now.Truncate(r.options.SnapshotFrequency)
929 err := r.options.Database.InsertTelemetryLock(ctx, database.InsertTelemetryLockParams{
930 EventType: "boundary_usage_summary",
931 PeriodEndingAt: periodEndingAt,
932 })
933 if database.IsUniqueViolation(err, database.UniqueTelemetryLocksPkey) {
934 r.options.Logger.Debug(ctx, "boundary usage telemetry lock already claimed by another replica, skipping", slog.F("period_ending_at", periodEndingAt))
935 return nil, nil //nolint:nilnil // This is simple to handle when dealing with telemetry.
936 }
937 if err != nil {
938 return nil, xerrors.Errorf("insert boundary usage telemetry lock (period_ending_at=%q): %w", periodEndingAt, err)
939 }
940
941 var summary database.GetAndResetBoundaryUsageSummaryRow
942 err = r.options.Database.InTx(func(tx database.Store) error {
943 // The advisory lock use here ensures a clean transition to the next snapshot by
944 // preventing replicas from upserting row(s) at the same time as we aggregate and
945 // delete all rows here.
946 var txErr error
947 if txErr = tx.AcquireLock(boundaryCtx, database.LockIDBoundaryUsageStats); txErr != nil {
948 return txErr
949 }
950 summary, txErr = tx.GetAndResetBoundaryUsageSummary(boundaryCtx, maxStaleness.Milliseconds())
951 return txErr
952 }, nil)
953 if err != nil {
954 return nil, xerrors.Errorf("get and reset boundary usage summary: %w", err)
955 }
956
957 return &BoundaryUsageSummary{
958 UniqueWorkspaces: summary.UniqueWorkspaces,
959 UniqueUsers: summary.UniqueUsers,
960 AllowedRequests: summary.AllowedRequests,
961 DeniedRequests: summary.DeniedRequests,
962 PeriodStart: now.Add(-r.options.SnapshotFrequency),
963 PeriodDurationMilliseconds: r.options.SnapshotFrequency.Milliseconds(),
964 }, nil
965}
966
967// collectUserSecretsSummary returns a deployment-wide aggregate of user
968// secrets configuration. Returns nil if another replica has already

Callers 1

createSnapshotMethod · 0.95

Calls 9

AsBoundaryUsageTrackerFunction · 0.92
TimeFunction · 0.92
IsUniqueViolationFunction · 0.92
InsertTelemetryLockMethod · 0.65
InTxMethod · 0.65
AcquireLockMethod · 0.65
AddMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected