Sha256: 9d0e6dd700245d0a6141d0360141cce9687d4276fc4741c0808f06d528c3e1ab

Contents?: true

Size: 1.25 KB

Versions: 18

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Metric
  class Base
    # prefix applied to all metrics
    def self.default_prefix=(name)
      @default_prefix = name
    end

    def self.default_prefix
      @default_prefix.to_s
    end

    attr_accessor :help, :name, :data

    def initialize(name, help)
      @name = name
      @help = help
    end

    def type
      raise "Not implemented"
    end

    def metric_text
      raise "Not implemented"
    end

    def from_json(json)
      json = JSON.parse(json) if String === json
      @name = json["name"]
      @help = json["help"]
      @data = json["data"]
      if Hash === json["data"]
        @data = {}
        json["data"].each do |k, v|
          k = JSON.parse(k)
          k = Hash[k.map { |k1, v1| [k1.to_sym, v1] }]
          @data[k] = v
        end
      end
    end

    def prefix(name)
      Base.default_prefix + name
    end

    def labels_text(labels)
      if labels && labels.length > 0
        s = labels.map do |key, value|
          "#{key}=\"#{value}\""
        end.join(",")
        "{#{s}}"
      end
    end

    def to_prometheus_text
      <<~TEXT
        # HELP #{prefix(name)} #{help}
        # TYPE #{prefix(name)} #{type}
        #{metric_text}
      TEXT
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
prometheus_exporter-0.2.0 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.16 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.15 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.14 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.13 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.12 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.11 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.10 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.9 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.8 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.7 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.6 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.5 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.4 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.3 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.2 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.1 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.1.0 lib/prometheus_exporter/metric/base.rb