Commit 3ed764b7 authored by Pablo Carranza's avatar Pablo Carranza

Extract PrometheusMetrics to its own file

parent dd845115
......@@ -24,28 +24,6 @@ class GitRunner
end
end
class PrometheusMetrics
def initialize
@metrics = {}
end
def add(name, value, **labels)
@metrics[name] = { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i}
end
def to_s
buffer = StringIO.new
@metrics.each { |name, metrics|
buffer.write("#{name}")
labels = (metrics[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
buffer.write("{#{labels}}") unless labels.empty?
buffer.write(" #{metrics[:value]} #{metrics[:timestamp]}\n")
}
buffer.rewind
buffer.read
end
end
def main
runner = GitRunner.new(File.expand_path("~/dev/emptyness"))
metrics = PrometheusMetrics.new
......
module GitLab
module Monitor
autoload :TimeTracker, "gitlab_monitor/util"
autoload :PrometheusMetrics, "gitlab_monitor/prometheus"
end
end
module GitLab
module Monitor
class PrometheusMetrics
def initialize
@metrics = {}
end
def add(name, value, **labels)
@metrics[name] = { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i}
self
end
def to_s
buffer = StringIO.new
@metrics.each { |name, metrics|
buffer.write("#{name}")
labels = (metrics[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
buffer.write("{#{labels}}") unless labels.empty?
buffer.write(" #{metrics[:value]} #{metrics[:timestamp]}\n")
}
buffer.rewind
buffer.read
end
end
end
end
......@@ -5,3 +5,18 @@ describe GitLab::Monitor::TimeTracker do
expect(subject.track { sleep 0.1 }).to satisfy {|v| v >= 0.1}
end
end
describe GitLab::Monitor::PrometheusMetrics do
it "supports simple metrics" do
expect(subject.add("mymetric", 1.1).to_s).to match(/mymetric 1.1 \d*$/)
end
it "supports metrics with one label" do
expect(subject.add("mymetric", 1.2, mylabel: "x").to_s).to match(/mymetric{mylabel="x"} 1.2 \d*$/)
end
it "supports metrics with many labels" do
expect(subject.add("mymetric", 1.3, mylabel: "x", myotherlabel: "y").to_s).to match(
/mymetric{mylabel="x",myotherlabel="y"} 1.3 \d*$/)
end
end
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