@@ -23,7 +23,7 @@ import (
23
23
)
24
24
25
25
// ActiveUsers tracks the number of users that have authenticated within the past hour.
26
- func ActiveUsers (ctx context.Context , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (context. CancelFunc , error ) {
26
+ func ActiveUsers (ctx context.Context , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (func () , error ) {
27
27
if duration == 0 {
28
28
duration = 5 * time .Minute
29
29
}
@@ -40,8 +40,10 @@ func ActiveUsers(ctx context.Context, registerer prometheus.Registerer, db datab
40
40
}
41
41
42
42
ctx , cancelFunc := context .WithCancel (ctx )
43
+ done := make (chan struct {})
43
44
ticker := time .NewTicker (duration )
44
45
go func () {
46
+ defer close (done )
45
47
defer ticker .Stop ()
46
48
for {
47
49
select {
@@ -61,11 +63,14 @@ func ActiveUsers(ctx context.Context, registerer prometheus.Registerer, db datab
61
63
gauge .Set (float64 (len (distinctUsers )))
62
64
}
63
65
}()
64
- return cancelFunc , nil
66
+ return func () {
67
+ cancelFunc ()
68
+ <- done
69
+ }, nil
65
70
}
66
71
67
72
// Workspaces tracks the total number of workspaces with labels on status.
68
- func Workspaces (ctx context.Context , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (context. CancelFunc , error ) {
73
+ func Workspaces (ctx context.Context , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (func () , error ) {
69
74
if duration == 0 {
70
75
duration = 5 * time .Minute
71
76
}
@@ -85,8 +90,11 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
85
90
gauge .WithLabelValues ("pending" ).Set (0 )
86
91
87
92
ctx , cancelFunc := context .WithCancel (ctx )
93
+ done := make (chan struct {})
94
+
88
95
ticker := time .NewTicker (duration )
89
96
go func () {
97
+ defer close (done )
90
98
defer ticker .Stop ()
91
99
for {
92
100
select {
@@ -115,11 +123,14 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
115
123
}
116
124
}
117
125
}()
118
- return cancelFunc , nil
126
+ return func () {
127
+ cancelFunc ()
128
+ <- done
129
+ }, nil
119
130
}
120
131
121
132
// Agents tracks the total number of workspaces with labels on status.
122
- func Agents (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , coordinator * atomic.Pointer [tailnet.Coordinator ], derpMap * tailcfg.DERPMap , agentInactiveDisconnectTimeout , duration time.Duration ) (context. CancelFunc , error ) {
133
+ func Agents (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , coordinator * atomic.Pointer [tailnet.Coordinator ], derpMap * tailcfg.DERPMap , agentInactiveDisconnectTimeout , duration time.Duration ) (func () , error ) {
123
134
if duration == 0 {
124
135
duration = 1 * time .Minute
125
136
}
@@ -182,8 +193,11 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
182
193
183
194
// nolint:gocritic // Prometheus must collect metrics for all Coder users.
184
195
ctx , cancelFunc := context .WithCancel (dbauthz .AsSystemRestricted (ctx ))
196
+ done := make (chan struct {})
197
+
185
198
ticker := time .NewTicker (duration )
186
199
go func () {
200
+ defer close (done )
187
201
defer ticker .Stop ()
188
202
for {
189
203
select {
@@ -290,7 +304,10 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
290
304
ticker .Reset (duration )
291
305
}
292
306
}()
293
- return cancelFunc , nil
307
+ return func () {
308
+ cancelFunc ()
309
+ <- done
310
+ }, nil
294
311
}
295
312
296
313
func AgentStats (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , initialCreateAfter time.Time , duration time.Duration ) (func (), error ) {
0 commit comments