Commit 74b91ff3 authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Merge branch 'feature/collect-more-db-tuple-stats' into 'master'

Collect more DB tuple stats

Closes #12

See merge request !35
parents 91eb74ad 9936ae38
PATH
remote: .
specs:
gitlab-monitor (0.0.22)
gitlab-monitor (0.0.25)
pg (~> 0.18.4)
quantile (~> 0.2.0)
redis-namespace (~> 1.5.2)
......
......@@ -13,7 +13,7 @@ frequency.
### Supported Probes
1. Database
* Dead tuples
* Per-table tuple stats
* Slow queries
* Blocked queries
* Vacuum queries
......
......@@ -5,8 +5,8 @@ server:
probes:
database:
multiple: true
dead_tuples_count:
class_name: Database::DeadTuplesProber
tuple_stats:
class_name: Database::TuplesProber
methods:
- probe_db
opts:
......
......@@ -76,11 +76,11 @@ module GitLab
end
end
# Database dead tuples runner.
# Database tuple stats runner.
#
# It will take a database connection string and print results to STDOUT
class DatabaseDeadTuples
COMMAND_NAME = "db-dead-tuples".freeze
class DatabaseTupleStats
COMMAND_NAME = "db-tuple-stats".freeze
def initialize(args)
@options = options(args)
......@@ -106,9 +106,9 @@ module GitLab
def run
validate!
::GitLab::Monitor::Database::DeadTuplesProber.new(connection_string: @db_connection_string)
.probe_db
.write_to(@target)
::GitLab::Monitor::Database::TuplesProber.new(connection_string: @db_connection_string)
.probe_db
.write_to(@target)
end
private
......@@ -451,7 +451,7 @@ module GitLab
def self.commands
[
GIT,
DatabaseDeadTuples,
DatabaseTupleStats,
DatabaseBlockedQueries,
DatabaseSlowQueries,
DatabaseVacuumQueries,
......
......@@ -6,7 +6,7 @@ module GitLab
autoload :BlockedQueriesProber, "gitlab_monitor/database/blocked_queries"
autoload :CiBuildsProber, "gitlab_monitor/database/ci_builds"
autoload :SlowQueriesProber, "gitlab_monitor/database/slow_queries"
autoload :DeadTuplesProber, "gitlab_monitor/database/dead_tuples"
autoload :TuplesProber, "gitlab_monitor/database/tuple_stats"
autoload :VacuumQueriesProber, "gitlab_monitor/database/vacuum_queries"
autoload :RowCountProber, "gitlab_monitor/database/row_count"
end
......
module GitLab
module Monitor
module Database
# A helper class to collect dead tuples stats from the database
# A helper class to collect tuple stats from the database
#
# It takes a connection string (e.g. "dbname=test port=5432")
class DeadTuplesCollector < Base
class TupleStatsCollector < Base
def run
table_names = connection.exec("SELECT tablename FROM pg_tables WHERE tableowner = 'gitlab'").values.flatten
stats = {}
table_names.each do |table_name|
result = connection.exec("SELECT n_dead_tup FROM pg_stat_user_tables WHERE relname = '#{table_name}'")
stats[table_name] = result[0]["n_dead_tup"].to_i
result = connection.exec(
<<-SQL
SELECT seq_tup_read, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_dead_tup
FROM pg_stat_user_tables
WHERE relname = '#{table_name}'
SQL
)
next if result.count.zero?
stats[table_name] = result[0]
end
stats
end
end
# Probes the DB specified by opts[:connection_string] for dead tubles stats, then converts them to metrics
class DeadTuplesProber
# Probes the DB specified by opts[:connection_string] for tuple stats, then converts them to metrics
class TuplesProber
def initialize(opts, metrics: PrometheusMetrics.new)
@metrics = metrics
@collector = DeadTuplesCollector.new(connection_string: opts[:connection_string])
@collector = TupleStatsCollector.new(connection_string: opts[:connection_string])
end
def probe_db
......@@ -30,8 +38,10 @@ module GitLab
result = @collector.run
result.each do |table_name, dead_tuples_count|
@metrics.add("pg_dead_tuples", dead_tuples_count, table_name: table_name)
result.each do |table_name, tuple_stats|
tuple_stats.each do |column_name, value|
@metrics.add("pg_stat_table_#{column_name}", value.to_s, table_name: table_name)
end
end
self
......
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