Commit aedde11a authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Merge branch 'zj-monitor-scheduled-jobs' into 'master'

Monitor scheduled jobs

See merge request !51
parents 06964419 1da4cd2e
Pipeline #49606 passed with stage
in 26 seconds
image: "ruby:2.2" image: "ruby:2.3"
before_script: before_script:
- git config --global user.email "bot@gitlab.com" - git config --global user.email "bot@gitlab.com"
......
# Commonly used screens these days easily fit more than 80 characters. AllCops:
Metrics/LineLength: TargetRubyVersion: 2.3
Max: 120
# Just use double quotes please # Just use double quotes please
Style/StringLiterals: Style/StringLiterals:
...@@ -16,8 +15,19 @@ Style/SignalException: ...@@ -16,8 +15,19 @@ Style/SignalException:
Style/RaiseArgs: Style/RaiseArgs:
EnforcedStyle: compact EnforcedStyle: compact
# Lets not freeze all the strings by default
Style/FrozenStringLiteralComment:
Enabled: false
Metrics/MethodLength: Metrics/MethodLength:
Max: 15 Max: 15
Metrics/AbcSize: Metrics/AbcSize:
Enabled: false Enabled: false
# Commonly used screens these days easily fit more than 80 characters.
Metrics/LineLength:
Max: 120
Metrics/ClassLength:
Max: 150
...@@ -28,26 +28,34 @@ module GitLab ...@@ -28,26 +28,34 @@ module GitLab
" AND ci_builds.updated_at < NOW() - INTERVAL '1 hour'".freeze " AND ci_builds.updated_at < NOW() - INTERVAL '1 hour'".freeze
PER_RUNNER_QUERY = PER_RUNNER_QUERY =
"SELECT " \ <<~SQL.freeze
" ci_builds.runner_id, " \ SELECT
" ci_runners.is_shared, " \ ci_builds.runner_id,
" projects.mirror, " \ ci_runners.is_shared,
" projects.pending_delete, " \ projects.mirror,
" projects.mirror_trigger_builds, " \ projects.pending_delete,
" COUNT(*) AS count " \ projects.mirror_trigger_builds,
" FROM ci_builds " \ ci_pipelines.pipeline_schedule_id,
" JOIN ci_runners " \ ci_builds.trigger_request_id,
" ON ci_runners.id = ci_builds.runner_id " \ COUNT(*) AS count
" JOIN projects " \ FROM ci_builds
" ON projects.id = ci_builds.project_id " \ JOIN ci_runners
" WHERE ci_builds.type = 'Ci::Build' " \ ON ci_runners.id = ci_builds.runner_id
" AND ci_builds.status = 'running' " \ JOIN projects
" GROUP BY " \ ON projects.id = ci_builds.project_id
" ci_builds.runner_id, " \ JOIN ci_pipelines
" projects.mirror, " \ ON ci_pipelines.id = ci_builds.commit_id
" projects.pending_delete, " \ WHERE ci_builds.type = 'Ci::Build'
" projects.mirror_trigger_builds, " \ AND ci_builds.status = 'running'
" ci_runners.is_shared".freeze 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 def run
results = {} results = {}
...@@ -65,14 +73,7 @@ module GitLab ...@@ -65,14 +73,7 @@ module GitLab
results = [] results = []
connection.exec(query).each do |row| connection.exec(query).each do |row|
results.push( results << transform_row_to_values(row)
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
)
end end
results results
...@@ -80,6 +81,19 @@ module GitLab ...@@ -80,6 +81,19 @@ module GitLab
[] []
end 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) def get_general(query)
connection.exec(query)[0]["count"].to_i connection.exec(query)[0]["count"].to_i
rescue PG::UndefinedTable, PG::UndefinedColumn rescue PG::UndefinedTable, PG::UndefinedColumn
...@@ -135,10 +149,12 @@ module GitLab ...@@ -135,10 +149,12 @@ module GitLab
def ci_builds_metrics(results_list, metric_name) def ci_builds_metrics(results_list, metric_name)
other_value = { "yes" => 0, "no" => 0 } other_value = { "yes" => 0, "no" => 0 }
results_list.each do |metric| results_list.each do |metric|
shared_runners = metric[:shared_runners] shared_runners = metric[:shared_runners]
namespace = metric[:namespace] namespace = metric[:namespace]
value = metric[:value] value = metric[:value]
# If we have a low value, put the value into an "other" bucket. # If we have a low value, put the value into an "other" bucket.
if value < 10 if value < 10
other_value[shared_runners] += value other_value[shared_runners] += value
...@@ -146,6 +162,7 @@ module GitLab ...@@ -146,6 +162,7 @@ module GitLab
@metrics.add(metric_name, value, namespace: namespace, shared_runners: shared_runners) @metrics.add(metric_name, value, namespace: namespace, shared_runners: shared_runners)
end end
end end
# Add metrics for the "other" bucket. # Add metrics for the "other" bucket.
@metrics.add(metric_name, other_value["yes"], namespace: "", shared_runners: "yes") @metrics.add(metric_name, other_value["yes"], namespace: "", shared_runners: "yes")
@metrics.add(metric_name, other_value["no"], namespace: "", shared_runners: "no") @metrics.add(metric_name, other_value["no"], namespace: "", shared_runners: "no")
...@@ -163,7 +180,9 @@ module GitLab ...@@ -163,7 +180,9 @@ module GitLab
shared_runner: metric[:shared_runner], shared_runner: metric[:shared_runner],
mirror: metric[:mirror], mirror: metric[:mirror],
pending_delete: metric[:pending_delete], 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 end
end end
......
...@@ -29,10 +29,14 @@ describe GitLab::Monitor::Database do ...@@ -29,10 +29,14 @@ describe GitLab::Monitor::Database do
"mirror" => "f", "mirror" => "f",
"pending_delete" => "f", "pending_delete" => "f",
"mirror_trigger_builds" => "f", "mirror_trigger_builds" => "f",
"pipeline_schedule_id" => 1,
"trigger_request_id" => nil,
"count" => 15 }, "count" => 15 },
{ "runner_id" => 2, { "runner_id" => 2,
"is_shared" => "f", "is_shared" => "f",
"mirror" => "t", "mirror" => "t",
"pipeline_schedule_id" => nil,
"trigger_request_id" => 3,
"pending_delete" => "f", "pending_delete" => "f",
"mirror_trigger_builds" => "t", "mirror_trigger_builds" => "t",
"count" => 5 }]) "count" => 5 }])
...@@ -43,8 +47,8 @@ describe GitLab::Monitor::Database do ...@@ -43,8 +47,8 @@ describe GitLab::Monitor::Database do
it "executes the query" do it "executes the query" do
expect(collector.run).to eq(per_runner: [ expect(collector.run).to eq(per_runner: [
{ runner: "1", shared_runner: "yes", mirror: "no", pending_delete: "no", mirror_trigger_builds: "no", value: 15 }, { 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", value: 5 } { runner: "2", shared_runner: "no", mirror: "yes", pending_delete: "no", mirror_trigger_builds: "yes", scheduled: "no", triggered: "yes", value: 5 }
], ],
pending_builds: [ pending_builds: [
{ namespace: "1", shared_runners: "yes", value: 30 }, { namespace: "1", shared_runners: "yes", value: 30 },
...@@ -86,8 +90,8 @@ describe GitLab::Monitor::Database do ...@@ -86,8 +90,8 @@ describe GitLab::Monitor::Database do
ci_created_builds{namespace="",shared_runners="yes"} 0 ci_created_builds{namespace="",shared_runners="yes"} 0
ci_created_builds{namespace="",shared_runners="no"} 0 ci_created_builds{namespace="",shared_runners="no"} 0
ci_stale_builds 2 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="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"} 5 ci_running_builds{runner="2",shared_runner="no",mirror="yes",pending_delete="no",mirror_trigger_builds="yes",scheduled="no",triggered="yes"} 5
OUTPUT OUTPUT
expect(writer.string).to eq(output.strip_heredoc) expect(writer.string).to eq(output.strip_heredoc)
......
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