Skip to content

Commit

Permalink
pipelines: store the source of the pipeline as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mathstuf committed Sep 30, 2023
1 parent 484359a commit 203a7e6
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 40 deletions.
61 changes: 33 additions & 28 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@
| `gitlab_ci_environment_deployment_status` | Status of the most recent deployment of the environment | [project], [environment], [status] | `project_defaults.pull.environments.enabled` |
| `gitlab_ci_environment_deployment_timestamp` | Creation date of the most recent deployment of the environment | [project], [environment] | `project_defaults.pull.environments.enabled` |
| `gitlab_ci_environment_information` | Information about the environment | [project], [environment], [environment_id], [external_url], [kind], [ref], [latest_commit_short_id], [current_commit_short_id], [available], [username] | `project_defaults.pull.environments.enabled` |
| `gitlab_ci_pipeline_coverage` | Coverage of the most recent pipeline | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_duration_seconds` | Duration in seconds of the most recent pipeline | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_id` | ID of the most recent pipeline | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_job_artifact_size_bytes` | Artifact size in bytes (sum of all of them) of the most recent job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_duration_seconds` | Duration in seconds of the most recent job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_id` | ID of the most recent job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_queued_duration_seconds` | Duration in seconds the most recent job has been queued before starting | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_run_count` | Number of executions of a job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_status` | Status of the most recent job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list], [status] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_timestamp` | Creation date timestamp of the the most recent job | [project], [topics], [ref], [runner_description], [kind], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_queued_duration_seconds` | Duration in seconds the most recent pipeline has been queued before starting | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_run_count` | Number of executions of a pipeline | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_status` | Status of the most recent pipeline | [project], [topics], [ref], [kind], [variables], [status] | *available by default* |
| `gitlab_ci_pipeline_timestamp` | Timestamp of the last update of the most recent pipeline | [project], [topics], [ref], [kind], [variables] | *available by default* |
| `gitlab_ci_pipeline_test_report_total_time` | Duration in seconds of all the tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_total_count` | Number of total tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_success_count` | Number of successful tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_failed_count` | Number of failed tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_skipped_count` | Number of skipped tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_error_count` | Number of errored tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_total_time` | Duration in seconds for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_total_count` | Number of total tests for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_success_count` | Number of successful tests for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_failed_count` | Number of failed tests for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_skipped_count` | Number of skipped tests for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_error_count` | Duration in errored tests for the test suite | [project], [topics], [ref], [kind], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_case_execution_time` | Duration in seconds for the test case | [project], [topics], [ref], [kind], [variables], [test_suite_name], [test_case_name], [test_case_classname] | `project_defaults.pull.pipeline.test_reports.test_cases.enabled` |
| `gitlab_ci_pipeline_test_case_status` | Status of the most recent test case | [project], [topics], [ref], [kind], [variables], [test_suite_name], [test_case_name], [test_case_classname], [status] | `project_defaults.pull.pipeline.test_reports.test_cases.enabled` |
| `gitlab_ci_pipeline_coverage` | Coverage of the most recent pipeline | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_duration_seconds` | Duration in seconds of the most recent pipeline | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_id` | ID of the most recent pipeline | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_job_artifact_size_bytes` | Artifact size in bytes (sum of all of them) of the most recent job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_duration_seconds` | Duration in seconds of the most recent job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_id` | ID of the most recent job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_queued_duration_seconds` | Duration in seconds the most recent job has been queued before starting | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_run_count` | Number of executions of a job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_status` | Status of the most recent job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list], [status] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_job_timestamp` | Creation date timestamp of the the most recent job | [project], [topics], [ref], [runner_description], [kind], [source], [variables], [stage], [job_name], [tag_list] | `project_defaults.pull.pipeline.jobs.enabled` |
| `gitlab_ci_pipeline_queued_duration_seconds` | Duration in seconds the most recent pipeline has been queued before starting | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_run_count` | Number of executions of a pipeline | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_status` | Status of the most recent pipeline | [project], [topics], [ref], [kind], [source], [variables], [status] | *available by default* |
| `gitlab_ci_pipeline_timestamp` | Timestamp of the last update of the most recent pipeline | [project], [topics], [ref], [kind], [source], [variables] | *available by default* |
| `gitlab_ci_pipeline_test_report_total_time` | Duration in seconds of all the tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_total_count` | Number of total tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_success_count` | Number of successful tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_failed_count` | Number of failed tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_skipped_count` | Number of skipped tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_report_error_count` | Number of errored tests in the most recently finished pipeline | [project], [topics], [ref], [kind], [source], [variables] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_total_time` | Duration in seconds for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_total_count` | Number of total tests for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_success_count` | Number of successful tests for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_failed_count` | Number of failed tests for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_skipped_count` | Number of skipped tests for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_suite_error_count` | Duration in errored tests for the test suite | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name] | `project_defaults.pull.pipeline.test_reports.enabled` |
| `gitlab_ci_pipeline_test_case_execution_time` | Duration in seconds for the test case | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name], [test_case_name], [test_case_classname] | `project_defaults.pull.pipeline.test_reports.test_cases.enabled` |
| `gitlab_ci_pipeline_test_case_status` | Status of the most recent test case | [project], [topics], [ref], [kind], [source], [variables], [test_suite_name], [test_case_name], [test_case_classname], [status] | `project_defaults.pull.pipeline.test_reports.test_cases.enabled` |

## Labels

Expand All @@ -72,6 +72,10 @@ Description of the runner on which the most recent job ran

Type of the ref used by the pipeline. Can be either **branch**, **tag** or **merge_request**

### Source

The reason the pipeline exists.

### Variables

User defined variables for the pipelines.
Expand Down Expand Up @@ -176,6 +180,7 @@ This flag affect every `_status$` metrics:
[status]: #status
[topics]: #topics
[username]: #username
[source]: #source
[variables]: #variables
[test_suite_name]: #test-suite-name
[test_case_name]: #test-case-name
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller
import "github.com/prometheus/client_golang/prometheus"

var (
defaultLabels = []string{"project", "topics", "kind", "ref", "variables"}
defaultLabels = []string{"project", "topics", "kind", "ref", "source", "variables"}
jobLabels = []string{"stage", "job_name", "runner_description", "tag_list"}
statusLabels = []string{"status"}
environmentLabels = []string{"project", "environment"}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestProcessJobMetrics(t *testing.T) {
"ref": ref.Name,
"kind": string(ref.Kind),
"variables": ref.LatestPipeline.Variables,
"source": ref.LatestPipeline.Source,
"stage": newJob.Stage,
"tag_list": newJob.TagList,
"job_name": newJob.Name,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestExportMetrics(_ *testing.T) {
"topics": "alpha",
"ref": "bar",
"kind": "branch",
"source": "schedule",
"variables": "beta",
},
Value: float64(107.7),
Expand All @@ -60,6 +61,7 @@ func TestExportMetrics(_ *testing.T) {
"topics": "alpha",
"ref": "bar",
"kind": "branch",
"source": "schedule",
"variables": "beta",
},
Value: float64(10),
Expand Down
16 changes: 9 additions & 7 deletions pkg/controller/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ func (c *Controller) PullRefMetrics(ctx context.Context, ref schemas.Ref) error
return err
}

labels := ref.DefaultLabelsValues()

// If the metric does not exist yet, start with 0 instead of 1
// this could cause some false positives in prometheus
// when restarting the exporter otherwise
runCount := schemas.Metric{
Kind: schemas.MetricKindRunCount,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
}

storeGetMetric(ctx, c.Store, &runCount)
Expand All @@ -99,41 +101,41 @@ func (c *Controller) PullRefMetrics(ctx context.Context, ref schemas.Ref) error

storeSetMetric(ctx, c.Store, schemas.Metric{
Kind: schemas.MetricKindCoverage,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
Value: pipeline.Coverage,
})

storeSetMetric(ctx, c.Store, schemas.Metric{
Kind: schemas.MetricKindID,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
Value: float64(pipeline.ID),
})

emitStatusMetric(
ctx,
c.Store,
schemas.MetricKindStatus,
ref.DefaultLabelsValues(),
labels,
statusesList[:],
pipeline.Status,
ref.Project.OutputSparseStatusMetrics,
)

storeSetMetric(ctx, c.Store, schemas.Metric{
Kind: schemas.MetricKindDurationSeconds,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
Value: pipeline.DurationSeconds,
})

storeSetMetric(ctx, c.Store, schemas.Metric{
Kind: schemas.MetricKindQueuedDurationSeconds,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
Value: pipeline.QueuedDurationSeconds,
})

storeSetMetric(ctx, c.Store, schemas.Metric{
Kind: schemas.MetricKindTimestamp,
Labels: ref.DefaultLabelsValues(),
Labels: labels,
Value: pipeline.Timestamp,
})

Expand Down
8 changes: 5 additions & 3 deletions pkg/controller/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestPullRefMetricsSucceed(t *testing.T) {
mux.HandleFunc("/api/v4/projects/foo/pipelines/1",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"id":1,"created_at":"2016-08-11T11:27:00.085Z", "started_at":"2016-08-11T11:28:00.085Z",
"duration":300,"queued_duration":60,"status":"running","coverage":"30.2"}`)
"duration":300,"queued_duration":60,"status":"running","coverage":"30.2","source":"schedule"}`)
})

mux.HandleFunc("/api/v4/projects/foo/pipelines/1/variables",
Expand Down Expand Up @@ -61,6 +61,7 @@ func TestPullRefMetricsSucceed(t *testing.T) {
"ref": "bar",
"topics": "",
"variables": "foo:bar",
"source": "schedule",
}

runCount := schemas.Metric{
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestPullRefTestReportMetrics(t *testing.T) {
mux.HandleFunc("/api/v4/projects/foo/pipelines/1",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"id":1,"created_at":"2016-08-11T11:27:00.085Z", "started_at":"2016-08-11T11:28:00.085Z",
"duration":300,"queued_duration":60,"status":"success","coverage":"30.2"}`)
"duration":300,"queued_duration":60,"status":"success","coverage":"30.2","source":"schedule"}`)
})

mux.HandleFunc("/api/v4/projects/foo/pipelines/1/variables",
Expand Down Expand Up @@ -150,6 +151,7 @@ func TestPullRefTestReportMetrics(t *testing.T) {
"ref": "bar",
"topics": "",
"variables": "foo:bar",
"source": "schedule",
}

trTotalTime := schemas.Metric{
Expand Down Expand Up @@ -269,7 +271,7 @@ func TestPullRefMetricsMergeRequestPipeline(t *testing.T) {

mux.HandleFunc("/api/v4/projects/foo/pipelines/1",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"id":1,"updated_at":"2016-08-11T11:28:34.085Z","duration":300,"status":"running","coverage":"30.2"}`)
fmt.Fprint(w, `{"id":1,"updated_at":"2016-08-11T11:28:34.085Z","duration":300,"status":"running","coverage":"30.2","source":"schedule"}`)
})

mux.HandleFunc(fmt.Sprintf("/api/v4/projects/foo/pipelines/1/variables"),
Expand Down
1 change: 1 addition & 0 deletions pkg/schemas/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (m Metric) Key() MetricKey {
m.Labels["project"],
m.Labels["kind"],
m.Labels["ref"],
m.Labels["source"],
})

case MetricKindJobArtifactSizeBytes, MetricKindJobDurationSeconds, MetricKindJobID, MetricKindJobQueuedDurationSeconds, MetricKindJobRunCount, MetricKindJobStatus, MetricKindJobTimestamp:
Expand Down
2 changes: 1 addition & 1 deletion pkg/schemas/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestMetricKey(t *testing.T) {
assert.Equal(t, MetricKey("3273426995"), Metric{
assert.Equal(t, MetricKey("3797596385"), Metric{
Kind: MetricKindCoverage,
Labels: prometheus.Labels{
"foo": "bar",
Expand Down
2 changes: 2 additions & 0 deletions pkg/schemas/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Pipeline struct {
Timestamp float64
DurationSeconds float64
QueuedDurationSeconds float64
Source string
Status string
Variables string
TestReport TestReport
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewPipeline(ctx context.Context, gp goGitlab.Pipeline) Pipeline {
Timestamp: timestamp,
DurationSeconds: float64(gp.Duration),
QueuedDurationSeconds: float64(gp.QueuedDuration),
Source: gp.Source,
Status: gp.Status,
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/schemas/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestNewPipeline(t *testing.T) {
UpdatedAt: &updatedAt,
Duration: 15,
QueuedDuration: 5,
Source: "schedule",
Status: "running",
}

Expand All @@ -31,6 +32,7 @@ func TestNewPipeline(t *testing.T) {
Timestamp: 1.60155755e+09,
DurationSeconds: 15,
QueuedDurationSeconds: 5,
Source: "schedule",
Status: "running",
}

Expand Down
Loading

0 comments on commit 203a7e6

Please sign in to comment.