Users tracks the total number of registered users, partitioned by status.
(ctx context.Context, logger slog.Logger, clk quartz.Clock, registerer prometheus.Registerer, db database.Store, duration time.Duration)
| 77 | |
| 78 | // Users tracks the total number of registered users, partitioned by status. |
| 79 | func Users(ctx context.Context, logger slog.Logger, clk quartz.Clock, registerer prometheus.Registerer, db database.Store, duration time.Duration) (func(), error) { |
| 80 | if duration == 0 { |
| 81 | // It's not super important this tracks real-time. |
| 82 | duration = defaultRefreshRate * 5 |
| 83 | } |
| 84 | |
| 85 | gauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 86 | Namespace: "coderd", |
| 87 | Subsystem: "api", |
| 88 | Name: "total_user_count", |
| 89 | Help: "The total number of registered users, partitioned by status.", |
| 90 | }, []string{"status"}) |
| 91 | err := registerer.Register(gauge) |
| 92 | if err != nil { |
| 93 | return nil, xerrors.Errorf("register total_user_count gauge: %w", err) |
| 94 | } |
| 95 | |
| 96 | ctx, cancelFunc := context.WithCancel(ctx) |
| 97 | done := make(chan struct{}) |
| 98 | ticker := clk.NewTicker(duration) |
| 99 | go func() { |
| 100 | defer close(done) |
| 101 | defer ticker.Stop() |
| 102 | for { |
| 103 | select { |
| 104 | case <-ctx.Done(): |
| 105 | return |
| 106 | case <-ticker.C: |
| 107 | } |
| 108 | |
| 109 | gauge.Reset() |
| 110 | //nolint:gocritic // This is a system service that needs full access |
| 111 | //to the users table. |
| 112 | users, err := db.GetUsers(dbauthz.AsSystemRestricted(ctx), database.GetUsersParams{}) |
| 113 | if err != nil { |
| 114 | logger.Error(ctx, "get all users for prometheus metrics", slog.Error(err)) |
| 115 | continue |
| 116 | } |
| 117 | |
| 118 | for _, user := range users { |
| 119 | gauge.WithLabelValues(string(user.Status)).Inc() |
| 120 | } |
| 121 | } |
| 122 | }() |
| 123 | return func() { |
| 124 | cancelFunc() |
| 125 | <-done |
| 126 | }, nil |
| 127 | } |
| 128 | |
| 129 | // Workspaces tracks the total number of workspaces with labels on status. |
| 130 | func Workspaces(ctx context.Context, logger slog.Logger, registerer prometheus.Registerer, db database.Store, duration time.Duration) (func(), error) { |