module Prometheus::Client::Helper::MetricsRepresentation

Constants

DELIMITER
HELP_LINE
LABEL
METRIC_LINE
REGEX
REPLACE
SEPARATOR
TYPE_LINE

Public Class Methods

escape(string, format = :doc) click to toggle source
# File lib/prometheus/client/helper/metrics_representation.rb, line 45
def self.escape(string, format = :doc)
  string.to_s.gsub(REGEX[format], REPLACE)
end
format_labels(set) click to toggle source
# File lib/prometheus/client/helper/metrics_representation.rb, line 35
def self.format_labels(set)
  return if set.empty?

  strings = set.each_with_object([]) do |(key, value), memo|
    memo << format(LABEL, key, escape(value, :label))
  end

  "{#{strings.join(SEPARATOR)}}"
end
metric(name, labels, value) click to toggle source
# File lib/prometheus/client/helper/metrics_representation.rb, line 31
def self.metric(name, labels, value)
  format(METRIC_LINE, name, labels, value)
end
to_text(metrics) click to toggle source
# File lib/prometheus/client/helper/metrics_representation.rb, line 16
def self.to_text(metrics)
  lines = []

  metrics.each do |name, metric|
    lines << format(HELP_LINE, name, escape(metric[:help]))
    lines << format(TYPE_LINE, name, metric[:type])
    metric[:samples].each do |metric_name, labels, value|
      lines << metric(metric_name, format_labels(labels), value)
    end
  end

  # there must be a trailing delimiter
  (lines << nil).join(DELIMITER)
end