Skip to content

Commit

Permalink
Enable job embedded metrics export
Browse files Browse the repository at this point in the history
This change enables export of metrics embedded in job logs.
For now, we export those metrics if sections are exported, i.e. if we
fetch the job logs anyway.
  • Loading branch information
cluttrdev committed Dec 4, 2023
1 parent 8066536 commit 4a21cba
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/export_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (c *ExportPipelineConfig) Exec(ctx context.Context, args []string) error {
ExportSections: c.exportSections,
ExportTestReports: c.exportTestReports,
ExportTraces: c.exportTraces,
ExportJobMetrics: c.exportSections, // for now, export metrics if we fetch the logs for sections anyway
}

return tasks.ExportPipelineHierarchy(ctx, opts, &ctl.GitLab, ctl.DataStore)
Expand Down
1 change: 1 addition & 0 deletions internal/worker/catchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (w *catchUpProjectWorker) process(ctx context.Context, pipelineChan <-chan
ExportSections: w.project.Export.Sections.Enabled,
ExportTestReports: w.project.Export.TestReports.Enabled,
ExportTraces: w.project.Export.Traces.Enabled,
ExportJobMetrics: w.project.Export.Sections.Enabled, // for now, export metrics if we fetch the logs for sections anyway
}

if err := tasks.ExportPipelineHierarchy(ctx, opts, w.gitlab, w.datastore); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/worker/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (w *exportProjectWorker) run(ctx context.Context) {
ExportSections: w.project.Export.Sections.Enabled,
ExportTestReports: w.project.Export.TestReports.Enabled,
ExportTraces: w.project.Export.Traces.Enabled,
ExportJobMetrics: w.project.Export.Sections.Enabled, // for now, export metrics if we fetch the logs for sections anyway
}

if err := tasks.ExportPipelineHierarchy(ctx, opts, w.gitlab, w.datastore); err != nil {
Expand Down
77 changes: 68 additions & 9 deletions pkg/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"sync"
"time"

"golang.org/x/time/rate"

Expand Down Expand Up @@ -87,11 +88,13 @@ func (c *Client) CheckReadiness(ctx context.Context) error {
}

type GetPipelineHierarchyOptions struct {
FetchSections bool
FetchSections bool
FetchJobMetrics bool
}

type GetPipelineHierarchyResult struct {
PipelineHierarchy *models.PipelineHierarchy
JobMetrics []*models.JobMetric
Error error
}

Expand All @@ -101,6 +104,12 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
go func() {
defer close(ch)

unixTime := func(ts int64) *time.Time {
const nsec int64 = 0
t := time.Unix(ts, nsec)
return &t
}

pipeline, err := c.GetPipeline(ctx, projectID, pipelineID)
if err != nil {
ch <- GetPipelineHierarchyResult{
Expand All @@ -111,6 +120,7 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe

jobs := []*models.Job{}
sections := []*models.Section{}
metrics := []*models.JobMetric{}
for jr := range c.ListPipelineJobs(ctx, projectID, pipelineID) {
if jr.Error != nil {
ch <- GetPipelineHierarchyResult{
Expand All @@ -120,16 +130,64 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
}
jobs = append(jobs, jr.Job)

if opt.FetchSections {
jobID := jr.Job.ID
for sr := range c.ListJobSections(ctx, projectID, jobID) {
if sr.Error != nil {
ch <- GetPipelineHierarchyResult{
Error: fmt.Errorf("[ListJobSections] %w", sr.Error),
if opt.FetchSections || opt.FetchJobMetrics {
job := jr.Job
r, err := c.GetJobLog(ctx, projectID, job.ID)
if err != nil {
ch <- GetPipelineHierarchyResult{
Error: fmt.Errorf("get job log: %w", err),
}
return
}

data, err := parseJobLog(r)
if err != nil {
ch <- GetPipelineHierarchyResult{
Error: fmt.Errorf("parse job log: %w", err),
}
return
}

if opt.FetchSections {
for secnum, secdat := range data.Sections {
section := &models.Section{
Name: secdat.Name,
StartedAt: unixTime(secdat.Start),
FinishedAt: unixTime(secdat.End),
Duration: float64(secdat.End - secdat.Start),
}
return

section.ID = job.ID*1000 + int64(secnum)
section.Job.ID = int64(job.ID)
section.Job.Name = job.Name
section.Job.Status = job.Status
section.Pipeline.ID = int64(job.Pipeline.ID)
section.Pipeline.ProjectID = int64(job.Pipeline.ProjectID)
section.Pipeline.Ref = job.Pipeline.Ref
section.Pipeline.Sha = job.Pipeline.Sha
section.Pipeline.Status = job.Pipeline.Status

sections = append(sections, section)
}
}

if opt.FetchJobMetrics {
for _, m := range data.Metrics {
metric := &models.JobMetric{
Name: m.Name,
Value: m.Value,
Timestamp: m.TimestampMs,
}

for _, pair := range m.Labels {
metric.Labels[pair.Name] = pair.Value
}

metric.Job.ID = job.ID
metric.Job.Name = job.Name

metrics = append(metrics, metric)
}
sections = append(sections, sr.Section)
}
}
}
Expand Down Expand Up @@ -168,6 +226,7 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
Bridges: bridges,
DownstreamPipelines: dps,
},
JobMetrics: metrics,
}
}()

Expand Down
4 changes: 3 additions & 1 deletion pkg/tasks/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ExportPipelineHierarchyOptions struct {
ExportSections bool
ExportTestReports bool
ExportTraces bool
ExportJobMetrics bool
}

func ExportPipelineHierarchy(ctx context.Context, opts ExportPipelineHierarchyOptions, gl *gitlab.Client, ds datastore.DataStore) error {
Expand All @@ -30,7 +31,8 @@ func exportPipelineHierarchy(ctx context.Context, opts ExportPipelineHierarchyOp
defer close(out)

opt := &gitlab.GetPipelineHierarchyOptions{
FetchSections: opts.ExportSections,
FetchSections: opts.ExportSections,
FetchJobMetrics: opts.ExportSections,
}

phr := <-gl.GetPipelineHierarchy(ctx, opts.ProjectID, opts.PipelineID, opt)
Expand Down

0 comments on commit 4a21cba

Please sign in to comment.