prometheus.rb 1.93 KB
Newer Older
1 2
require "quantile"

3
module GitLab
4
  module Exporter
Pablo Carranza's avatar
Pablo Carranza committed
5 6 7 8 9 10
    # 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.
11
    class PrometheusMetrics
12
      def initialize(include_timestamp: true)
13
        @metrics = Hash.new { |h, k| h[k] = [] }
14
        @quantiles = Hash.new { |h, k| h[k] = [] }
15
        @include_timestamp = include_timestamp
16 17
      end

18
      def add(name, value, quantile = false, **labels)
Ben Kochie's avatar
Ben Kochie committed
19 20
        fail "value must be a number" unless value.is_a?(Numeric)

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

27 28 29
        self
      end

Ahmad Sherif's avatar
Ahmad Sherif committed
30
      def to_s
31 32
        add_quantiles_to_metrics

33
        buffer = ""
34 35
        @metrics.each do |name, measurements|
          measurements.each do |measurement|
36
            buffer << name.to_s
37
            labels = (measurement[:labels] || {}).map { |label, value| "#{label}=\"#{value}\"" }.join(",")
38 39 40 41
            buffer << "{#{labels}}" unless labels.empty?
            buffer << " #{measurement[:value]}"
            buffer << " #{measurement[:timestamp]}" if @include_timestamp
            buffer << "\n"
42
          end
Pablo Carranza's avatar
Pablo Carranza committed
43
        end
44
        buffer
45
      end
46 47 48

      private

Ahmad Sherif's avatar
Ahmad Sherif committed
49
      def add_quantiles_to_metrics
50 51 52 53 54 55 56 57 58 59 60 61 62 63
        @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
64 65 66
    end
  end
end