Sha256: 093aa7f3ac1cc61a30f53fbaf49818a157d7d3b44ced0e6446aa27419daa79bf

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 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

    def self.default_labels=(labels)
      @default_labels = labels
    end

    def self.default_labels
      @default_labels || {}
    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)
      labels = (labels || {}).merge(Base.default_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

11 entries across 11 versions & 1 rubygems

Version Path
prometheus_exporter-0.4.6 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.5 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.4 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.3 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.2 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.1 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.4.0 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.3.4 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.3.3 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.3.1 lib/prometheus_exporter/metric/base.rb
prometheus_exporter-0.3.0 lib/prometheus_exporter/metric/base.rb