Commit 32fe843f authored by Ahmad Sherif's avatar Ahmad Sherif

Pass a label properly to PrometheusMetrics#add

parent 22935f0f
......@@ -119,7 +119,7 @@ module GitLab
result = collector.run
result.each do |table_name, dead_tuples_count|
metrics.add("pg_dead_tuples{table=\"#{table_name}\"}", dead_tuples_count)
metrics.add("pg_dead_tuples", dead_tuples_count, table_name: table_name)
end
puts metrics.to_s
......
......@@ -8,21 +8,23 @@ module GitLab
# The add method also can take any arbitrary amount of labels in a `key: value` format.
class PrometheusMetrics
def initialize
@metrics = {}
@metrics = Hash.new { |h, k| h[k] = [] }
end
def add(name, value, **labels)
@metrics[name] = { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i }
@metrics[name] << { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i }
self
end
def to_s
buffer = StringIO.new
@metrics.each do |name, metrics|
buffer.write(name.to_s)
labels = (metrics[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
buffer.write("{#{labels}}") unless labels.empty?
buffer.write(" #{metrics[:value]} #{metrics[:timestamp]}\n")
@metrics.each do |name, measurements|
measurements.each do |measurement|
buffer.write(name.to_s)
labels = (measurement[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
buffer.write("{#{labels}}") unless labels.empty?
buffer.write(" #{measurement[:value]} #{measurement[:timestamp]}\n")
end
end
buffer.rewind
buffer.read
......
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