Handle :9090 and 0.0.0.0:9090 listen_address formats

Prometheus listen_address can be in formats of :9090 and 0.0.0.0:9090.
But before these can be used to connect a project to Prometheus, they
have to converted into absolute URIs.
This commit is contained in:
Reuben Pereira 2019-09-02 14:35:02 +00:00 committed by Sean McGivern
parent 2d3dc05fb9
commit 414523b0bf
3 changed files with 42 additions and 40 deletions

View File

@ -45,20 +45,20 @@ module Gitlab
def validate_application_settings
return success if application_settings
log_error(_('No application_settings found'))
log_error('No application_settings found')
error(_('No application_settings found'))
end
def validate_project_created
return success unless project_created?
log_error(_('Project already created'))
log_error('Project already created')
error(_('Project already created'))
end
def validate_admins
unless instance_admins.any?
log_error(_('No active admin user found'))
log_error('No active admin user found')
return error(_('No active admin user found'))
end
@ -83,7 +83,7 @@ module Gitlab
def create_project
if project_created?
log_info(_('Instance administration project already exists'))
log_info('Instance administration project already exists')
@project = application_settings.instance_administration_project
return success(project: project)
end
@ -93,7 +93,7 @@ module Gitlab
if project.persisted?
success(project: project)
else
log_error(_("Could not create instance administration project. Errors: %{errors}") % { errors: project.errors.full_messages })
log_error("Could not create instance administration project. Errors: %{errors}" % { errors: project.errors.full_messages })
error(_('Could not create project'))
end
end
@ -106,7 +106,7 @@ module Gitlab
if result
success
else
log_error(_("Could not save instance administration project ID, errors: %{errors}") % { errors: application_settings.errors.full_messages })
log_error("Could not save instance administration project ID, errors: %{errors}" % { errors: application_settings.errors.full_messages })
error(_('Could not save project ID'))
end
end
@ -116,7 +116,7 @@ module Gitlab
errors = members.flat_map { |member| member.errors.full_messages }
if errors.any?
log_error(_('Could not add admins as members to self-monitoring project. Errors: %{errors}') % { errors: errors })
log_error('Could not add admins as members to self-monitoring project. Errors: %{errors}' % { errors: errors })
error(_('Could not add admins as members'))
else
success
@ -128,7 +128,7 @@ module Gitlab
return success unless prometheus_listen_address.present?
uri = parse_url(internal_prometheus_listen_address_uri)
return error(_('Prometheus listen_address is not a valid URI')) unless uri
return error(_('Prometheus listen_address in config/gitlab.yml is not a valid URI')) unless uri
application_settings.add_to_outbound_local_requests_whitelist([uri.normalized_host])
result = application_settings.save
@ -140,7 +140,7 @@ module Gitlab
Gitlab::CurrentSettings.expire_current_application_settings
success
else
log_error(_("Could not add prometheus URL to whitelist, errors: %{errors}") % { errors: application_settings.errors.full_messages })
log_error("Could not add prometheus URL to whitelist, errors: %{errors}" % { errors: application_settings.errors.full_messages })
error(_('Could not add prometheus URL to whitelist'))
end
end
@ -152,7 +152,7 @@ module Gitlab
service = project.find_or_initialize_service('prometheus')
unless service.update(prometheus_service_attributes)
log_error(_('Could not save prometheus manual configuration for self-monitoring project. Errors: %{errors}') % { errors: service.errors.full_messages })
log_error('Could not save prometheus manual configuration for self-monitoring project. Errors: %{errors}' % { errors: service.errors.full_messages })
return error(_('Could not save prometheus manual configuration'))
end
@ -175,15 +175,15 @@ module Gitlab
def prometheus_enabled?
Gitlab.config.prometheus.enable if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
log_error(_('prometheus.enable is not present in gitlab.yml'))
log_error('prometheus.enable is not present in config/gitlab.yml')
false
end
def prometheus_listen_address
Gitlab.config.prometheus.listen_address if Gitlab.config.prometheus
Gitlab.config.prometheus.listen_address.to_s if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
log_error(_('prometheus.listen_address is not present in gitlab.yml'))
log_error('Prometheus listen_address is not present in config/gitlab.yml')
nil
end
@ -228,9 +228,21 @@ module Gitlab
end
def internal_prometheus_listen_address_uri
if prometheus_listen_address.starts_with?('http')
if prometheus_listen_address.starts_with?('0.0.0.0:')
# 0.0.0.0:9090
port = ':' + prometheus_listen_address.split(':').second
'http://localhost' + port
elsif prometheus_listen_address.starts_with?(':')
# :9090
'http://localhost' + prometheus_listen_address
elsif prometheus_listen_address.starts_with?('http')
# https://localhost:9090
prometheus_listen_address
else
# localhost:9090
'http://' + prometheus_listen_address
end
end

View File

@ -3374,15 +3374,9 @@ msgstr ""
msgid "Could not add admins as members"
msgstr ""
msgid "Could not add admins as members to self-monitoring project. Errors: %{errors}"
msgstr ""
msgid "Could not add prometheus URL to whitelist"
msgstr ""
msgid "Could not add prometheus URL to whitelist, errors: %{errors}"
msgstr ""
msgid "Could not authorize chat nickname. Try again!"
msgstr ""
@ -3398,9 +3392,6 @@ msgstr ""
msgid "Could not create group"
msgstr ""
msgid "Could not create instance administration project. Errors: %{errors}"
msgstr ""
msgid "Could not create project"
msgstr ""
@ -3419,18 +3410,12 @@ msgstr ""
msgid "Could not revoke personal access token %{personal_access_token_name}."
msgstr ""
msgid "Could not save instance administration project ID, errors: %{errors}"
msgstr ""
msgid "Could not save project ID"
msgstr ""
msgid "Could not save prometheus manual configuration"
msgstr ""
msgid "Could not save prometheus manual configuration for self-monitoring project. Errors: %{errors}"
msgstr ""
msgid "Coverage"
msgstr ""
@ -6098,9 +6083,6 @@ msgstr ""
msgid "Instance Statistics visibility"
msgstr ""
msgid "Instance administration project already exists"
msgstr ""
msgid "Instance administrators group already exists"
msgstr ""
@ -9202,7 +9184,7 @@ msgstr ""
msgid "ProjectsNew|Want to house several dependent projects under the same namespace? %{link_start}Create a group.%{link_end}"
msgstr ""
msgid "Prometheus listen_address is not a valid URI"
msgid "Prometheus listen_address in config/gitlab.yml is not a valid URI"
msgstr ""
msgid "PrometheusService|%{exporters} with %{metrics} were found"
@ -14177,12 +14159,6 @@ msgstr ""
msgid "project avatar"
msgstr ""
msgid "prometheus.enable is not present in gitlab.yml"
msgstr ""
msgid "prometheus.listen_address is not present in gitlab.yml"
msgstr ""
msgid "quick actions"
msgstr ""

View File

@ -176,14 +176,28 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
end
context 'with non default prometheus address' do
let(:listen_address) { 'https://localhost:9090' }
let(:prometheus_settings) do
{
enable: true,
listen_address: 'https://localhost:9090'
listen_address: listen_address
}
end
it_behaves_like 'has prometheus service', 'https://localhost:9090'
context 'with :9090 symbol' do
let(:listen_address) { :':9090' }
it_behaves_like 'has prometheus service', 'http://localhost:9090'
end
context 'with 0.0.0.0:9090' do
let(:listen_address) { '0.0.0.0:9090' }
it_behaves_like 'has prometheus service', 'http://localhost:9090'
end
end
context 'when prometheus setting is not present in gitlab.yml' do