Add count of created jobs to ci_builds_total metric

parent fc16c306
Pipeline #42240 passed with stage
in 5 minutes and 2 seconds
......@@ -5,7 +5,7 @@ module GitLab
class CiBuildsCollector < Base
TOTAL_BUILDS_QUERY =
"SELECT p.shared_runners_enabled, b.status, COUNT(*) AS count FROM ci_builds AS b JOIN projects AS p " \
"ON p.id = b.gl_project_id WHERE b.type = 'Ci::Build' AND b.status IN ('pending', 'running') " \
"ON p.id = b.gl_project_id WHERE b.type = 'Ci::Build' AND b.status IN ('created', 'pending', 'running') " \
"GROUP BY p.shared_runners_enabled, b.status".freeze
NOT_UPDATED_RUNNING_BUILDS_QUERY =
......@@ -30,8 +30,9 @@ module GitLab
results = {}
total = get_shared_enabled_and_status_grouped(TOTAL_BUILDS_QUERY)
results[:running] = total[:running]
results[:created] = total[:created]
results[:pending] = total[:pending]
results[:running] = total[:running]
results[:not_updated_last_hour] = get_general(NOT_UPDATED_RUNNING_BUILDS_QUERY)
results[:per_runner] = get_per_shared_runner(RUNNING_PER_SHARED_RUNNER_QUERY)
......@@ -67,8 +68,9 @@ module GitLab
end
def get_shared_enabled_and_status_grouped(query)
results = { running: { shared_enabled: 0, shared_disabled: 0 },
pending: { shared_enabled: 0, shared_disabled: 0 } }
results = { created: { shared_enabled: 0, shared_disabled: 0 },
pending: { shared_enabled: 0, shared_disabled: 0 },
running: { shared_enabled: 0, shared_disabled: 0 } }
connection.exec(query).each do |row|
shared_enabled = row["shared_runners_enabled"] == "t"
......@@ -80,7 +82,9 @@ module GitLab
results
rescue PG::UndefinedTable, PG::UndefinedColumn
{ running: { shared_enabled: 0, shared_disabled: 0 }, pending: { shared_enabled: 0, shared_disabled: 0 } }
{ created: { shared_enabled: 0, shared_disabled: 0 },
pending: { shared_enabled: 0, shared_disabled: 0 },
running: { shared_enabled: 0, shared_disabled: 0 } }
end
def shared_runners_ids
......@@ -119,14 +123,18 @@ module GitLab
private
def metrics_total
@metrics.add("ci_builds_total", @results[:running][:shared_enabled], status: "running",
@metrics.add("ci_builds_total", @results[:created][:shared_enabled], status: "created",
shared_runners_enabled_projects: 1)
@metrics.add("ci_builds_total", @results[:running][:shared_disabled], status: "running",
@metrics.add("ci_builds_total", @results[:created][:shared_disabled], status: "created",
shared_runners_enabled_projects: 0)
@metrics.add("ci_builds_total", @results[:pending][:shared_enabled], status: "pending",
shared_runners_enabled_projects: 1)
@metrics.add("ci_builds_total", @results[:pending][:shared_disabled], status: "pending",
shared_runners_enabled_projects: 0)
@metrics.add("ci_builds_total", @results[:running][:shared_enabled], status: "running",
shared_runners_enabled_projects: 1)
@metrics.add("ci_builds_total", @results[:running][:shared_disabled], status: "running",
shared_runners_enabled_projects: 0)
@metrics.add("ci_builds_stale", @results[:not_updated_last_hour], status: "running", when: "last_hour")
end
......
......@@ -21,10 +21,12 @@ describe GitLab::Monitor::Database do
.and_return([1, 2])
allow(connection).to receive(:exec).with(total_builds_query)
.and_return([{ "shared_runners_enabled" => "f", "status" => "running", "count" => 2 },
{ "shared_runners_enabled" => "t", "status" => "running", "count" => 3 },
.and_return([{ "shared_runners_enabled" => "f", "status" => "created", "count" => 1 },
{ "shared_runners_enabled" => "t", "status" => "created", "count" => 4 },
{ "shared_runners_enabled" => "f", "status" => "pending", "count" => 2 },
{ "shared_runners_enabled" => "t", "status" => "pending", "count" => 5 }])
{ "shared_runners_enabled" => "t", "status" => "pending", "count" => 5 },
{ "shared_runners_enabled" => "f", "status" => "running", "count" => 2 },
{ "shared_runners_enabled" => "t", "status" => "running", "count" => 3 }])
allow(connection).to receive(:exec).with(not_updated_running_builds_query).and_return([{ "count" => 2 }])
allow(connection).to receive(:exec).with(per_runner_query % "1, 2")
.and_return([{ "runner_id" => 2, "count" => 15 }])
......@@ -38,8 +40,9 @@ describe GitLab::Monitor::Database do
it "executes the query" do
expect(collector.run).to eq(per_runner: { 1 => 0, 2 => 15 },
per_runner_mirrors: { 1 => 0, 2 => 10 },
running: { shared_enabled: 3, shared_disabled: 2 },
created: { shared_enabled: 4, shared_disabled: 1 },
pending: { shared_enabled: 5, shared_disabled: 2 },
running: { shared_enabled: 3, shared_disabled: 2 },
not_updated_last_hour: 2)
end
end
......@@ -59,10 +62,12 @@ describe GitLab::Monitor::Database do
prober.probe_db
prober.write_to(writer)
expect(writer.string).to match(/ci_builds_total{status="running",shared_runners_enabled_projects="1"} 3/)
expect(writer.string).to match(/ci_builds_total{status="running",shared_runners_enabled_projects="0"} 2/)
expect(writer.string).to match(/ci_builds_total{status="created",shared_runners_enabled_projects="1"} 4/)
expect(writer.string).to match(/ci_builds_total{status="created",shared_runners_enabled_projects="0"} 1/)
expect(writer.string).to match(/ci_builds_total{status="pending",shared_runners_enabled_projects="1"} 5/)
expect(writer.string).to match(/ci_builds_total{status="pending",shared_runners_enabled_projects="0"} 2/)
expect(writer.string).to match(/ci_builds_total{status="running",shared_runners_enabled_projects="1"} 3/)
expect(writer.string).to match(/ci_builds_total{status="running",shared_runners_enabled_projects="0"} 2/)
expect(writer.string).to match(/ci_builds_stale{status="running",when="last_hour"} 2/)
expect(writer.string).to match(/ci_builds_per_runner{status="running",runner="1",mirrors="0"} 0/)
expect(writer.string).to match(/ci_builds_per_runner{status="running",runner="2",mirrors="0"} 5/)
......
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