2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
module Gitlab
|
|
|
|
module HealthChecks
|
|
|
|
class PrometheusTextFormat
|
|
|
|
def marshal(metrics)
|
2017-06-02 09:55:44 -04:00
|
|
|
"#{metrics_with_type_declarations(metrics).join("\n")}\n"
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
private
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
def metrics_with_type_declarations(metrics)
|
|
|
|
type_declaration_added = {}
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
metrics.flat_map do |metric|
|
|
|
|
metric_lines = []
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-06-07 08:37:32 -04:00
|
|
|
unless type_declaration_added.key?(metric.name)
|
2017-05-29 18:18:46 -04:00
|
|
|
type_declaration_added[metric.name] = true
|
|
|
|
metric_lines << metric_type_declaration(metric)
|
|
|
|
end
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
metric_lines << metric_text(metric)
|
|
|
|
end
|
2017-05-29 08:19:43 -04:00
|
|
|
end
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
def metric_type_declaration(metric)
|
|
|
|
"# TYPE #{metric.name} gauge"
|
|
|
|
end
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
def metric_text(metric)
|
|
|
|
labels = metric.labels&.map { |key, value| "#{key}=\"#{value}\"" }&.join(',') || ''
|
2017-05-29 08:19:43 -04:00
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
if labels.empty?
|
|
|
|
"#{metric.name} #{metric.value}"
|
|
|
|
else
|
|
|
|
"#{metric.name}{#{labels}} #{metric.value}"
|
|
|
|
end
|
2017-05-29 08:19:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-01 08:38:40 -04:00
|
|
|
end
|