summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/exporter/health_checks_middleware.rb
blob: c43b8004b72a911bcd1460598d85742556b4394f (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Exporter
      class HealthChecksMiddleware
        def initialize(app, readiness_probe, liveness_probe)
          @app = app
          @readiness_probe = readiness_probe
          @liveness_probe = liveness_probe
        end

        def call(env)
          case env['PATH_INFO']
          when '/readiness' then render_probe(@readiness_probe)
          when '/liveness' then render_probe(@liveness_probe)
          else @app.call(env)
          end
        end

        private

        def render_probe(probe)
          result = probe.execute

          [
            result.http_status,
            { 'Content-Type' => 'application/json; charset=utf-8' },
            [result.json.to_json]
          ]
        end
      end
    end
  end
end