From 1da4cd2e2a928fbfdf12c170388e4d09d84cd9af Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Fri, 19 May 2017 13:11:46 +0000 Subject: [PATCH] Monitor scheduled jobs --- .gitlab-ci.yml | 2 +- .rubocop.yml | 16 ++++- lib/gitlab_monitor/database/ci_builds.rb | 77 +++++++++++++++--------- spec/database/ci_builds_spec.rb | 12 ++-- 4 files changed, 70 insertions(+), 37 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1fd936f..ab3a5f0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: "ruby:2.2" +image: "ruby:2.3" before_script: - git config --global user.email "bot@gitlab.com" diff --git a/.rubocop.yml b/.rubocop.yml index ec6add7..ddbed52 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,5 @@ -# Commonly used screens these days easily fit more than 80 characters. -Metrics/LineLength: - Max: 120 +AllCops: + TargetRubyVersion: 2.3 # Just use double quotes please Style/StringLiterals: @@ -16,8 +15,19 @@ Style/SignalException: Style/RaiseArgs: EnforcedStyle: compact +# Lets not freeze all the strings by default +Style/FrozenStringLiteralComment: + Enabled: false + Metrics/MethodLength: Max: 15 Metrics/AbcSize: Enabled: false + +# Commonly used screens these days easily fit more than 80 characters. +Metrics/LineLength: + Max: 120 + +Metrics/ClassLength: + Max: 150 diff --git a/lib/gitlab_monitor/database/ci_builds.rb b/lib/gitlab_monitor/database/ci_builds.rb index a304141..8659ae8 100644 --- a/lib/gitlab_monitor/database/ci_builds.rb +++ b/lib/gitlab_monitor/database/ci_builds.rb @@ -28,26 +28,34 @@ module GitLab " AND ci_builds.updated_at < NOW() - INTERVAL '1 hour'".freeze PER_RUNNER_QUERY = - "SELECT " \ - " ci_builds.runner_id, " \ - " ci_runners.is_shared, " \ - " projects.mirror, " \ - " projects.pending_delete, " \ - " projects.mirror_trigger_builds, " \ - " COUNT(*) AS count " \ - " FROM ci_builds " \ - " JOIN ci_runners " \ - " ON ci_runners.id = ci_builds.runner_id " \ - " JOIN projects " \ - " ON projects.id = ci_builds.project_id " \ - " WHERE ci_builds.type = 'Ci::Build' " \ - " AND ci_builds.status = 'running' " \ - " GROUP BY " \ - " ci_builds.runner_id, " \ - " projects.mirror, " \ - " projects.pending_delete, " \ - " projects.mirror_trigger_builds, " \ - " ci_runners.is_shared".freeze + <<~SQL.freeze + SELECT + ci_builds.runner_id, + ci_runners.is_shared, + projects.mirror, + projects.pending_delete, + projects.mirror_trigger_builds, + ci_pipelines.pipeline_schedule_id, + ci_builds.trigger_request_id, + COUNT(*) AS count + FROM ci_builds + JOIN ci_runners + ON ci_runners.id = ci_builds.runner_id + JOIN projects + ON projects.id = ci_builds.project_id + JOIN ci_pipelines + ON ci_pipelines.id = ci_builds.commit_id + WHERE ci_builds.type = 'Ci::Build' + AND ci_builds.status = 'running' + GROUP BY + ci_builds.runner_id, + projects.mirror, + projects.pending_delete, + projects.mirror_trigger_builds, + ci_runners.is_shared, + ci_pipelines.pipeline_schedule_id, + ci_builds.trigger_request_id + SQL def run results = {} @@ -65,14 +73,7 @@ module GitLab results = [] connection.exec(query).each do |row| - results.push( - runner: row["runner_id"].to_s, - shared_runner: row["is_shared"] == "t" ? "yes" : "no", - mirror: row["mirror"] == "t" ? "yes" : "no", - pending_delete: row["pending_delete"] == "t" ? "yes" : "no", - mirror_trigger_builds: row["mirror_trigger_builds"] == "t" ? "yes" : "no", - value: row["count"].to_i - ) + results << transform_row_to_values(row) end results @@ -80,6 +81,19 @@ module GitLab [] end + def transform_row_to_values(row) # rubocop:disable Metrics/CyclomaticComplexity + { + runner: row["runner_id"].to_s, + shared_runner: row["is_shared"] == "t" ? "yes" : "no", + mirror: row["mirror"] == "t" ? "yes" : "no", + scheduled: row["pipeline_schedule_id"] ? "yes" : "no", + triggered: row["trigger_request_id"] ? "yes" : "no", + pending_delete: row["pending_delete"] == "t" ? "yes" : "no", + mirror_trigger_builds: row["mirror_trigger_builds"] == "t" ? "yes" : "no", + value: row["count"].to_i + } + end + def get_general(query) connection.exec(query)[0]["count"].to_i rescue PG::UndefinedTable, PG::UndefinedColumn @@ -135,10 +149,12 @@ module GitLab def ci_builds_metrics(results_list, metric_name) other_value = { "yes" => 0, "no" => 0 } + results_list.each do |metric| shared_runners = metric[:shared_runners] namespace = metric[:namespace] value = metric[:value] + # If we have a low value, put the value into an "other" bucket. if value < 10 other_value[shared_runners] += value @@ -146,6 +162,7 @@ module GitLab @metrics.add(metric_name, value, namespace: namespace, shared_runners: shared_runners) end end + # Add metrics for the "other" bucket. @metrics.add(metric_name, other_value["yes"], namespace: "", shared_runners: "yes") @metrics.add(metric_name, other_value["no"], namespace: "", shared_runners: "no") @@ -163,7 +180,9 @@ module GitLab shared_runner: metric[:shared_runner], mirror: metric[:mirror], pending_delete: metric[:pending_delete], - mirror_trigger_builds: metric[:mirror_trigger_builds]) + mirror_trigger_builds: metric[:mirror_trigger_builds], + scheduled: metric[:scheduled], + triggered: metric[:triggered]) end end end diff --git a/spec/database/ci_builds_spec.rb b/spec/database/ci_builds_spec.rb index 1df9bb1..265f2b3 100644 --- a/spec/database/ci_builds_spec.rb +++ b/spec/database/ci_builds_spec.rb @@ -29,10 +29,14 @@ describe GitLab::Monitor::Database do "mirror" => "f", "pending_delete" => "f", "mirror_trigger_builds" => "f", + "pipeline_schedule_id" => 1, + "trigger_request_id" => nil, "count" => 15 }, { "runner_id" => 2, "is_shared" => "f", "mirror" => "t", + "pipeline_schedule_id" => nil, + "trigger_request_id" => 3, "pending_delete" => "f", "mirror_trigger_builds" => "t", "count" => 5 }]) @@ -43,8 +47,8 @@ describe GitLab::Monitor::Database do it "executes the query" do expect(collector.run).to eq(per_runner: [ - { runner: "1", shared_runner: "yes", mirror: "no", pending_delete: "no", mirror_trigger_builds: "no", value: 15 }, - { runner: "2", shared_runner: "no", mirror: "yes", pending_delete: "no", mirror_trigger_builds: "yes", value: 5 } + { runner: "1", shared_runner: "yes", mirror: "no", pending_delete: "no", mirror_trigger_builds: "no", scheduled: "yes", triggered: "no", value: 15 }, + { runner: "2", shared_runner: "no", mirror: "yes", pending_delete: "no", mirror_trigger_builds: "yes", scheduled: "no", triggered: "yes", value: 5 } ], pending_builds: [ { namespace: "1", shared_runners: "yes", value: 30 }, @@ -86,8 +90,8 @@ describe GitLab::Monitor::Database do ci_created_builds{namespace="",shared_runners="yes"} 0 ci_created_builds{namespace="",shared_runners="no"} 0 ci_stale_builds 2 - ci_running_builds{runner="1",shared_runner="yes",mirror="no",pending_delete="no",mirror_trigger_builds="no"} 15 - ci_running_builds{runner="2",shared_runner="no",mirror="yes",pending_delete="no",mirror_trigger_builds="yes"} 5 + ci_running_builds{runner="1",shared_runner="yes",mirror="no",pending_delete="no",mirror_trigger_builds="no",scheduled="yes",triggered="no"} 15 + ci_running_builds{runner="2",shared_runner="no",mirror="yes",pending_delete="no",mirror_trigger_builds="yes",scheduled="no",triggered="yes"} 5 OUTPUT expect(writer.string).to eq(output.strip_heredoc) -- GitLab