Commit aff71ba6 authored by Fabio Pitino's avatar Fabio Pitino Committed by Bob Van Landuyt

Compare live vs actual CI minutes consumption

parent 7f637bd8
...@@ -130,6 +130,7 @@ The following metrics are available: ...@@ -130,6 +130,7 @@ The following metrics are available:
| `pipeline_graph_links_total` | Histogram | 13.9 | Number of links per graph | | | `pipeline_graph_links_total` | Histogram | 13.9 | Number of links per graph | |
| `pipeline_graph_links_per_job_ratio` | Histogram | 13.9 | Ratio of links to job per graph | | | `pipeline_graph_links_per_job_ratio` | Histogram | 13.9 | Ratio of links to job per graph | |
| `gitlab_ci_pipeline_security_orchestration_policy_processing_duration_seconds` | Histogram | 13.12 | Time in seconds it takes to process Security Policies in CI/CD pipeline | | | `gitlab_ci_pipeline_security_orchestration_policy_processing_duration_seconds` | Histogram | 13.12 | Time in seconds it takes to process Security Policies in CI/CD pipeline | |
| `gitlab_ci_difference_live_vs_actual_minutes` | Histogram | 13.12 | Difference between CI minute consumption counted while jobs were running (live) vs when jobs are complete (actual). Used to enforce CI minute consumption limits on long running jobs. | `plan` |
## Metrics controlled by a feature flag ## Metrics controlled by a feature flag
......
...@@ -16,6 +16,8 @@ module Ci ...@@ -16,6 +16,8 @@ module Ci
legacy_track_usage_of_monthly_minutes(consumption_in_seconds) legacy_track_usage_of_monthly_minutes(consumption_in_seconds)
track_usage_of_monthly_minutes(consumption) track_usage_of_monthly_minutes(consumption)
compare_with_live_consumption(build, consumption)
end end
private private
...@@ -40,6 +42,14 @@ module Ci ...@@ -40,6 +42,14 @@ module Ci
end end
end end
def compare_with_live_consumption(build, consumption)
live_consumption = ::Ci::Minutes::TrackLiveConsumptionService.new(build).live_consumption
return if live_consumption == 0
difference = consumption.to_f - live_consumption.to_f
observe_ci_minutes_difference(difference, plan: namespace.actual_plan_name)
end
def namespace_statistics def namespace_statistics
namespace.namespace_statistics || namespace.create_namespace_statistics namespace.namespace_statistics || namespace.create_namespace_statistics
end end
...@@ -51,6 +61,12 @@ module Ci ...@@ -51,6 +61,12 @@ module Ci
def namespace def namespace
project.shared_runners_limit_namespace project.shared_runners_limit_namespace
end end
def observe_ci_minutes_difference(difference, plan:)
::Gitlab::Ci::Pipeline::Metrics
.gitlab_ci_difference_live_vs_actual_minutes
.observe({ plan: plan }, difference)
end
end end
end end
end end
---
title: Compare live vs actual CI minutes consumption and record it as a Prometheus metric
merge_request: 60949
author:
type: added
...@@ -139,6 +139,44 @@ RSpec.describe Ci::Minutes::UpdateBuildMinutesService do ...@@ -139,6 +139,44 @@ RSpec.describe Ci::Minutes::UpdateBuildMinutesService do
.to eq((project.statistics.reload.shared_runners_seconds.to_f / 60).round(2)) .to eq((project.statistics.reload.shared_runners_seconds.to_f / 60).round(2))
end end
end end
context 'when live tracking exists for the build', :redis do
before do
allow(Gitlab).to receive(:com?).and_return(true)
build.update!(status: :running)
freeze_time do
::Ci::Minutes::TrackLiveConsumptionService.new(build).tap do |service|
service.time_last_tracked_consumption!((build.duration.to_i - 5.minutes).ago)
service.execute
end
end
build.update!(status: :success)
end
it 'observes the difference between actual vs live consumption' do
histogram = double(:histogram)
expect(::Gitlab::Ci::Pipeline::Metrics)
.to receive(:gitlab_ci_difference_live_vs_actual_minutes)
.and_return(histogram)
expect(histogram).to receive(:observe).with({ plan: 'free' }, 5 * cost_factor)
subject
end
it_behaves_like 'new tracking matches legacy tracking'
end
context 'when live tracking does not exist for the build' do
it 'does not observe the difference' do
expect(::Gitlab::Ci::Pipeline::Metrics).not_to receive(:gitlab_ci_difference_live_vs_actual_minutes)
subject
end
end
end end
context 'for specific runner' do context 'for specific runner' do
......
...@@ -70,6 +70,14 @@ module Gitlab ...@@ -70,6 +70,14 @@ module Gitlab
Gitlab::Metrics.counter(name, comment) Gitlab::Metrics.counter(name, comment)
end end
def self.gitlab_ci_difference_live_vs_actual_minutes
name = :gitlab_ci_difference_live_vs_actual_minutes
comment = 'Comparison between CI minutes consumption from live tracking vs actual consumption'
labels = {}
buckets = [-120.0, -60.0, -30.0, -10.0, -5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0, 10.0, 30.0, 60.0, 120.0]
::Gitlab::Metrics.histogram(name, comment, labels, buckets)
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment