diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /lib/gitlab/consul | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'lib/gitlab/consul')
-rw-r--r-- | lib/gitlab/consul/internal.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/gitlab/consul/internal.rb b/lib/gitlab/consul/internal.rb new file mode 100644 index 00000000000..3afc24ddab9 --- /dev/null +++ b/lib/gitlab/consul/internal.rb @@ -0,0 +1,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 + 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 + raise Consul::Internal::UnexpectedResponseError + end + end + end + end +end |