summaryrefslogtreecommitdiff
path: root/app/services/ci/prometheus_metrics/observe_histograms_service.rb
blob: 10b3d61247be026cc5ac1adad95f348d69b7ea02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Ci
  module PrometheusMetrics
    class ObserveHistogramsService
      class << self
        def available_histograms
          @available_histograms ||= [
            histogram(:pipeline_graph_link_calculation_duration_seconds, 'Total time spent calculating links, in seconds', {}, [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.8, 1, 2]),
            histogram(:pipeline_graph_links_total, 'Number of links per graph', {}, [1, 5, 10, 25, 50, 100, 200]),
            histogram(:pipeline_graph_links_per_job_ratio, 'Ratio of links to job per graph', {}, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
          ].to_h
        end

        private

        def histogram(name, *attrs)
          [name.to_s, proc { Gitlab::Metrics.histogram(name, *attrs) }]
        end
      end

      def initialize(project, params)
        @project = project
        @params = params
      end

      def execute
        params
          .fetch(:histograms, [])
          .each { |data| observe(data) }

        ServiceResponse.success(http_status: :created)
      end

      private

      attr_reader :project, :params

      def observe(data)
        histogram = find_histogram(data[:name])
        histogram.observe({}, data[:value].to_f)
      end

      def find_histogram(name)
        self.class.available_histograms
          .fetch(name) { raise ActiveRecord::RecordNotFound }
          .call
      end
    end
  end
end