Unverified Commit 6724873b authored by Sean McGivern's avatar Sean McGivern
Browse files

Allow describing metrics

parent 0b9c0dce
...@@ -12,9 +12,14 @@ module GitLab ...@@ -12,9 +12,14 @@ module GitLab
def initialize(include_timestamp: true) def initialize(include_timestamp: true)
@metrics = Hash.new { |h, k| h[k] = [] } @metrics = Hash.new { |h, k| h[k] = [] }
@quantiles = Hash.new { |h, k| h[k] = [] } @quantiles = Hash.new { |h, k| h[k] = [] }
@metric_descriptions = {}
@include_timestamp = include_timestamp @include_timestamp = include_timestamp
end end
def describe(name, description)
@metric_descriptions[name] = description
end
def add(name, value, quantile = false, **labels) def add(name, value, quantile = false, **labels)
fail "value '#{value}' must be a number" unless value.is_a?(Numeric) fail "value '#{value}' must be a number" unless value.is_a?(Numeric)
...@@ -32,6 +37,8 @@ module GitLab ...@@ -32,6 +37,8 @@ module GitLab
buffer = "" buffer = ""
@metrics.each do |name, measurements| @metrics.each do |name, measurements|
buffer << "# HELP #{name} #{@metric_descriptions[name]}\n" if @metric_descriptions[name]
measurements.each do |measurement| measurements.each do |measurement|
buffer << name.to_s buffer << name.to_s
labels = (measurement[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",") labels = (measurement[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
......
...@@ -23,4 +23,19 @@ describe GitLab::Exporter::PrometheusMetrics do ...@@ -23,4 +23,19 @@ describe GitLab::Exporter::PrometheusMetrics do
subject.add("mymetric", "invalid", mylabel: "x", myotherlabel: "y").to_s subject.add("mymetric", "invalid", mylabel: "x", myotherlabel: "y").to_s
}.to raise_error(RuntimeError) }.to raise_error(RuntimeError)
end end
it "supports described metrics" do
time = Time.now
allow(Time).to receive(:now).and_return(time)
subject.describe("mymetric", "description")
subject.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
end
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