Commit c4d265c4 authored by Pablo Carranza's avatar Pablo Carranza

Add documentation

parent e1e8c7a4
module GitLab
# GitLab Monitoring
module Monitor
autoload :CLI, "gitlab_monitor/cli"
autoload :TimeTracker, "gitlab_monitor/util"
......
module GitLab
module Monitor
# Stores runner classes in a single place
#
# The entry point is the module method "for" which takes the name of a runner.
# In case the runner is invalid it will return a NullRunner which fails with an
# InvalidCLICommand error, which contains the general application usage instructions.
module CLI
EXECUTABLE_NAME = "gitlab-mon".freeze
......@@ -9,6 +14,8 @@ module GitLab
class InvalidCLICommand < RuntimeError; end
# Empty runner that will raise an InvalidCLICommand when executed to provide the usage
# in the exception message
class NullRunner
def initialize(args)
end
......@@ -19,6 +26,12 @@ module GitLab
end
end
# Git runner.
#
# Takes something that behaves like ARGV with optparse included as an argument.
#
# It will take 2 positional arguments once parsed, the first one for the repository location,
# the optional second one is an IO like object to write to
class GIT
COMMAND_NAME = "git".freeze
......
......@@ -2,6 +2,14 @@ require "open3"
module GitLab
module Monitor
# Git monitoring helping class
#
# Takes a repository path for construction and provides 2 main methods:
# - pull
# - push
#
# Both methods return a CommandResult which includes the output of the execution
# plus the tracked execution time.
class Git
def initialize(repo)
fail "Repository #{repo} does not exists" unless Dir.exist? repo
......@@ -31,6 +39,10 @@ module GitLab
end
end
# Result of a command
#
# Provides some handy methods for checking if the execution failed and a simple to_s that will
# return the command output
CommandResult = Struct.new(:stdout, :status) do
def failed?
status.nonzero?
......@@ -45,6 +57,10 @@ module GitLab
end
end
# Handles creating a Git object, probing for both pull and push, and finally writing to metrics
#
# Optionally takes a metrics object which by default is a PrometheusMetrics, useful to change the
# metrics writer to something else.
class GitProber
def initialize(options, metrics: PrometheusMetrics.new)
@metrics = metrics
......
module GitLab
module Monitor
# Prometheus metrics container
#
# Provides a simple API to `add` metrics and then turn them `to_s` which will just
# dump all the metrics in prometheus format
#
# The add method also can take any arbitrary amount of labels in a `key: value` format.
class PrometheusMetrics
def initialize
@metrics = {}
......
module GitLab
module Monitor
# Simple time wrapper that provides a to_i and wraps the execution result
TrackedResult = Struct.new(:result, :time) do
def to_i
time
end
end
# Time tracking object
#
# Provides a simple time tracking, and returns back the result plus the tracked time
# wraped in a TrackedResult struct
class TimeTracker
def track
@start = Time.now.to_f
......
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