8000 feat(api): expose started and failed workflow runs metrics (#3373) · ovh/cds@b758517 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit b758517

Browse files
fsaminrichardlt
authored andcommitted
feat(api): expose started and failed workflow runs metrics (#3373)
1 parent fcfb482 commit b758517

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

engine/api/api.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ type API struct {
226226
warnChan chan sdk.Event
227227
Cache cache.Store
228228
Stats struct {
229-
WorkflowRuns *stats.Int64Measure
230-
Sessions *stats.Int64Measure
229+
WorkflowRunFailed *stats.Int64Measure
230+
WorkflowRunStarted *stats.Int64Measure
231+
Sessions *stats.Int64Measure
231232
}
232233
}
233234

@@ -739,16 +740,25 @@ func (a *API) Serve(ctx context.Context) error {
739740
}
740741

741742
func (a *API) initStats() error {
742-
label := fmt.Sprintf("cds/cds-api/%s/workflow_runs", a.Name)
743-
a.Stats.WorkflowRuns = stats.Int64(label, "number of workflow runs", stats.UnitDimensionless)
743+
label := fmt.Sprintf("cds/cds-api/%s/workflow_runs_started", a.Name)
744+
a.Stats.WorkflowRunStarted = stats.Int64(label, "number of started workflow runs", stats.UnitDimensionless)
745+
746+
label = fmt.Sprintf("cds/cds-api/%s/workflow_runs_failed", a.Name)
747+
a.Stats.WorkflowRunFailed = stats.Int64(label, "number of failed workflow runs", stats.UnitDimensionless)
744748

745749
log.Info("api> Stats initialized")
746750

747751
return observability.RegisterView(
748752
&view.View{
749-
Name: "workflow_runs",
750-
Description: a.Stats.WorkflowRuns.Description(),
751-
Measure: a.Stats.WorkflowRuns,
753+
Name: "workflow_runs_started",
754+
Description: a.Stats.WorkflowRunStarted.Description(),
755+
Measure: a.Stats.WorkflowRunStarted,
756+
Aggregation: view.Count(),
757+
},
758+
&view.View{
759+
Name: "workflow_runs_failed",
760+
Description: a.Stats.WorkflowRunFailed.Description(),
761+
Measure: a.Stats.WorkflowRunFailed,
752762
Aggregation: view.Count(),
753763
},
754764
)

engine/api/observability/stats.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func RegisterView(views ...*view.View) error {
2424
// StatsHandler returns a Handler to exposer prometheus views
2525
func StatsHandler() service.Handler {
2626
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
27+
if statsExporter == nil {
28+
return nil
29+
}
2730
statsExporter.ServeHTTP(w, r)
2831
return nil
2932
}

engine/api/workflow_queue.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,16 @@ func (api *API) postWorkflowJobResultHandler() service.Handler {
418418
return sdk.WrapError(err, "postWorkflowJobResultHandler> unable to post job result")
419419
}
420420

421+
observability.Record(ctx, api.Stats.WorkflowRunStarted, 1)
421422
workflowRuns, workflowNodeRuns := workflow.GetWorkflowRunEventData(report, proj.Key)
422423

423424
if len(workflowRuns) > 0 {
424425
observability.Current(ctx,
425-
observability.Tag(observability.TagWorkflow, workflowRuns[0].Workflow.Name),
426-
)
426+
observability.Tag(observability.TagWorkflow, workflowRuns[0].Workflow.Name))
427+
428+
if workflowRuns[0].Status == sdk.StatusFail.String() {
429+
observability.Record(ctx, api.Stats.WorkflowRunFailed, 1)
< ED4F /code>
430+
}
427431
}
428432

429433
db := api.mustDB()

engine/api/workflow_run.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ func (api *API) stopWorkflowRunHandler() service.Handler {
331331
workflowRuns, workflowNodeRuns := workflow.GetWorkflowRunEventData(report, proj.Key)
332332
go workflow.SendEvent(api.mustDB(), workflowRuns, workflowNodeRuns, proj.Key)
333333

334+
if len(workflowRuns) > 0 {
335+
observability.Current(ctx,
336+
observability.Tag(observability.TagProjectKey, proj.Key),
337+
observability.Tag(observability.TagWorkflow, workflowRuns[0].Workflow.Name),
338+
)
339+
340+
if workflowRuns[0].Status == sdk.StatusFail.String() {
341+
observability.Record(ctx, api.Stats.WorkflowRunFailed, 1)
342+
}
343+
}
344+
334345
return service.WriteJSON(w, run, http.StatusOK)
335346
}
336347
}
@@ -519,7 +530,7 @@ func (api *API) stopWorkflowNodeRunHandler() service.Handler {
519530
return sdk.WrapError(err, "stopWorkflowNodeRunHandler> Unable to load last workflow run")
520531
}
521532

522-
report, err := stopWorkflowNodeRun(ctx, api.mustDB, api.Cache, p, nodeRun, name, getUser(ctx))
533+
report, err := api.stopWorkflowNodeRun(ctx, api.mustDB, api.Cache, p, nodeRun, name, getUser(ctx))
523534
if err != nil {
524535
return sdk.WrapError(err, "stopWorkflowNodeRunHandler> Unable to stop workflow run")
525536
}
@@ -531,7 +542,7 @@ func (api *API) stopWorkflowNodeRunHandler() service.Handler {
531542
}
532543
}
533544

534-
func stopWorkflowNodeRun(ctx context.Context, dbFunc func() *gorp.DbMap, store cache.Store, p *sdk.Pr 1E79 oject, nodeRun *sdk.WorkflowNodeRun, workflowName string, u *sdk.User) (*workflow.ProcessorReport, error) {
545+
func (api *API) stopWorkflowNodeRun(ctx context.Context, dbFunc func() *gorp.DbMap, store cache.Store, p *sdk.Project, nodeRun *sdk.WorkflowNodeRun, workflowName string, u *sdk.User) (*workflow.ProcessorReport, error) {
535546
tx, errTx := dbFunc().Begin()
536547
if errTx != nil {
537548
return nil, sdk.WrapError(errTx, "stopWorkflowNodeRunHandler> Unable to create transaction")
@@ -560,6 +571,14 @@ func stopWorkflowNodeRun(ctx context.Context, dbFunc func() *gorp.DbMap, store c
560571

561572
_, _ = report.Merge(r1, nil)
562573

574+
observability.Current(ctx,
575+
observability.Tag(observability.TagProjectKey, p.Key),
576+
observability.Tag(observability.TagWorkflow, wr.Workflow.Name),
577+
)
578+
if wr.Status == sdk.StatusFail.String() {
579+
observability.Record(ctx, api.Stats.WorkflowRunFailed, 1)
580+
}
581+
563582
if errC := tx.Commit(); errC != nil {
564583
return nil, sdk.WrapError(errC, "stopWorkflowNodeRunHandler> Unable to commit")
565584
}
@@ -597,8 +616,11 @@ func (api *API) postWorkflowRunHandler() service.Handler {
597616
name := vars["permWorkflowName"]
598617
u := getUser(ctx)
599618

600-
observability.Current(ctx, observability.Tag(observability.TagWorkflow, name))
601-
observability.Record(ctx, api.Stats.WorkflowRuns, 1)
619+
observability.Current(ctx,
620+
observability.Tag(observability.TagProjectKey, key),
621+
observability.Tag(observability.TagWorkflow, name),
622+
)
623+
observability.Record(ctx, api.Stats.WorkflowRunStarted, 1)
602624

603625
_, next := observability.Span(ctx, "project.Load")
604626
p, errP := project.Load(api.mustDB(), api.Cache, key, u,

0 commit comments

Comments
 (0)
0