Class: Gitlab::Consul::Internal

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/consul/internal.rb

Constant Summary collapse

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 Method Summary collapse

Class Method Details

.api_urlObject


13
14
15
16
17
18
19
# File 'lib/gitlab/consul/internal.rb', line 13

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

.discover_prometheus_uriObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/consul/internal.rb', line 38

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

  return unless service_address && service_port

  # There really is not a way to discover whether a Prometheus connection is using TLS or not
  # Try TLS first because HTTPS will return fast if failed.
  %w[https http].find do |scheme|
    connection_url = "#{scheme}://#{service_address}:#{service_port}"
    break connection_url if Gitlab::PrometheusClient.new(connection_url, allow_local_requests: true).healthy?
  rescue
    nil
  end
end

.discover_service(service_name:) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/consul/internal.rb', line 21

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