Commit c0e11351 authored by Takuya Noguchi's avatar Takuya Noguchi
Browse files

Update rubocop from 0.42.0 to 0.93.1


Signed-off-by: default avatarTakuya Noguchi <takninnovationresearch@gmail.com>
parent 0073b8e5
inherit_from: .rubocop_todo.yml
AllCops: AllCops:
TargetRubyVersion: 2.3 TargetRubyVersion: 2.7
DisplayCopNames: true DisplayCopNames: true
# Just use double quotes please # Just use double quotes please
...@@ -20,15 +22,15 @@ Style/RaiseArgs: ...@@ -20,15 +22,15 @@ Style/RaiseArgs:
Style/FrozenStringLiteralComment: Style/FrozenStringLiteralComment:
Enabled: false Enabled: false
# Commonly used screens these days easily fit more than 80 characters.
Layout/LineLength:
Max: 120
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: Metrics/ClassLength:
Max: 150 Max: 150
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2021-01-11 12:24:03 UTC using RuboCop version 0.93.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'gitlab-exporter.gemspec'
# Offense count: 3
Lint/AmbiguousBlockAssociation:
Exclude:
- 'spec/git_spec.rb'
- 'spec/util_spec.rb'
# Offense count: 2
# Configuration parameters: MaximumRangeSize.
Lint/MissingCopEnableDirective:
Exclude:
- 'spec/database/ci_builds_spec.rb'
- 'spec/git_process_proper_spec.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/NonDeterministicRequireOrder:
Exclude:
- 'spec/spec_helper.rb'
# Offense count: 16
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 349
# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 8
# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 9
# Offense count: 1
# Configuration parameters: EnforcedStyle, AllowModifiersOnSymbols.
# SupportedStyles: inline, group
Style/AccessModifierDeclarations:
Exclude:
- 'lib/gitlab_exporter/util.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods.
# AllowedMethods: present?, blank?, presence, try, try!
Style/SafeNavigation:
Exclude:
- 'lib/gitlab_exporter/database/base.rb'
...@@ -13,22 +13,24 @@ PATH ...@@ -13,22 +13,24 @@ PATH
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
ast (2.4.0) ast (2.4.1)
connection_pool (2.2.2) connection_pool (2.2.2)
diff-lcs (1.3) diff-lcs (1.3)
mustermann (1.0.3) mustermann (1.0.3)
parser (2.5.1.0) parallel (1.20.1)
ast (~> 2.4.0) parser (3.0.0.0)
ast (~> 2.4.1)
pg (1.2.2) pg (1.2.2)
powerpack (0.1.1)
quantile (0.2.1) quantile (0.2.1)
rack (2.0.7) rack (2.0.7)
rack-protection (2.0.7) rack-protection (2.0.7)
rack rack
rainbow (2.1.0) rainbow (3.0.0)
redis (4.1.3) redis (4.1.3)
redis-namespace (1.6.0) redis-namespace (1.6.0)
redis (>= 3.0.4) redis (>= 3.0.4)
regexp_parser (2.0.3)
rexml (3.2.4)
rspec (3.7.0) rspec (3.7.0)
rspec-core (~> 3.7.0) rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0) rspec-expectations (~> 3.7.0)
...@@ -42,13 +44,18 @@ GEM ...@@ -42,13 +44,18 @@ GEM
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0) rspec-support (~> 3.7.0)
rspec-support (3.7.1) rspec-support (3.7.1)
rubocop (0.42.0) rubocop (0.93.1)
parser (>= 2.3.1.1, < 3.0) parallel (~> 1.10)
powerpack (~> 0.1) parser (>= 2.7.1.5)
rainbow (>= 1.99.1, < 3.0) rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (>= 1.4.0, < 2.0)
ruby-progressbar (1.8.1) rubocop-ast (1.4.0)
parser (>= 2.7.1.5)
ruby-progressbar (1.11.0)
sidekiq (5.2.7) sidekiq (5.2.7)
connection_pool (~> 2.2, >= 2.2.2) connection_pool (~> 2.2, >= 2.2.2)
rack (>= 1.5.0) rack (>= 1.5.0)
...@@ -60,7 +67,7 @@ GEM ...@@ -60,7 +67,7 @@ GEM
rack-protection (= 2.0.7) rack-protection (= 2.0.7)
tilt (~> 2.0) tilt (~> 2.0)
tilt (2.0.9) tilt (2.0.9)
unicode-display_width (1.6.0) unicode-display_width (1.7.0)
PLATFORMS PLATFORMS
ruby ruby
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "optparse" require "optparse"
require "gitlab_exporter" require "gitlab_exporter"
......
lib = File.expand_path("../lib", __FILE__) lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "gitlab_exporter/version" require "gitlab_exporter/version"
...@@ -20,13 +20,13 @@ Gem::Specification.new do |s| ...@@ -20,13 +20,13 @@ 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 "connection_pool", "~> 2.2.1"
s.add_runtime_dependency "pg", "~> 1.1" s.add_runtime_dependency "pg", "~> 1.1"
s.add_runtime_dependency "sinatra", "~> 2.0.4"
s.add_runtime_dependency "quantile", "~> 0.2.0" s.add_runtime_dependency "quantile", "~> 0.2.0"
s.add_runtime_dependency "sidekiq", "~> 5.2.1"
s.add_runtime_dependency "redis", "~> 4.1.2" s.add_runtime_dependency "redis", "~> 4.1.2"
s.add_runtime_dependency "redis-namespace", "~> 1.6.0" s.add_runtime_dependency "redis-namespace", "~> 1.6.0"
s.add_runtime_dependency "connection_pool", "~> 2.2.1" s.add_runtime_dependency "sidekiq", "~> 5.2.1"
s.add_runtime_dependency "sinatra", "~> 2.0.4"
s.add_development_dependency "rspec", "~> 3.7.0" s.add_development_dependency "rspec", "~> 3.7.0"
s.add_development_dependency "rspec-expectations", "~> 3.7.0" s.add_development_dependency "rspec-expectations", "~> 3.7.0"
......
...@@ -19,8 +19,7 @@ module GitLab ...@@ -19,8 +19,7 @@ module GitLab
# Empty runner that will raise an InvalidCLICommand when executed to provide the usage # Empty runner that will raise an InvalidCLICommand when executed to provide the usage
# in the exception message # in the exception message
class NullRunner class NullRunner
def initialize(args) def initialize(args); end
end
def run def run
fail InvalidCLICommand.new("Usage: #{EXECUTABLE_NAME} <command> [options] [arguments...]\n\n"\ fail InvalidCLICommand.new("Usage: #{EXECUTABLE_NAME} <command> [options] [arguments...]\n\n"\
......
...@@ -31,13 +31,11 @@ module GitLab ...@@ -31,13 +31,11 @@ module GitLab
def with_connection_pool def with_connection_pool
connection_pool.with do |conn| connection_pool.with do |conn|
begin yield conn
yield conn rescue PG::UnableToSend => e
rescue PG::UnableToSend => e @logger.warn "Error sending to the database: #{e}" if @logger
@logger.warn "Error sending to the database: #{e}" if @logger conn.reset
conn.reset raise e
raise e
end
end end
rescue PG::Error => e rescue PG::Error => e
@logger.error "Error connecting to the database: #{e}" if @logger @logger.error "Error connecting to the database: #{e}" if @logger
......
...@@ -35,7 +35,7 @@ module GitLab ...@@ -35,7 +35,7 @@ module GitLab
# Prober class to gather bloat metrics # Prober class to gather bloat metrics
class BloatProber class BloatProber
METRIC_KEYS = %w(bloat_ratio bloat_size extra_size real_size).freeze METRIC_KEYS = %w[bloat_ratio bloat_size extra_size real_size].freeze
attr_reader :metrics, :collector, :bloat_types attr_reader :metrics, :collector, :bloat_types
...@@ -46,7 +46,7 @@ module GitLab ...@@ -46,7 +46,7 @@ module GitLab
@metrics = metrics @metrics = metrics
@collector = collector @collector = collector
@collector.logger = logger @collector.logger = logger
@bloat_types = opts[:bloat_types] || %i(btree table) @bloat_types = opts[:bloat_types] || %i[btree table]
end end
def probe_db def probe_db
......
...@@ -391,6 +391,7 @@ module GitLab ...@@ -391,6 +391,7 @@ module GitLab
def include_bool_if_row_defined(row, field) def include_bool_if_row_defined(row, field)
return {} unless row[field.to_s] return {} unless row[field.to_s]
{ field => row[field.to_s] == "t" ? "yes" : "no" } { field => row[field.to_s] == "t" ? "yes" : "no" }
end end
...@@ -470,7 +471,7 @@ module GitLab ...@@ -470,7 +471,7 @@ module GitLab
def add_ci_created_pending_builds(metric_name, value, labels) def add_ci_created_pending_builds(metric_name, value, labels)
add_metric_with_namespace_label(metric_name, add_metric_with_namespace_label(metric_name,
[:namespace, :shared_runners, :has_minutes], %i[namespace shared_runners has_minutes],
value, value,
labels) labels)
end end
...@@ -505,8 +506,8 @@ module GitLab ...@@ -505,8 +506,8 @@ module GitLab
def add_ci_running_builds(value, labels) def add_ci_running_builds(value, labels)
add_metric_with_namespace_label( add_metric_with_namespace_label(
"ci_running_builds", "ci_running_builds",
[:runner, :namespace, :runner_type, :scheduled, %i[runner namespace runner_type scheduled
:triggered, :mirror, :mirror_trigger_builds, :has_minutes], triggered mirror mirror_trigger_builds has_minutes],
value, value,
labels labels
) )
......
...@@ -29,7 +29,7 @@ module GitLab ...@@ -29,7 +29,7 @@ module GitLab
MIRROR_QUERY = { MIRROR_QUERY = {
select: :projects, select: :projects,
joins: <<~SQL, joins: <<~SQL,
INNER JOIN project_mirror_data ON project_mirror_data.project_id = projects.id INNER JOIN project_mirror_data ON project_mirror_data.project_id = projects.id
INNER JOIN namespaces AS root_namespaces ON root_namespaces.id = ( INNER JOIN namespaces AS root_namespaces ON root_namespaces.id = (
WITH RECURSIVE "base_and_ancestors" AS ( WITH RECURSIVE "base_and_ancestors" AS (
...@@ -112,9 +112,9 @@ module GitLab ...@@ -112,9 +112,9 @@ module GitLab
ON users.id = u.user_id", ON users.id = u.user_id",
where: "user_type IS NULL", where: "user_type IS NULL",
fields: { fields: {
admin: {}, admin: {},
external: {}, external: {},
state: {}, state: {},
access_level: { definition: "COALESCE(u.access_level, 0)" } access_level: { definition: "COALESCE(u.access_level, 0)" }
} }
}, },
...@@ -122,14 +122,14 @@ module GitLab ...@@ -122,14 +122,14 @@ module GitLab
select: :projects, select: :projects,
fields: { fields: {
visibility_level: {}, visibility_level: {},
archived: {} archived: {}
} }
}, },
groups: { groups: {
select: :namespaces, select: :namespaces,
fields: { fields: {
visibility_level: {}, visibility_level: {},
root: { definition: "(parent_id IS NULL)" } root: { definition: "(parent_id IS NULL)" }
} }
} }
}.freeze }.freeze
......
...@@ -5,7 +5,7 @@ module GitLab ...@@ -5,7 +5,7 @@ module GitLab
# #
# It takes a connection string (e.g. "dbname=test port=5432") # It takes a connection string (e.g. "dbname=test port=5432")
class TupleStatsCollector < Base class TupleStatsCollector < Base
COLUMNS = %w(relname seq_tup_read idx_tup_fetch n_tup_ins n_tup_upd n_tup_del n_tup_hot_upd n_dead_tup seq_scan) COLUMNS = %w[relname seq_tup_read idx_tup_fetch n_tup_ins n_tup_upd n_tup_del n_tup_hot_upd n_dead_tup seq_scan]
.join(",") .join(",")
QUERY = <<-SQL.freeze QUERY = <<-SQL.freeze
SELECT #{COLUMNS} SELECT #{COLUMNS}
...@@ -35,9 +35,11 @@ module GitLab ...@@ -35,9 +35,11 @@ module GitLab
result.each do |table_name, tuple_stats| result.each do |table_name, tuple_stats|
tuple_stats.each do |column_name, value| tuple_stats.each do |column_name, value|
next if value.is_a?(Numeric)
@metrics.add("gitlab_database_stat_table_#{column_name}", @metrics.add("gitlab_database_stat_table_#{column_name}",
value.to_f, value.to_f,
table_name: table_name) unless value.is_a?(Numeric) table_name: table_name)
end end
end end
......
...@@ -13,6 +13,7 @@ module GitLab ...@@ -13,6 +13,7 @@ module GitLab
class Git class Git
def initialize(repo) def initialize(repo)
fail "Repository #{repo} does not exists" unless Dir.exist? repo fail "Repository #{repo} does not exists" unless Dir.exist? repo
@repo = repo @repo = repo
@tracker = TimeTracker.new @tracker = TimeTracker.new
end end
...@@ -35,6 +36,7 @@ module GitLab ...@@ -35,6 +36,7 @@ module GitLab
def execute(command) def execute(command)
result = CommandResult.new(*Open3.capture2e(command, chdir: @repo)) result = CommandResult.new(*Open3.capture2e(command, chdir: @repo))
fail "Command #{command} failed with status #{result.status}\n#{result.stdout}" if result.failed? fail "Command #{command} failed with status #{result.status}\n#{result.stdout}" if result.failed?
result result
end end
end end
...@@ -96,7 +98,7 @@ module GitLab ...@@ -96,7 +98,7 @@ module GitLab
Utils.pgrep("^git ").each do |pid| Utils.pgrep("^git ").each do |pid|
process_cmd = begin process_cmd = begin
File.read("/proc/#{pid}/cmdline") File.read("/proc/#{pid}/cmdline")
rescue rescue StandardError
"" # Process file is gone (race condition) "" # Process file is gone (race condition)
end end
subcommand = self.class.extract_subcommand(process_cmd) subcommand = self.class.extract_subcommand(process_cmd)
...@@ -131,6 +133,7 @@ module GitLab ...@@ -131,6 +133,7 @@ module GitLab
def self.extract_subcommand(cmd) def self.extract_subcommand(cmd)
return if cmd.empty? return if cmd.empty?
cmd_splitted = cmd.split("\u0000") # cmdline does not return it space-separated cmd_splitted = cmd.split("\u0000") # cmdline does not return it space-separated
cmd_splitted.shift # Because it's "git" cmd_splitted.shift # Because it's "git"
......
...@@ -87,7 +87,7 @@ module GitLab ...@@ -87,7 +87,7 @@ module GitLab
end end
end end
end end
rescue => e rescue StandardError => e
puts "Error: #{e}" puts "Error: #{e}"
@valid = false @valid = false
create_memstats_not_available(totals) create_memstats_not_available(totals)
......
...@@ -25,7 +25,7 @@ module GitLab ...@@ -25,7 +25,7 @@ module GitLab
# Locked: 0 kB # Locked: 0 kB
# VmFlags: rd ex mr mw me dw sd # VmFlags: rd ex mr mw me dw sd
class Mapping class Mapping
FIELDS = %w(size rss shared_clean shared_dirty private_clean private_dirty swap pss).freeze FIELDS = %w[size rss shared_clean shared_dirty private_clean private_dirty swap pss].freeze
attr_reader :address_start attr_reader :address_start
attr_reader :address_end attr_reader :address_end
......
...@@ -105,9 +105,7 @@ module GitLab ...@@ -105,9 +105,7 @@ module GitLab
::GitLab::Exporter::MemStats::Mapping::FIELDS.each do |field| ::GitLab::Exporter::MemStats::Mapping::FIELDS.each do |field|
value = stats.totals[field] value = stats.totals[field]
if value >= 0 @metrics.add("process_smaps_#{field}_bytes", value * 1024, @use_quantiles, **labels) if value >= 0
@metrics.add("process_smaps_#{field}_bytes", value * 1024, @use_quantiles, **labels)
end
end end
end end
......
...@@ -53,21 +53,19 @@ module GitLab ...@@ -53,21 +53,19 @@ module GitLab
self self
end end
def probe_jobs # rubocop:disable Metrics/MethodLength def probe_jobs
return self unless connected? return self unless connected?
job_stats = {} job_stats = {}
Sidekiq::Queue.all.each do |queue| Sidekiq::Queue.all.each do |queue|
begin Sidekiq.redis do |conn|
Sidekiq.redis do |conn| stats = conn.evalsha(QUEUE_JOB_STATS_SHA, ["queue:#{queue.name}"])
stats = conn.evalsha(QUEUE_JOB_STATS_SHA, ["queue:#{queue.name}"]) job_stats.merge!(stats.to_h)
job_stats.merge!(stats.to_h)
end
rescue Redis::CommandError # Could happen if the script exceeded the maximum run time (5 seconds by default)
# FIXME: Should we call SCRIPT KILL?
return self
end end
rescue Redis::CommandError # Could happen if the script exceeded the maximum run time (5 seconds by default)
# FIXME: Should we call SCRIPT KILL?
return self
end end
job_stats.each do |class_name, count| job_stats.each do |class_name, count|
...@@ -153,7 +151,7 @@ module GitLab ...@@ -153,7 +151,7 @@ module GitLab
end end
true true
end end
rescue Redis::CannotConnectError, Redis::TimeoutError # rubocop:disable Lint/HandleExceptions rescue Redis::CannotConnectError, Redis::TimeoutError
# Maybe we're trying connecting to a slave # Maybe we're trying connecting to a slave
end end
...@@ -164,6 +162,7 @@ module GitLab ...@@ -164,6 +162,7 @@ module GitLab
# Using administrative commands on conn directly (which is a Redis::Namespace) # Using administrative commands on conn directly (which is a Redis::Namespace)
# will be removed in redis-namespace 2.0. # will be removed in redis-namespace 2.0.
next if conn.redis.script(:exists, QUEUE_JOB_STATS_SHA) next if conn.redis.script(:exists, QUEUE_JOB_STATS_SHA)
conn.redis.script(:load, QUEUE_JOB_STATS_SCRIPT) conn.redis.script(:load, QUEUE_JOB_STATS_SCRIPT)
end end
end end
......
...@@ -24,7 +24,7 @@ module GitLab ...@@ -24,7 +24,7 @@ module GitLab
# Helper methods, some stuff was copied from ActiveSupport # Helper methods, some stuff was copied from ActiveSupport
module Utils module Utils
def camel_case_string(str) def camel_case_string(str)
str.gsub(/(?:_|^)([a-z\d]*)/i) { $1.capitalize } # rubocop:disable PerlBackrefs str.gsub(/(?:_|^)([a-z\d]*)/i) { $1.capitalize } # rubocop:disable Style/PerlBackrefs
end end
module_function :camel_case_string module_function :camel_case_string
......
...@@ -24,7 +24,7 @@ module GitLab ...@@ -24,7 +24,7 @@ module GitLab
private private