@@ -22,6 +22,12 @@ import (
22
22
"github.com/coder/quartz"
23
23
)
24
24
25
+ type metricCheck struct {
26
+ name string
27
+ value * float64
28
+ isCounter bool
29
+ }
30
+
25
31
func TestMetricsCollector (t * testing.T ) {
26
32
t .Parallel ()
27
33
@@ -35,9 +41,12 @@ func TestMetricsCollector(t *testing.T) {
35
41
jobStatuses []database.ProvisionerJobStatus
36
42
initiatorIDs []uuid.UUID
37
43
ownerIDs []uuid.UUID
38
- shouldIncrementPrebuildsCreated * bool
39
- shouldIncrementPrebuildsFailed * bool
40
- shouldIncrementPrebuildsAssigned * bool
44
+ shouldIncrementPrebuildsCreated * float64
45
+ shouldIncrementPrebuildsFailed * float64
46
+ shouldIncrementPrebuildsAssigned * float64
47
+ shouldSetDesiredPrebuilds * float64
48
+ shouldSetActualPrebuilds * float64
49
+ shouldSetEligiblePrebuilds * float64
41
50
}
42
51
43
52
tests := []testCase {
@@ -50,35 +59,57 @@ func TestMetricsCollector(t *testing.T) {
50
59
jobStatuses : allJobStatuses ,
51
60
initiatorIDs : []uuid.UUID {prebuilds .OwnerID },
52
61
ownerIDs : []uuid.UUID {prebuilds .OwnerID , uuid .New ()},
53
- shouldIncrementPrebuildsCreated : ptr .To (true ),
62
+ shouldIncrementPrebuildsCreated : ptr .To (1.0 ),
63
+ shouldSetDesiredPrebuilds : ptr .To (1.0 ),
64
+ shouldSetEligiblePrebuilds : ptr .To (0.0 ),
65
+ },
66
+ {
67
+ name : "prebuild running" ,
68
+ transitions : []database.WorkspaceTransition {database .WorkspaceTransitionStart },
69
+ jobStatuses : []database.ProvisionerJobStatus {database .ProvisionerJobStatusSucceeded },
70
+ initiatorIDs : []uuid.UUID {prebuilds .OwnerID },
71
+ ownerIDs : []uuid.UUID {prebuilds .OwnerID },
72
+ shouldIncrementPrebuildsCreated : ptr .To (1.0 ),
73
+ shouldSetDesiredPrebuilds : ptr .To (1.0 ),
74
+ shouldSetActualPrebuilds : ptr .To (1.0 ),
75
+ shouldSetEligiblePrebuilds : ptr .To (0.0 ),
54
76
},
55
77
{
56
78
name : "prebuild failed" ,
57
79
transitions : allTransitions ,
58
80
jobStatuses : []database.ProvisionerJobStatus {database .ProvisionerJobStatusFailed },
59
81
initiatorIDs : []uuid.UUID {prebuilds .OwnerID },
60
82
ownerIDs : []uuid.UUID {prebuilds .OwnerID , uuid .New ()},
61
- shouldIncrementPrebuildsCreated : ptr .To (true ),
62
- shouldIncrementPrebuildsFailed : ptr .To (true ),
83
+ shouldIncrementPrebuildsCreated : ptr .To (1.0 ),
84
+ shouldIncrementPrebuildsFailed : ptr .To (1.0 ),
85
+ shouldSetDesiredPrebuilds : ptr .To (1.0 ),
86
+ shouldSetActualPrebuilds : ptr .To (0.0 ),
87
+ shouldSetEligiblePrebuilds : ptr .To (0.0 ),
63
88
},
64
89
{
65
90
name : "prebuild assigned" ,
66
91
transitions : allTransitions ,
67
92
jobStatuses : allJobStatuses ,
68
93
initiatorIDs : []uuid.UUID {prebuilds .OwnerID },
69
94
ownerIDs : []uuid.UUID {uuid .New ()},
70
- shouldIncrementPrebuildsCreated : ptr .To (true ),
71
- shouldIncrementPrebuildsAssigned : ptr .To (true ),
95
+ shouldIncrementPrebuildsCreated : ptr .To (1.0 ),
96
+ shouldIncrementPrebuildsAssigned : ptr .To (1.0 ),
97
+ shouldSetDesiredPrebuilds : ptr .To (1.0 ),
98
+ shouldSetActualPrebuilds : ptr .To (0.0 ),
99
+ shouldSetEligiblePrebuilds : ptr .To (0.0 ),
72
100
},
73
101
{
74
102
name : "workspaces that were not created by the prebuilds user are not counted" ,
75
103
transitions : allTransitions ,
76
104
jobStatuses : allJobStatuses ,
77
105
initiatorIDs : []uuid.UUID {uuid .New ()},
78
106
ownerIDs : []uuid.UUID {uuid .New ()},
79
- shouldIncrementPrebuildsCreated : ptr .To (false ),
80
- shouldIncrementPrebuildsFailed : ptr .To (false ),
81
- shouldIncrementPrebuildsAssigned : ptr .To (false ),
107
+ shouldIncrementPrebuildsCreated : nil ,
108
+ shouldIncrementPrebuildsFailed : nil ,
109
+ shouldIncrementPrebuildsAssigned : nil ,
110
+ shouldSetDesiredPrebuilds : ptr .To (1.0 ),
111
+ shouldSetActualPrebuilds : ptr .To (0.0 ),
112
+ shouldSetEligiblePrebuilds : ptr .To (0.0 ),
82
113
},
83
114
}
84
115
for _ , test := range tests {
@@ -121,7 +152,7 @@ func TestMetricsCollector(t *testing.T) {
121
152
for i := 0 ; i < numTemplates ; i ++ {
122
153
orgID , templateID := setupTestDBTemplate (t , db , ownerID )
123
154
templateVersionID := setupTestDBTemplateVersion (t , ctx , db , pubsub , orgID , ownerID , templateID )
124
- preset := setupTestDBPreset (t , ctx , db , pubsub , templateVersionID , 1 )
155
+ preset := setupTestDBPreset (t , ctx , db , pubsub , templateVersionID , 1 , uuid . New (). String () )
125
156
setupTestDBPrebuild (
126
157
t , ctx , db , pubsub ,
127
158
transition , jobStatus , orgID , templateID , templateVersionID , preset .ID , initiatorID , ownerID ,
@@ -147,42 +178,31 @@ func TestMetricsCollector(t *testing.T) {
147
178
require .Equal (t , 1 , len (presets ))
148
179
149
180
for _ , preset := range presets {
150
- if test .shouldIncrementPrebuildsCreated != nil {
151
- metric := findMetric (metricsFamilies , "coderd_prebuilds_created" , map [string ]string {
152
- "template_name" : template .Name ,
153
- "preset_name" : preset .Name ,
154
- })
155
- if * test .shouldIncrementPrebuildsCreated {
156
- require .NotNil (t , metric )
157
- require .Equal (t , metric .GetCounter ().GetValue (), 1.0 )
158
- } else {
159
- require .Nil (t , metric )
160
- }
181
+ checks := []metricCheck {
182
+ {"coderd_prebuilds_created" , test .shouldIncrementPrebuildsCreated , true },
183
+ {"coderd_prebuilds_failed" , test .shouldIncrementPrebuildsFailed , true },
184
+ {"coderd_prebuilds_assigned" , test .shouldIncrementPrebuildsAssigned , true },
185
+ {"coderd_prebuilds_desired" , test .shouldSetDesiredPrebuilds , false },
186
+ {"coderd_prebuilds_actual" , test .shouldSetActualPrebuilds , false },
187
+ {"coderd_prebuilds_eligible" , test .shouldSetEligiblePrebuilds , false },
161
188
}
162
189
163
- if test .shouldIncrementPrebuildsFailed != nil {
164
- metric := findMetric (metricsFamilies , "coderd_prebuilds_failed" , map [string ]string {
165
- "template_name" : template .Name ,
166
- "preset_name" : preset .Name ,
167
- })
168
- if * test .shouldIncrementPrebuildsFailed {
169
- require .NotNil (t , metric )
170
- require .Equal (t , metric .GetCounter ().GetValue (), 1.0 )
171
- } else {
172
- require .Nil (t , metric )
173
- }
190
+ labels := map [string ]string {
191
+ "template_name" : template .Name ,
192
+ "preset_name" : preset .Name ,
174
193
}
175
194
176
- if test .shouldIncrementPrebuildsAssigned != nil {
177
- metric := findMetric (metricsFamilies , "coderd_prebuilds_assigned" , map [string ]string {
178
- "template_name" : template .Name ,
179
- "preset_name" : preset .Name ,
180
- })
181
- if * test .shouldIncrementPrebuildsAssigned {
182
- require .NotNil (t , metric )
183
- require .Equal (t , metric .GetCounter ().GetValue (), 1.0 )
195
+ for _ , check := range checks {
196
+ metric := findMetric (metricsFamilies , check .name , labels )
197
+ if check .value == nil {
198
+ continue
199
+ }
200
+
201
+ require .NotNil (t , metric , "metric %s should exist" , check .name )
202
+ if check .isCounter {
203
+ require .Equal (t , * check .value , metric .GetCounter ().GetValue (), "counter %s value mismatch" , check .name )
184
204
} else {
185
- require .Nil (t , metric )
205
+ require .Equal (t , * check . value , metric . GetGauge (). GetValue (), "gauge %s value mismatch" , check . name )
186
206
}
187
207
}
188
208
}
0 commit comments