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

Merge branch '6-monitor-vacuum' into 'master'

Include Vacuum Queries collector

Closes #6

See merge request !16
parents e1b920a8 09d543c7
Pipeline #22724 passed with stage
in 5 minutes and 51 seconds
......@@ -13,9 +13,10 @@ frequency.
### Supported Probes
1. Database
* Dead tuples
* Dead tuples
* Slow queries
* Blocked queries
* Vacuum queries
1. Git
* git pull/push timings
1. Process
......
......@@ -202,6 +202,48 @@ module GitLab
end
end
# Database vacuum queries runner.
#
# It will take a database connection string and print results to STDOUT
class DatabaseVacuumQueries
COMMAND_NAME = "db-vacuum-queries".freeze
def initialize(args)
@options = options(args)
@options.parse!
@target = args.shift || STDOUT
@target = File.open(@target, "a") if @target.is_a?(String)
end
def options(args)
args.options do |opts|
opts.banner = "Usage: #{EXECUTABLE_NAME} #{COMMAND_NAME} [options]"
opts.on("--db-conn=\"dbname=test port=5432\"", "Database connection string") do |val|
@db_connection_string = val
end
end
end
def help
@options.help
end
def run
validate!
::GitLab::Monitor::Database::VacuumQueriesProber.new(connection_string: @db_connection_string)
.probe_db
.write_to(@target)
end
private
def validate!
fail InvalidCLICommand.new(help) unless @db_connection_string
end
end
# Run a web server that exposes the metrics specified in a config file
class Server
COMMAND_NAME = "web".freeze
......@@ -287,6 +329,7 @@ module GitLab
DatabaseDeadTuples,
DatabaseBlockedQueries,
DatabaseSlowQueries,
DatabaseVacuumQueries,
Process,
Server
].each_with_object({}) do |command_class, commands|
......
......@@ -6,6 +6,7 @@ module GitLab
autoload :BlockedQueriesProber, "gitlab_monitor/database/blocked_queries"
autoload :SlowQueriesProber, "gitlab_monitor/database/slow_queries"
autoload :DeadTuplesProber, "gitlab_monitor/database/dead_tuples"
autoload :VacuumQueriesProber, "gitlab_monitor/database/vacuum_queries"
end
end
end
module GitLab
module Monitor
module Database
# A helper class to collect vacuum and vacuum analyze information
#
# It takes a connection string (e.g. "dbname=test port=5432")
class VacuumQueriesCollector < Base
def run
# Remove current VacuumQueriesCollector queries
pg_pid = connection.exec("SELECT pg_backend_pid() AS pid")[0]["pid"]
stats = {}
["vacuum", "vacuum analyze"].each do |query|
result = connection.exec(
<<-SQL
SELECT COUNT(*) AS query_count, MAX(EXTRACT(EPOCH FROM (clock_timestamp() - query_start))) AS query_duration
FROM pg_catalog.pg_stat_activity
WHERE state = 'active' AND query LIKE '%#{query}%' AND pid != #{pg_pid};
SQL
)
stats[query] = { "total" => result[0]["query_count"].to_i,
"max_age" => result[0]["query_duration"].to_f }
end
stats
end
end
# Probes the DB specified by opts[:connection_string] for vacuum and vacuum analyze information,
# then converts them to metrics
class VacuumQueriesProber
def initialize(opts, metrics: PrometheusMetrics.new)
@metrics = metrics
@collector = VacuumQueriesCollector.new(connection_string: opts[:connection_string])
end
def probe_db
return self unless @collector.connected?
result = @collector.run
@metrics.add("pg_vacuum_count", result["vacuum"]["total"])
@metrics.add("pg_vacuum_age_in_seconds", result["vacuum"]["max_age"])
@metrics.add("pg_vacuum_analyze_count", result["vacuum analyze"]["total"])
@metrics.add("pg_vacuum_analyze_age_in_seconds", result["vacuum analyze"]["max_age"])
self
end
def write_to(target)
target.write(@metrics.to_s)
end
end
end
end
end
module GitLab
module Monitor
VERSION = "0.0.6".freeze
VERSION = "0.0.7".freeze
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