Welcome to mirror list, hosted at ThFree Co, Russian Federation.

observe_histograms_service.rb « prometheus_metrics « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee22ea75df98b1c553df2e662ddcb88c1d9c917f (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
52
53
54
55
56
57
# 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
        return ServiceResponse.success(http_status: :accepted) unless enabled?

        params
          .fetch(:histograms, [])
          .each(&method(:observe))

        ServiceResponse.success(http_status: :created)
      end

      private

      attr_reader :project, :params

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

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

      def enabled?
        ::Feature.enabled?(:ci_accept_frontend_prometheus_metrics, project, default_enabled: :yaml)
      end
    end
  end
end