Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
gitlab-org
gitlab-exporter
Commits
8a67bdea
Commit
8a67bdea
authored
Sep 29, 2016
by
Ahmad Sherif
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reorganize database-related classes
parent
27d81152
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
91 additions
and
61 deletions
+91
-61
lib/gitlab_monitor.rb
lib/gitlab_monitor.rb
+1
-2
lib/gitlab_monitor/cli.rb
lib/gitlab_monitor/cli.rb
+3
-3
lib/gitlab_monitor/database.rb
lib/gitlab_monitor/database.rb
+9
-0
lib/gitlab_monitor/database/base.rb
lib/gitlab_monitor/database/base.rb
+32
-0
lib/gitlab_monitor/database/dead_tuples.rb
lib/gitlab_monitor/database/dead_tuples.rb
+46
-0
lib/gitlab_monitor/database_dead_tuples.rb
lib/gitlab_monitor/database_dead_tuples.rb
+0
-56
No files found.
lib/gitlab_monitor.rb
View file @
8a67bdea
...
...
@@ -6,7 +6,6 @@ module GitLab
autoload
:PrometheusMetrics
,
"gitlab_monitor/prometheus"
autoload
:Git
,
"gitlab_monitor/git"
autoload
:GitProber
,
"gitlab_monitor/git"
autoload
:DatabaseDeadTuplesCollector
,
"gitlab_monitor/database_dead_tuples"
autoload
:DatabaseDeadTuplesProber
,
"gitlab_monitor/database_dead_tuples"
autoload
:Database
,
"gitlab_monitor/database"
end
end
lib/gitlab_monitor/cli.rb
View file @
8a67bdea
...
...
@@ -104,9 +104,9 @@ module GitLab
def
run
validate!
::
GitLab
::
Monitor
::
DatabaseDeadTuplesProber
.
new
(
connection_string:
@db_connection_string
)
.
probe_db
.
write_to
(
@target
)
::
GitLab
::
Monitor
::
Database
::
DeadTuplesProber
.
new
(
connection_string:
@db_connection_string
)
.
probe_db
.
write_to
(
@target
)
end
private
...
...
lib/gitlab_monitor/database.rb
0 → 100644
View file @
8a67bdea
module
GitLab
module
Monitor
# Database-related classes
module
Database
autoload
:Base
,
"gitlab_monitor/database/base"
autoload
:DeadTuplesProber
,
"gitlab_monitor/database/dead_tuples"
end
end
end
lib/gitlab_monitor/database/base.rb
0 → 100644
View file @
8a67bdea
require
"pg"
module
GitLab
module
Monitor
module
Database
# An abstract class for interacting with DB
#
# It takes a connection string (e.g. "dbname=test port=5432")
class
Base
def
initialize
(
args
)
@connection_string
=
args
[
:connection_string
]
end
def
run
fail
NotImplemented
end
def
connected?
!
connection
.
nil?
end
private
def
connection
@connection
||=
PG
.
connect
(
@connection_string
)
rescue
PG
::
ConnectionBad
# rubocop:disable Lint/HandleExceptions
# Do nothing, we could be on the slave machine
end
end
end
end
end
lib/gitlab_monitor/database/dead_tuples.rb
0 → 100644
View file @
8a67bdea
module
GitLab
module
Monitor
module
Database
# A helper class to collect dead tuples stats from the database
#
# It takes a connection string (e.g. "dbname=test port=5432")
class
DeadTuplesCollector
<
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
end
stats
end
end
# Probes the DB specified by opts[:connection_string] for dead tubles stats, then converts them to metrics
class
DeadTuplesProber
def
initialize
(
opts
,
metrics
=
PrometheusMetrics
.
new
)
@metrics
=
metrics
@collector
=
DeadTuplesCollector
.
new
(
connection_string:
opts
[
:connection_string
])
end
def
probe_db
return
self
unless
@collector
.
connected?
result
=
@collector
.
run
result
.
each
do
|
table_name
,
dead_tuples_count
|
@metrics
.
add
(
"pg_dead_tuples"
,
dead_tuples_count
,
table_name:
table_name
)
end
self
end
def
write_to
(
target
)
target
.
write
(
@metrics
.
to_s
)
end
end
end
end
end
lib/gitlab_monitor/database_dead_tuples.rb
deleted
100644 → 0
View file @
27d81152
require
"pg"
module
GitLab
module
Monitor
# A helper class to collect dead tuples stats from the database
#
# It takes a connection string (e.g. "dbname=test port=5432")
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 = '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
end
stats
end
private
def
connection
@connection
||=
PG
.
connect
(
@connection_string
)
end
end
# Probes the DB specified by opts[:connection_string] for dead tubles stats, then converts them to metrics
class
DatabaseDeadTuplesProber
def
initialize
(
opts
,
metrics
=
PrometheusMetrics
.
new
)
@metrics
=
metrics
@collector
=
DatabaseDeadTuplesCollector
.
new
(
connection_string:
opts
[
:connection_string
])
end
def
probe_db
result
=
@collector
.
run
result
.
each
do
|
table_name
,
dead_tuples_count
|
@metrics
.
add
(
"pg_dead_tuples"
,
dead_tuples_count
,
table_name:
table_name
)
end
self
end
def
write_to
(
target
)
target
.
write
(
@metrics
.
to_s
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment