Commit 310dcf75 authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Monitor dead tuples per table

parent 0993a9d1
PATH
remote: .
specs:
gitlab-monitor (0.0.1)
pg (~> 0.18.4)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
...@@ -5,6 +11,7 @@ GEM ...@@ -5,6 +11,7 @@ GEM
diff-lcs (1.2.5) diff-lcs (1.2.5)
parser (2.3.1.2) parser (2.3.1.2)
ast (~> 2.2) ast (~> 2.2)
pg (0.18.4)
powerpack (0.1.1) powerpack (0.1.1)
rainbow (2.1.0) rainbow (2.1.0)
rspec (3.5.0) rspec (3.5.0)
...@@ -33,6 +40,7 @@ PLATFORMS ...@@ -33,6 +40,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
gitlab-monitor!
rspec (~> 3.5) rspec (~> 3.5)
rubocop (~> 0.42) rubocop (~> 0.42)
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "optparse" require "optparse"
require "gitlab_monitor" require "gitlab_monitor"
......
...@@ -20,5 +20,7 @@ Gem::Specification.new do |s| ...@@ -20,5 +20,7 @@ Gem::Specification.new do |s|
s.homepage = "http://gitlab.com" s.homepage = "http://gitlab.com"
s.license = "MIT" s.license = "MIT"
s.add_runtime_dependency "pg", "~> 0.18.4"
s.add_development_dependency "rspec", "~> 3.3" s.add_development_dependency "rspec", "~> 3.3"
end end
...@@ -6,5 +6,6 @@ module GitLab ...@@ -6,5 +6,6 @@ module GitLab
autoload :PrometheusMetrics, "gitlab_monitor/prometheus" autoload :PrometheusMetrics, "gitlab_monitor/prometheus"
autoload :Git, "gitlab_monitor/git" autoload :Git, "gitlab_monitor/git"
autoload :GitProber, "gitlab_monitor/git" autoload :GitProber, "gitlab_monitor/git"
autoload :DatabaseDeadTuplesCollector, "gitlab_monitor/database_dead_tuples_collector"
end end
end end
...@@ -74,8 +74,63 @@ module GitLab ...@@ -74,8 +74,63 @@ module GitLab
end end
end end
class DatabaseDeadTuples
COMMAND_NAME = "db-dead-tuples".freeze
def initialize(args)
@options = options(args)
@options.parse!
end
def options(args)
args.options do |opts|
opts.banner = "Usage: #{EXECUTABLE_NAME} #{COMMAND_NAME} [options]"
opts.on(
"-i", "--interval [NUMBER]", Numeric, "Interval (in seconds) at which data is collected. Default is 60."
) do |val|
@interval = val || 60
end
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!
run_loop
end
private
def validate!
fail InvalidCLICommand.new(help) unless @db_connection_string
end
def run_loop
collector = ::GitLab::Monitor::DatabaseDeadTuplesCollector.new(connection_string: @db_connection_string)
metrics = PrometheusMetrics.new
loop do
result = collector.run
result.each do |table_name, dead_tuples_count|
metrics.add("#{table_name}_dead_tuples", dead_tuples_count)
end
puts metrics.to_s
sleep @interval
end
end
end
def self.commands def self.commands
[GIT].each_with_object({}) do |command_class, commands| [GIT, DatabaseDeadTuples].each_with_object({}) do |command_class, commands|
commands[command_class::COMMAND_NAME] = command_class commands[command_class::COMMAND_NAME] = command_class
commands commands
end end
......
require "pg"
module GitLab
module Monitor
class DatabaseDeadTuplesCollector
attr_reader :result
def initialize(args)
@connection_string = args[:connection_string]
end
def run
table_names = connection.exec("SELECT tablename FROM pg_tables WHERE tableowner <> 'postgres'").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
end
stats
end
private
def connection
@connection ||= PG.connect(@connection_string)
end
end
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