Commit f8b8d4a8 authored by Balasankar "Balu" C's avatar Balasankar "Balu" C Committed by Peter Leitzen

Deprecate prometheus.enable and prometheus.listen_address

We introduced `prometheus.server_address` setting in `gitlab.yml` which
is to be Rails application's SSOT regarding a Prometheus address.
Hence, we can deprecate the existing settings `enable` and
`listen_address`.

Changes:
* To `Gitlab::Prometheus::Internal` class methods:
  * `server_address` method renamed to `uri_without_protocol`, to
    reflect what the method actually does.
  * `listen_address` method renamed to `server_address`.
  * `promtheus_enabled?` method now depends on presence of
    `server_address`.

* To `Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService`
  class methods:
  * `prometheus_listen_address` method renamed to
    `prometheus_server_address`.
  * `internal_prometheus_listen_address_uri` renamed to
    `internal_prometheus_server_address_uri`.
Signed-off-by: default avatarBalasankar "Balu" C <balasankarc@autistici.org>
parent 5f003f9d
---
title: Deprecate prometheus.listen_address and prometheus.enable
merge_request: 50500
author:
type: deprecated
...@@ -1207,10 +1207,7 @@ production: &base ...@@ -1207,10 +1207,7 @@ production: &base
# yourself, and then update the values here. # yourself, and then update the values here.
# https://docs.gitlab.com/ee/administration/monitoring/prometheus/ # https://docs.gitlab.com/ee/administration/monitoring/prometheus/
prometheus: prometheus:
# Do not use `enable` and `listen_address` in any new code, as they are deprecated. Use `server_address` instead. # enabled: true
# https://gitlab.com/gitlab-org/gitlab/-/issues/227111
# enable: true
# listen_address: 'localhost:9090'
# server_address: 'localhost:9090' # server_address: 'localhost:9090'
## Consul settings ## Consul settings
...@@ -1499,8 +1496,8 @@ test: ...@@ -1499,8 +1496,8 @@ test:
group_base: 'ou=groups,dc=example,dc=com' group_base: 'ou=groups,dc=example,dc=com'
admin_group: '' admin_group: ''
prometheus: prometheus:
enable: true enabled: true
listen_address: 'localhost:9090' server_address: 'localhost:9090'
staging: staging:
<<: *base <<: *base
...@@ -851,6 +851,18 @@ Settings.monitoring.web_exporter['enabled'] ||= false ...@@ -851,6 +851,18 @@ Settings.monitoring.web_exporter['enabled'] ||= false
Settings.monitoring.web_exporter['address'] ||= 'localhost' Settings.monitoring.web_exporter['address'] ||= 'localhost'
Settings.monitoring.web_exporter['port'] ||= 8083 Settings.monitoring.web_exporter['port'] ||= 8083
#
# Prometheus settings
#
Settings['prometheus'] ||= Settingslogic.new({})
# TODO: Remove listen_address and enable in GitLab 14.0 and set default value
# of server_address to be nil and enabled to be false -
# https://gitlab.com/gitlab-org/gitlab/-/issues/296022
Settings.prometheus['enable'] ||= false
Settings.prometheus['listen_address'] ||= nil
Settings.prometheus['enabled'] = Settings.prometheus['enable'] if Settings.prometheus['enabled'].nil?
Settings.prometheus['server_address'] ||= Settings.prometheus['listen_address']
# #
# Shutdown settings # Shutdown settings
# #
......
...@@ -75,7 +75,7 @@ module Gitlab ...@@ -75,7 +75,7 @@ module Gitlab
if response if response
# In the add_prometheus_manual_configuration method, the Prometheus # In the add_prometheus_manual_configuration method, the Prometheus
# listen_address config is saved as an api_url in the PrometheusService # server_address config is saved as an api_url in the PrometheusService
# model. There are validates hooks in the PrometheusService model that # model. There are validates hooks in the PrometheusService model that
# check if the project associated with the PrometheusService is the # check if the project associated with the PrometheusService is the
# self_monitoring project. It checks # self_monitoring project. It checks
...@@ -105,7 +105,7 @@ module Gitlab ...@@ -105,7 +105,7 @@ module Gitlab
def add_prometheus_manual_configuration(result) def add_prometheus_manual_configuration(result)
return success(result) unless prometheus_enabled? return success(result) unless prometheus_enabled?
return success(result) unless prometheus_listen_address.present? return success(result) unless prometheus_server_address.present?
service = result[:project].find_or_initialize_service('prometheus') service = result[:project].find_or_initialize_service('prometheus')
...@@ -132,8 +132,8 @@ module Gitlab ...@@ -132,8 +132,8 @@ module Gitlab
::Gitlab::Prometheus::Internal.prometheus_enabled? ::Gitlab::Prometheus::Internal.prometheus_enabled?
end end
def prometheus_listen_address def prometheus_server_address
::Gitlab::Prometheus::Internal.listen_address ::Gitlab::Prometheus::Internal.server_address
end end
def docs_path def docs_path
...@@ -152,13 +152,13 @@ module Gitlab ...@@ -152,13 +152,13 @@ module Gitlab
} }
end end
def internal_prometheus_listen_address_uri def internal_prometheus_server_address_uri
::Gitlab::Prometheus::Internal.uri ::Gitlab::Prometheus::Internal.uri
end end
def prometheus_service_attributes def prometheus_service_attributes
{ {
api_url: internal_prometheus_listen_address_uri, api_url: internal_prometheus_server_address_uri,
manual_configuration: true, manual_configuration: true,
active: true active: true
} }
......
...@@ -4,43 +4,39 @@ module Gitlab ...@@ -4,43 +4,39 @@ module Gitlab
module Prometheus module Prometheus
class Internal class Internal
def self.uri def self.uri
return if listen_address.blank? return if server_address.blank?
if listen_address.starts_with?('0.0.0.0:') if server_address.starts_with?('0.0.0.0:')
# 0.0.0.0:9090 # 0.0.0.0:9090
port = ':' + listen_address.split(':').second port = ':' + server_address.split(':').second
'http://localhost' + port 'http://localhost' + port
elsif listen_address.starts_with?(':') elsif server_address.starts_with?(':')
# :9090 # :9090
'http://localhost' + listen_address 'http://localhost' + server_address
elsif listen_address.starts_with?('http') elsif server_address.starts_with?('http')
# https://localhost:9090 # https://localhost:9090
listen_address server_address
else else
# localhost:9090 # localhost:9090
'http://' + listen_address 'http://' + server_address
end end
end end
def self.server_address def self.server_address
uri&.strip&.sub(/^http[s]?:\/\//, '') Gitlab.config.prometheus.server_address.to_s if Gitlab.config.prometheus
end
def self.listen_address
Gitlab.config.prometheus.listen_address.to_s if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting rescue Settingslogic::MissingSetting
Gitlab::AppLogger.error('Prometheus listen_address is not present in config/gitlab.yml') Gitlab::AppLogger.error('Prometheus server_address is not present in config/gitlab.yml')
nil nil
end end
def self.prometheus_enabled? def self.prometheus_enabled?
Gitlab.config.prometheus.enable if Gitlab.config.prometheus Gitlab.config.prometheus.enabled if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting rescue Settingslogic::MissingSetting
Gitlab::AppLogger.error('prometheus.enable is not present in config/gitlab.yml') Gitlab::AppLogger.error('prometheus.enabled is not present in config/gitlab.yml')
false false
end end
......
...@@ -145,7 +145,8 @@ module Gitlab ...@@ -145,7 +145,8 @@ module Gitlab
def prometheus_server_address def prometheus_server_address
if Gitlab::Prometheus::Internal.prometheus_enabled? if Gitlab::Prometheus::Internal.prometheus_enabled?
Gitlab::Prometheus::Internal.server_address # Stripping protocol from URI
Gitlab::Prometheus::Internal.uri&.strip&.sub(%r{^https?://}, '')
elsif Gitlab::Consul::Internal.api_url elsif Gitlab::Consul::Internal.api_url
Gitlab::Consul::Internal.discover_prometheus_server_address Gitlab::Consul::Internal.discover_prometheus_server_address
end end
......
...@@ -8,8 +8,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -8,8 +8,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: true, enabled: true,
listen_address: 'localhost:9090' server_address: 'localhost:9090'
} }
end end
...@@ -63,13 +63,13 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -63,13 +63,13 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
application_setting.update(allow_local_requests_from_web_hooks_and_services: true) application_setting.update(allow_local_requests_from_web_hooks_and_services: true)
end end
shared_examples 'has prometheus service' do |listen_address| shared_examples 'has prometheus service' do |server_address|
it do it do
expect(result[:status]).to eq(:success) expect(result[:status]).to eq(:success)
prometheus = project.prometheus_service prometheus = project.prometheus_service
expect(prometheus).not_to eq(nil) expect(prometheus).not_to eq(nil)
expect(prometheus.api_url).to eq(listen_address) expect(prometheus.api_url).to eq(server_address)
expect(prometheus.active).to eq(true) expect(prometheus.active).to eq(true)
expect(prometheus.manual_configuration).to eq(true) expect(prometheus.manual_configuration).to eq(true)
end end
...@@ -202,25 +202,25 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -202,25 +202,25 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
end end
context 'with non default prometheus address' do context 'with non default prometheus address' do
let(:listen_address) { 'https://localhost:9090' } let(:server_address) { 'https://localhost:9090' }
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: true, enabled: true,
listen_address: listen_address server_address: server_address
} }
end end
it_behaves_like 'has prometheus service', 'https://localhost:9090' it_behaves_like 'has prometheus service', 'https://localhost:9090'
context 'with :9090 symbol' do context 'with :9090 symbol' do
let(:listen_address) { :':9090' } let(:server_address) { :':9090' }
it_behaves_like 'has prometheus service', 'http://localhost:9090' it_behaves_like 'has prometheus service', 'http://localhost:9090'
end end
context 'with 0.0.0.0:9090' do context 'with 0.0.0.0:9090' do
let(:listen_address) { '0.0.0.0:9090' } let(:server_address) { '0.0.0.0:9090' }
it_behaves_like 'has prometheus service', 'http://localhost:9090' it_behaves_like 'has prometheus service', 'http://localhost:9090'
end end
...@@ -251,8 +251,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -251,8 +251,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
context 'when prometheus setting is disabled in gitlab.yml' do context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: false, enabled: false,
listen_address: 'http://localhost:9090' server_address: 'http://localhost:9090'
} }
end end
...@@ -262,8 +262,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -262,8 +262,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
end end
end end
context 'when prometheus listen address is blank in gitlab.yml' do context 'when prometheus server address is blank in gitlab.yml' do
let(:prometheus_settings) { { enable: true, listen_address: '' } } let(:prometheus_settings) { { enabled: true, server_address: '' } }
it 'does not configure prometheus' do it 'does not configure prometheus' do
expect(result).to include(status: :success) expect(result).to include(status: :success)
...@@ -296,8 +296,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService ...@@ -296,8 +296,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
context 'when prometheus manual configuration cannot be saved' do context 'when prometheus manual configuration cannot be saved' do
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: true, enabled: true,
listen_address: 'httpinvalid://localhost:9090' server_address: 'httpinvalid://localhost:9090'
} }
end end
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Prometheus::Internal do RSpec.describe Gitlab::Prometheus::Internal do
let(:listen_address) { 'localhost:9090' } let(:server_address) { 'localhost:9090' }
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: true, enabled: true,
listen_address: listen_address server_address: server_address
} }
end end
...@@ -27,25 +27,25 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -27,25 +27,25 @@ RSpec.describe Gitlab::Prometheus::Internal do
it_behaves_like 'returns valid uri', 'http://localhost:9090' it_behaves_like 'returns valid uri', 'http://localhost:9090'
context 'with non default prometheus address' do context 'with non default prometheus address' do
let(:listen_address) { 'https://localhost:9090' } let(:server_address) { 'https://localhost:9090' }
it_behaves_like 'returns valid uri', 'https://localhost:9090' it_behaves_like 'returns valid uri', 'https://localhost:9090'
context 'with :9090 symbol' do context 'with :9090 symbol' do
let(:listen_address) { :':9090' } let(:server_address) { :':9090' }
it_behaves_like 'returns valid uri', 'http://localhost:9090' it_behaves_like 'returns valid uri', 'http://localhost:9090'
end end
context 'with 0.0.0.0:9090' do context 'with 0.0.0.0:9090' do
let(:listen_address) { '0.0.0.0:9090' } let(:server_address) { '0.0.0.0:9090' }
it_behaves_like 'returns valid uri', 'http://localhost:9090' it_behaves_like 'returns valid uri', 'http://localhost:9090'
end end
end end
context 'when listen_address is nil' do context 'when server_address is nil' do
let(:listen_address) { nil } let(:server_address) { nil }
it 'does not fail' do it 'does not fail' do
expect(described_class.uri).to be_nil expect(described_class.uri).to be_nil
...@@ -53,7 +53,7 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -53,7 +53,7 @@ RSpec.describe Gitlab::Prometheus::Internal do
end end
context 'when prometheus listen address is blank in gitlab.yml' do context 'when prometheus listen address is blank in gitlab.yml' do
let(:listen_address) { '' } let(:server_address) { '' }
it 'does not configure prometheus' do it 'does not configure prometheus' do
expect(described_class.uri).to be_nil expect(described_class.uri).to be_nil
...@@ -61,26 +61,6 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -61,26 +61,6 @@ RSpec.describe Gitlab::Prometheus::Internal do
end end
end end
describe '.server_address' do
context 'self.uri returns valid uri' do
['http://localhost:9090', 'https://localhost:9090 '].each do |valid_uri|
it 'returns correct server address' do
expect(described_class).to receive(:uri).and_return(valid_uri)
expect(described_class.server_address).to eq('localhost:9090')
end
end
end
context 'self.uri returns nil' do
it 'returns nil' do
expect(described_class).to receive(:uri).and_return(nil)
expect(described_class.server_address).to be_nil
end
end
end
describe '.prometheus_enabled?' do describe '.prometheus_enabled?' do
it 'returns correct value' do it 'returns correct value' do
expect(described_class.prometheus_enabled?).to eq(true) expect(described_class.prometheus_enabled?).to eq(true)
...@@ -89,8 +69,8 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -89,8 +69,8 @@ RSpec.describe Gitlab::Prometheus::Internal do
context 'when prometheus setting is disabled in gitlab.yml' do context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
enable: false, enabled: false,
listen_address: listen_address server_address: server_address
} }
end end
...@@ -110,9 +90,9 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -110,9 +90,9 @@ RSpec.describe Gitlab::Prometheus::Internal do
end end
end end
describe '.listen_address' do describe '.server_address' do
it 'returns correct value' do it 'returns correct value' do
expect(described_class.listen_address).to eq(listen_address) expect(described_class.server_address).to eq(server_address)
end end
context 'when prometheus setting is not present in gitlab.yml' do context 'when prometheus setting is not present in gitlab.yml' do
...@@ -121,7 +101,7 @@ RSpec.describe Gitlab::Prometheus::Internal do ...@@ -121,7 +101,7 @@ RSpec.describe Gitlab::Prometheus::Internal do
end end
it 'does not fail' do it 'does not fail' do
expect(described_class.listen_address).to be_nil expect(described_class.server_address).to be_nil
end end
end end
end end
......
...@@ -284,7 +284,7 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -284,7 +284,7 @@ RSpec.describe Gitlab::Utils::UsageData do
context 'when Prometheus server address is available from settings' do context 'when Prometheus server address is available from settings' do
before do before do
expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(true) expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(true)
expect(Gitlab::Prometheus::Internal).to receive(:server_address).and_return('prom:9090') expect(Gitlab::Prometheus::Internal).to receive(:uri).and_return('http://prom:9090')
end end
it_behaves_like 'try to query Prometheus with given address' it_behaves_like 'try to query Prometheus with given address'
......
...@@ -95,7 +95,7 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowpl ...@@ -95,7 +95,7 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowpl
service.api_url = 'http://localhost:9090' service.api_url = 'http://localhost:9090'
stub_application_setting(self_monitoring_project_id: project.id) stub_application_setting(self_monitoring_project_id: project.id)
stub_config(prometheus: { enable: true, listen_address: 'localhost:9090' }) stub_config(prometheus: { enable: true, server_address: 'localhost:9090' })
end end
it 'allows self-monitoring project to connect to internal Prometheus' do it 'allows self-monitoring project to connect to internal Prometheus' do
...@@ -242,7 +242,7 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowpl ...@@ -242,7 +242,7 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowpl
stub_config(prometheus: { stub_config(prometheus: {
enable: true, enable: true,
listen_address: api_url server_address: api_url
}) })
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment