Skip to content

Commit 780efa3

Browse files
Fix queued time observation (#225)
* Fix queued time observation Signed-off-by: Marcel Kirsche <[email protected]> * Fix linter errors Signed-off-by: Marcel Kirsche <[email protected]> --------- Signed-off-by: Marcel Kirsche <[email protected]>
1 parent df497f1 commit 780efa3

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

internal/server/workflow_metrics_exporter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ func (c *WorkflowMetricsExporter) CollectWorkflowJobEvent(event *github.Workflow
120120
break
121121
}
122122

123+
if len(workflowJob.Steps) > 1 {
124+
// If there are more than one steps, we are receiving an update of an already running job.
125+
// Don't count the queued time again since it's already running.
126+
break
127+
}
128+
123129
firstStep := workflowJob.Steps[0]
124130
queuedSeconds := firstStep.StartedAt.Time.Sub(workflowJob.GetStartedAt().Time).Seconds()
125131
c.PrometheusObserver.ObserveWorkflowJobDuration(org, repo, branch, "queued", runnerGroup, workflowName, jobName, math.Max(0, queuedSeconds))

internal/server/workflow_metrics_exporter_test.go

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Test_GHActionExporter_HandleGHWebHook_WorkflowJobQueuedEvent(t *testing.T)
165165
}, 50*time.Millisecond)
166166
}
167167

168-
func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEvent(t *testing.T) {
168+
func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEventFirstStep(t *testing.T) {
169169
// Given
170170
observer := NewTestPrometheusObserver(t)
171171
subject := server.WorkflowMetricsExporter{
@@ -184,7 +184,7 @@ func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEvent(t *testing
184184
stepStartedAt := jobStartedAt.Add(time.Duration(expectedDuration) * time.Second)
185185
runnerGroupName := "runner-group"
186186
action := "in_progress"
187-
status := "in_progress"
187+
statusInProgress := "in_progress"
188188
workflowName := "Build and test"
189189
jobName := "Test"
190190

@@ -198,14 +198,12 @@ func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEvent(t *testing
198198
},
199199
WorkflowJob: &github.WorkflowJob{
200200
HeadBranch: &branch,
201-
Status: &status,
201+
Status: &statusInProgress,
202202
StartedAt: &github.Timestamp{Time: jobStartedAt},
203203
Steps: []*github.TaskStep{
204204
{
205205
StartedAt: &github.Timestamp{Time: stepStartedAt},
206-
},
207-
{
208-
StartedAt: &github.Timestamp{Time: stepStartedAt.Add(5 * time.Second)},
206+
Status: &statusInProgress,
209207
},
210208
},
211209
RunnerGroupName: &runnerGroupName,
@@ -243,6 +241,78 @@ func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEvent(t *testing
243241
}, 50*time.Millisecond)
244242
}
245243

244+
func Test_GHActionExporter_HandleGHWebHook_WorkflowJobInProgressEventSecondStep(t *testing.T) {
245+
// Given
246+
observer := NewTestPrometheusObserver(t)
247+
subject := server.WorkflowMetricsExporter{
248+
Logger: log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout)),
249+
Opts: server.Opts{
250+
GitHubToken: webhookSecret,
251+
},
252+
PrometheusObserver: observer,
253+
}
254+
255+
repo := "some-repo"
256+
org := "someone"
257+
branch := "some-branch"
258+
expectedDuration := 10.0
259+
jobStartedAt := time.Unix(1650308740, 0)
260+
stepStartedAt := jobStartedAt.Add(time.Duration(expectedDuration) * time.Second)
261+
runnerGroupName := "runner-group"
262+
action := "in_progress"
263+
statusInProgress := "in_progress"
264+
statusCompleted := "completed"
265+
workflowName := "Build and test"
266+
jobName := "Test"
267+
268+
event := github.WorkflowJobEvent{
269+
Action: &action,
270+
Repo: &github.Repository{
271+
Name: &repo,
272+
Owner: &github.User{
273+
Login: &org,
274+
},
275+
},
276+
WorkflowJob: &github.WorkflowJob{
277+
HeadBranch: &branch,
278+
Status: &statusInProgress,
279+
StartedAt: &github.Timestamp{Time: jobStartedAt},
280+
Steps: []*github.TaskStep{
281+
{
282+
StartedAt: &github.Timestamp{Time: stepStartedAt},
283+
Status: &statusCompleted,
284+
},
285+
{
286+
StartedAt: &github.Timestamp{Time: stepStartedAt.Add(5 * time.Second)},
287+
Status: &statusInProgress,
288+
},
289+
},
290+
RunnerGroupName: &runnerGroupName,
291+
WorkflowName: &workflowName,
292+
Name: &jobName,
293+
},
294+
}
295+
req := testWebhookRequest(t, "/anything", "workflow_job", event)
296+
297+
// When
298+
res := httptest.NewRecorder()
299+
subject.HandleGHWebHook(res, req)
300+
301+
// Then
302+
assert.Equal(t, http.StatusAccepted, res.Result().StatusCode)
303+
observer.assertNoWorkflowJobDurationObservation(50 * time.Millisecond)
304+
observer.assertWorkflowJobStatusCount(workflowJobStatusCount{
305+
org: org,
306+
repo: repo,
307+
branch: branch,
308+
runnerGroup: runnerGroupName,
309+
status: action,
310+
conclusion: "",
311+
workflowName: workflowName,
312+
jobName: jobName,
313+
}, 50*time.Millisecond)
314+
}
315+
246316
func Test_WorkflowMetricsExporter_HandleGHWebHook_WorkflowJobInProgressEventWithNegativeDuration(t *testing.T) {
247317
// Given
248318
observer := NewTestPrometheusObserver(t)
@@ -282,9 +352,6 @@ func Test_WorkflowMetricsExporter_HandleGHWebHook_WorkflowJobInProgressEventWith
282352
{
283353
StartedAt: &github.Timestamp{Time: stepStartedAt},
284354
},
285-
{
286-
StartedAt: &github.Timestamp{Time: stepStartedAt.Add(5 * time.Second)},
287-
},
288355
},
289356
RunnerGroupName: &runnerGroupName,
290357
WorkflowName: &workflowName,

0 commit comments

Comments
 (0)