prometheus.rb 1.68 KB
Newer Older
1
2
3
4
5
6
7
require 'prometheus/client'

module Gitlab
  module Metrics
    module Prometheus
      include Gitlab::CurrentSettings

8
      def metrics_folder_present?
9
10
11
12
13
        multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir

        multiprocess_files_dir &&
          ::Dir.exist?(multiprocess_files_dir) &&
          ::File.writable?(multiprocess_files_dir)
14
15
      end

16
      def prometheus_metrics_enabled?
17
18
19
        return @prometheus_metrics_enabled if defined?(@prometheus_metrics_enabled)

        @prometheus_metrics_enabled = prometheus_metrics_enabled_unmemoized
20
21
22
23
24
25
26
27
28
29
30
31
32
33
      end

      def registry
        @registry ||= ::Prometheus::Client.registry
      end

      def counter(name, docstring, base_labels = {})
        provide_metric(name) || registry.counter(name, docstring, base_labels)
      end

      def summary(name, docstring, base_labels = {})
        provide_metric(name) || registry.summary(name, docstring, base_labels)
      end

34
35
      def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
        provide_metric(name) || registry.gauge(name, docstring, base_labels, multiprocess_mode)
36
37
38
39
40
41
42
43
44
45
46
47
48
      end

      def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
        provide_metric(name) || registry.histogram(name, docstring, base_labels, buckets)
      end

      def provide_metric(name)
        if prometheus_metrics_enabled?
          registry.get(name)
        else
          NullMetric.new
        end
      end
49
50
51
52
53
54

      private

      def prometheus_metrics_enabled_unmemoized
        metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
      end
55
56
    end
  end
57
end