Unverified Commit 38c4c422 authored by Sean McGivern's avatar Sean McGivern
Browse files

Describe metrics at class level

parent 3c1f6886
......@@ -12,14 +12,24 @@ module GitLab
def initialize(include_timestamp: true)
@metrics = Hash.new { |h, k| h[k] = [] }
@quantiles = Hash.new { |h, k| h[k] = [] }
@metric_descriptions = {}
@include_timestamp = include_timestamp
end
class << self
def describe(name, description)
@metric_descriptions ||= {}
@metric_descriptions[name] = description
end
def description(name)
@metric_descriptions && @metric_descriptions[name]
end
def clear_descriptions
@metric_descriptions = {}
end
end
def add(name, value, quantile = false, **labels)
fail "value '#{value}' must be a number" unless value.is_a?(Numeric)
......@@ -37,7 +47,7 @@ module GitLab
buffer = ""
@metrics.each do |name, measurements|
buffer << "# HELP #{name} #{@metric_descriptions[name]}\n" if @metric_descriptions[name]
buffer << "# HELP #{name} #{self.class.description(name)}\n" if self.class.description(name)
measurements.each do |measurement|
buffer << name.to_s
......
......@@ -24,6 +24,9 @@ module GitLab
# needed to be re-initialized
POOL_TIMEOUT = 90
PrometheusMetrics.describe("sidekiq_enqueued_jobs",
"Total number of jobs enqueued by class name. Only inspects the first #{PROBE_JOBS_LIMIT} jobs per queue.") # rubocop:disable Layout/LineLength
def self.connection_pool
@@connection_pool ||= Hash.new do |h, connection_hash| # rubocop:disable Style/ClassVars
config = connection_hash.merge(pool_timeout: POOL_TIMEOUT, size: POOL_SIZE)
......@@ -108,10 +111,7 @@ module GitLab
#
# DO NOT USE this and probe_jobs together, as they export the same
# metric (sidekiq_enqueued_jobs).
def probe_jobs_limit # rubocop:disable Metrics/MethodLength
@metrics.describe("sidekiq_enqueued_jobs",
"Total number of jobs enqueued by class name. Only inspects the first #{PROBE_JOBS_LIMIT} jobs per queue.") # rubocop:disable Layout/LineLength
def probe_jobs_limit
with_sidekiq do
job_stats = Hash.new(0)
......
......@@ -29,13 +29,15 @@ describe GitLab::Exporter::PrometheusMetrics do
allow(Time).to receive(:now).and_return(time)
subject.describe("mymetric", "description")
subject.describe("missingmetric", "otherdescription")
described_class.describe("mymetric", "description")
described_class.describe("missingmetric", "otherdescription")
subject.add("mymetric", 1.3, mylabel: "x", myotherlabel: "y")
expect(subject.to_s).to eq(<<~METRICS)
# HELP mymetric description
mymetric{mylabel="x",myotherlabel="y"} 1.3 #{(time.to_f * 1000).to_i}
METRICS
described_class.clear_descriptions
end
end
Supports Markdown
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