Skip to content
Snippets Groups Projects
Commit 509ffd29 authored by Fabio Pitino's avatar Fabio Pitino :two:
Browse files

Merge branch 'feature/gb/add-pipeline-age-metrics' into 'master'

Add pipeline age Prometheus histogram metric

See merge request !90027
parents 31f314d4 67ddec52
No related branches found
No related tags found
1 merge request!90027Add pipeline age Prometheus histogram metric
Pipeline #582259004 passed
......@@ -131,6 +131,7 @@ class Pipeline < Ci::ApplicationRecord
validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create
after_create :keep_around_commits, unless: :importing?
after_find :observe_age_in_minutes, unless: :importing?
use_fast_destroy :job_artifacts
use_fast_destroy :build_trace_chunks
......@@ -1322,6 +1323,16 @@ def has_expired_test_reports?
end
end
def age_in_minutes
return 0 unless persisted?
unless has_attribute?(:created_at)
raise ArgumentError, 'pipeline not fully loaded'
end
(Time.current - created_at).ceil / 60
end
private
def add_message(severity, content)
......@@ -1372,6 +1383,21 @@ def keep_around_commits
project.repository.keep_around(self.sha, self.before_sha)
end
def observe_age_in_minutes
return unless age_metric_enabled?
return unless persisted? && has_attribute?(:created_at)
::Gitlab::Ci::Pipeline::Metrics
.pipeline_age_histogram
.observe({}, age_in_minutes)
end
def age_metric_enabled?
::Gitlab::SafeRequestStore.fetch(:age_metric_enabled) do
::Feature.enabled?(:ci_pipeline_age_histogram, type: :ops)
end
end
# Without using `unscoped`, caller scope is also included into the query.
# Using `unscoped` here will be redundant after Rails 6.1
def object_hierarchy(options = {})
......
---
name: ci_pipeline_age_histogram
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90027
rollout_issue_url:
milestone: '15.1'
type: ops
group: group::pipeline execution
default_enabled: false
......@@ -42,6 +42,15 @@ def self.pipeline_size_histogram
::Gitlab::Metrics.histogram(name, comment, labels, buckets)
end
def self.pipeline_age_histogram
name = :gitlab_ci_pipeline_age_minutes
comment = 'Pipeline age histogram'
buckets = [5, 30, 120, 720, 1440, 7200, 21600, 43200, 86400, 172800, 518400, 1036800]
# 5m 30m 2h 12h 24h 5d 15d 30d 60d 180d 360d 2y
::Gitlab::Metrics.histogram(name, comment, {}, buckets)
end
def self.active_jobs_histogram
name = :gitlab_ci_active_jobs
comment = 'Total amount of active jobs'
......
......@@ -118,6 +118,38 @@
end
end
describe 'pipeline age metric' do
let_it_be(:pipeline) { create(:ci_empty_pipeline, :created) }
let(:pipeline_age_histogram) do
::Gitlab::Ci::Pipeline::Metrics.pipeline_age_histogram
end
context 'when pipeline age histogram is enabled' do
before do
stub_feature_flags(ci_pipeline_age_histogram: true)
end
it 'observes pipeline age' do
expect(pipeline_age_histogram).to receive(:observe)
described_class.find(pipeline.id)
end
end
context 'when pipeline age histogram is disabled' do
before do
stub_feature_flags(ci_pipeline_age_histogram: false)
end
it 'observes pipeline age' do
expect(pipeline_age_histogram).not_to receive(:observe)
described_class.find(pipeline.id)
end
end
end
describe '#set_status' do
let(:pipeline) { build(:ci_empty_pipeline, :created) }
......@@ -4979,4 +5011,34 @@ def create_bridge(upstream, downstream, depend = false)
end
end
end
describe '#age_in_minutes' do
let(:pipeline) { build(:ci_pipeline) }
context 'when pipeline has not been persisted' do
it 'returns zero' do
expect(pipeline.age_in_minutes).to eq 0
end
end
context 'when pipeline has been saved' do
it 'returns pipeline age in minutes' do
pipeline.save!
travel_to(pipeline.created_at + 2.hours) do
expect(pipeline.age_in_minutes).to eq 120
end
end
end
context 'when pipeline has been loaded without all attributes' do
it 'raises an exception' do
pipeline.save!
pipeline_id = Ci::Pipeline.where(id: pipeline.id).select(:id).first
expect { pipeline_id.age_in_minutes }.to raise_error(ArgumentError)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment