Sha256: 762fb0bbcef2aa099c917f7ca0fd28597bfe357ce1dec24f45420e10d35ca4b0

Contents?: true

Size: 1.94 KB

Versions: 25

Compression:

Stored size: 1.94 KB

Contents

require "quantile"

module GitLab
  module Exporter
    # Prometheus metrics container
    #
    # Provides a simple API to `add` metrics and then turn them `to_s` which will just
    # dump all the metrics in prometheus format
    #
    # The add method also can take any arbitrary amount of labels in a `key: value` format.
    class PrometheusMetrics
      def initialize(include_timestamp: true)
        @metrics = Hash.new { |h, k| h[k] = [] }
        @quantiles = Hash.new { |h, k| h[k] = [] }
        @include_timestamp = include_timestamp
      end

      def add(name, value, quantile = false, **labels)
        fail "value '#{value}' must be a number" unless value.is_a?(Numeric)

        if quantile
          @quantiles[{ name: name, labels: labels }] << value
        else
          @metrics[name] << { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i }
        end

        self
      end

      def to_s
        add_quantiles_to_metrics

        buffer = ""
        @metrics.each do |name, measurements|
          measurements.each do |measurement|
            buffer << name.to_s
            labels = (measurement[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
            buffer << "{#{labels}}" unless labels.empty?
            buffer << " #{measurement[:value]}"
            buffer << " #{measurement[:timestamp]}" if @include_timestamp
            buffer << "\n"
          end
        end
        buffer
      end

      private

      def add_quantiles_to_metrics
        @quantiles.each do |data, measurements|
          estimator = Quantile::Estimator.new

          measurements.each do |value|
            estimator.observe(value)
          end

          estimator.invariants.each do |invariant|
            data[:labels][:quantile] = "#{(invariant.quantile * 100).to_i}th"

            add(data[:name], estimator.query(invariant.quantile), **data[:labels])
          end
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
gitlab-exporter-6.0.0 lib/gitlab_exporter/prometheus.rb
gitlab-exporter-5.2.2 lib/gitlab_exporter/prometheus.rb
gitlab-exporter-5.2.1 lib/gitlab_exporter/prometheus.rb
gitlab-exporter-5.2.0 lib/gitlab_exporter/prometheus.rb
gitlab-exporter-5.1.0 lib/gitlab_exporter/prometheus.rb