summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/exporter/web_exporter.rb
blob: c5fa1e545d7ed24fd5905dbdc462c3d89a385eed (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Exporter
      class WebExporter < BaseExporter
        ExporterCheck = Struct.new(:exporter) do
          def readiness
            Gitlab::HealthChecks::Result.new(
              'web_exporter', exporter.running)
          end

          def available?
            true
          end
        end

        RailsMetricsInitializer = Struct.new(:app) do
          def call(env)
            Gitlab::Metrics::RailsSlis.initialize_request_slis_if_needed!

            app.call(env)
          end
        end

        attr_reader :running

        # This exporter is always run on master process
        def initialize
          super

          # DEPRECATED:
          # these `readiness_checks` are deprecated
          # as presenting no value in a way how we run
          # application: https://gitlab.com/gitlab-org/gitlab/issues/35343
          self.readiness_checks = [
            WebExporter::ExporterCheck.new(self),
            Gitlab::HealthChecks::PumaCheck
          ]
        end

        def settings
          Gitlab.config.monitoring.web_exporter
        end

        def log_filename
          File.join(Rails.root, 'log', 'web_exporter.log')
        end

        def mark_as_not_running!
          @running = false
        end

        private

        def rack_app
          app = super

          Rack::Builder.app do
            use RailsMetricsInitializer
            run app
          end
        end

        def start_working
          @running = true
          super
        end

        def stop_working
          mark_as_not_running!
          super
        end
      end
    end
  end
end