summaryrefslogtreecommitdiff
path: root/lib/gitlab/prometheus/adapter.rb
blob: 76e65d29c7a0c8ab93e25916e2b4808070f110ad (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
# frozen_string_literal: true

module Gitlab
  module Prometheus
    class Adapter
      attr_reader :project, :cluster

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

      def prometheus_adapter
        @prometheus_adapter ||= if service_prometheus_adapter.can_query?
                                  service_prometheus_adapter
                                else
                                  cluster_prometheus_adapter
                                end
      end

      def cluster_prometheus_adapter
        if cluster&.integration_prometheus
          return cluster.integration_prometheus
        end

        application = cluster&.application_prometheus

        application if application&.available?
      end

      private

      def service_prometheus_adapter
        project.find_or_initialize_service('prometheus')
      end
    end
  end
end