Sha256: 4817bd6fb30fd78f24e2fffecd6c8de141be3d86c31af7a4a579d4b3b9ea6da8

Contents?: true

Size: 1.42 KB

Versions: 197

Compression:

Stored size: 1.42 KB

Contents

module Prometheus
  module Client
    module Helper
      module MetricsRepresentation
        METRIC_LINE = '%s%s %s'.freeze
        TYPE_LINE = '# TYPE %s %s'.freeze
        HELP_LINE = '# HELP %s %s'.freeze

        LABEL = '%s="%s"'.freeze
        SEPARATOR = ','.freeze
        DELIMITER = "\n".freeze

        REGEX = { doc: /[\n\\]/, label: /[\n\\"]/ }.freeze
        REPLACE = { "\n" => '\n', '\\' => '\\\\', '"' => '\"' }.freeze

        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

        def self.metric(name, labels, value)
          format(METRIC_LINE, name, labels, value)
        end

        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

        def self.escape(string, format = :doc)
          string.to_s.gsub(REGEX[format], REPLACE)
        end
      end
    end
  end
end

Version data entries

197 entries across 197 versions & 2 rubygems

Version Path
prometheus-client-mmap-1.1.2 lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.1.2-x86_64-linux lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.1.2-x86_64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.1.2-arm64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.1.2-aarch64-linux lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6 lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-x86_64-linux-musl lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-x86_64-linux-gnu lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-x86_64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-arm64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-aarch64-linux-musl lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.6-aarch64-linux-gnu lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5 lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-x86_64-linux-musl lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-x86_64-linux-gnu lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-x86_64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-arm64-darwin lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-aarch64-linux-musl lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.5-aarch64-linux-gnu lib/prometheus/client/helper/metrics_representation.rb
prometheus-client-mmap-1.2.4 lib/prometheus/client/helper/metrics_representation.rb