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

internal.rb « consul « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1994369dee9597b59a732d143d3cf6e9ea805d65 (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
78
# frozen_string_literal: true

module Gitlab
  module Consul
    class Internal
      Error = Class.new(StandardError)
      UnexpectedResponseError = Class.new(Gitlab::Consul::Internal::Error)
      SocketError = Class.new(Gitlab::Consul::Internal::Error)
      SSLError = Class.new(Gitlab::Consul::Internal::Error)
      ECONNREFUSED = Class.new(Gitlab::Consul::Internal::Error)

      class << self
        def api_url
          Gitlab.config.consul.api_url.to_s.presence if Gitlab.config.consul
        rescue Settingslogic::MissingSetting
          Gitlab::AppLogger.error('Consul api_url is not present in config/gitlab.yml')

          nil
        end

        def discover_service(service_name:)
          return unless service_name.present? && api_url

          api_path = URI.join(api_url, '/v1/catalog/service/', URI.encode_www_form_component(service_name)).to_s
          services = json_get(api_path, allow_local_requests: true, open_timeout: 5, read_timeout: 10)

          # Use the first service definition
          service = services&.first

          return unless service

          service_address = service['ServiceAddress'] || service['Address']
          service_port = service['ServicePort']

          [service_address, service_port]
        end

        def discover_prometheus_server_address
          service_address, service_port = discover_service(service_name: 'prometheus')

          return unless service_address && service_port

          "#{service_address}:#{service_port}"
        end

        private

        def json_get(path, options)
          response = get(path, options)
          code = response.try(:code)
          body = response.try(:body)

          raise Consul::Internal::UnexpectedResponseError unless code == 200 && body

          parse_response_body(body)
        end

        def parse_response_body(body)
          Gitlab::Json.parse(body)
        rescue StandardError
          raise Consul::Internal::UnexpectedResponseError
        end

        def get(path, options)
          Gitlab::HTTP.get(path, options)
        rescue ::SocketError
          raise Consul::Internal::SocketError
        rescue OpenSSL::SSL::SSLError
          raise Consul::Internal::SSLError
        rescue Errno::ECONNREFUSED
          raise Consul::Internal::ECONNREFUSED
        rescue StandardError
          raise Consul::Internal::UnexpectedResponseError
        end
      end
    end
  end
end