Commit 8b4c43c0 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'deprecate-prometheus-listen-address' into 'master'

Deprecate prometheus.listen_address and prometheus.enable

See merge request gitlab-org/gitlab!50500
parents 82872c01 f8b8d4a8
---
title: Deprecate prometheus.listen_address and prometheus.enable
merge_request: 50500
author:
type: deprecated
......@@ -1207,10 +1207,7 @@ production: &base
# yourself, and then update the values here.
# https://docs.gitlab.com/ee/administration/monitoring/prometheus/
prometheus:
# Do not use `enable` and `listen_address` in any new code, as they are deprecated. Use `server_address` instead.
# https://gitlab.com/gitlab-org/gitlab/-/issues/227111
# enable: true
# listen_address: 'localhost:9090'
# enabled: true
# server_address: 'localhost:9090'
## Consul settings
......@@ -1499,8 +1496,8 @@ test:
group_base: 'ou=groups,dc=example,dc=com'
admin_group: ''
prometheus:
enable: true
listen_address: 'localhost:9090'
enabled: true
server_address: 'localhost:9090'
staging:
<<: *base
......@@ -851,6 +851,18 @@ Settings.monitoring.web_exporter['enabled'] ||= false
Settings.monitoring.web_exporter['address'] ||= 'localhost'
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
#
......
......@@ -75,7 +75,7 @@ module Gitlab
if response
# 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
# check if the project associated with the PrometheusService is the
# self_monitoring project. It checks
......@@ -105,7 +105,7 @@ module Gitlab
def add_prometheus_manual_configuration(result)
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')
......@@ -132,8 +132,8 @@ module Gitlab
::Gitlab::Prometheus::Internal.prometheus_enabled?
end
def prometheus_listen_address
::Gitlab::Prometheus::Internal.listen_address
def prometheus_server_address
::Gitlab::Prometheus::Internal.server_address
end
def docs_path
......@@ -152,13 +152,13 @@ module Gitlab
}
end
def internal_prometheus_listen_address_uri
def internal_prometheus_server_address_uri
::Gitlab::Prometheus::Internal.uri
end
def prometheus_service_attributes
{
api_url: internal_prometheus_listen_address_uri,
api_url: internal_prometheus_server_address_uri,
manual_configuration: true,
active: true
}
......
......@@ -4,43 +4,39 @@ module Gitlab
module Prometheus
class Internal
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
port = ':' + listen_address.split(':').second
port = ':' + server_address.split(':').second
'http://localhost' + port
elsif listen_address.starts_with?(':')
elsif server_address.starts_with?(':')
# :9090
'http://localhost' + listen_address
'http://localhost' + server_address
elsif listen_address.starts_with?('http')
elsif server_address.starts_with?('http')
# https://localhost:9090
listen_address
server_address
else
# localhost:9090
'http://' + listen_address
'http://' + server_address
end
end
def self.server_address
uri&.strip&.sub(/^http[s]?:\/\//, '')
end
def self.listen_address
Gitlab.config.prometheus.listen_address.to_s if Gitlab.config.prometheus
Gitlab.config.prometheus.server_address.to_s if Gitlab.config.prometheus
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
end
def self.prometheus_enabled?
Gitlab.config.prometheus.enable if Gitlab.config.prometheus
Gitlab.config.prometheus.enabled if Gitlab.config.prometheus
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
end
......
......@@ -145,7 +145,8 @@ module Gitlab
def prometheus_server_address
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
Gitlab::Consul::Internal.discover_prometheus_server_address
end
......
......@@ -8,8 +8,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
let(:prometheus_settings) do
{
enable: true,
listen_address: 'localhost:9090'
enabled: true,
server_address: 'localhost:9090'
}
end
......@@ -63,13 +63,13 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
application_setting.update(allow_local_requests_from_web_hooks_and_services: true)
end
shared_examples 'has prometheus service' do |listen_address|
shared_examples 'has prometheus service' do |server_address|
it do
expect(result[:status]).to eq(:success)
prometheus = project.prometheus_service
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.manual_configuration).to eq(true)
end
......@@ -202,25 +202,25 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
end
context 'with non default prometheus address' do
let(:listen_address) { 'https://localhost:9090' }
let(:server_address) { 'https://localhost:9090' }
let(:prometheus_settings) do
{
enable: true,
listen_address: listen_address
enabled: true,
server_address: server_address
}
end
it_behaves_like 'has prometheus service', 'https://localhost:9090'
context 'with :9090 symbol' do
let(:listen_address) { :':9090' }
let(:server_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' }
let(:server_address) { '0.0.0.0:9090' }
it_behaves_like 'has prometheus service', 'http://localhost:9090'
end
......@@ -251,8 +251,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do
{
enable: false,
listen_address: 'http://localhost:9090'
enabled: false,
server_address: 'http://localhost:9090'
}
end
......@@ -262,8 +262,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
end
end
context 'when prometheus listen address is blank in gitlab.yml' do
let(:prometheus_settings) { { enable: true, listen_address: '' } }
context 'when prometheus server address is blank in gitlab.yml' do
let(:prometheus_settings) { { enabled: true, server_address: '' } }
it 'does not configure prometheus' do
expect(result).to include(status: :success)
......@@ -296,8 +296,8 @@ RSpec.describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
context 'when prometheus manual configuration cannot be saved' do
let(:prometheus_settings) do
{
enable: true,
listen_address: 'httpinvalid://localhost:9090'
enabled: true,
server_address: 'httpinvalid://localhost:9090'
}
end
......
......@@ -3,12 +3,12 @@
require 'spec_helper'
RSpec.describe Gitlab::Prometheus::Internal do
let(:listen_address) { 'localhost:9090' }
let(:server_address) { 'localhost:9090' }
let(:prometheus_settings) do
{
enable: true,
listen_address: listen_address
enabled: true,
server_address: server_address
}
end
......@@ -27,25 +27,25 @@ RSpec.describe Gitlab::Prometheus::Internal do
it_behaves_like 'returns valid uri', 'http://localhost:9090'
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'
context 'with :9090 symbol' do
let(:listen_address) { :':9090' }
let(:server_address) { :':9090' }
it_behaves_like 'returns valid uri', 'http://localhost:9090'
end
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'
end
end
context 'when listen_address is nil' do
let(:listen_address) { nil }
context 'when server_address is nil' do
let(:server_address) { nil }
it 'does not fail' do
expect(described_class.uri).to be_nil
......@@ -53,7 +53,7 @@ RSpec.describe Gitlab::Prometheus::Internal do
end
context 'when prometheus listen address is blank in gitlab.yml' do
let(:listen_address) { '' }
let(:server_address) { '' }
it 'does not configure prometheus' do
expect(described_class.uri).to be_nil
......@@ -61,26 +61,6 @@ RSpec.describe Gitlab::Prometheus::Internal do
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
it 'returns correct value' do
expect(described_class.prometheus_enabled?).to eq(true)
......@@ -89,8 +69,8 @@ RSpec.describe Gitlab::Prometheus::Internal do
context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do
{
enable: false,
listen_address: listen_address
enabled: false,
server_address: server_address
}
end
......@@ -110,9 +90,9 @@ RSpec.describe Gitlab::Prometheus::Internal do
end
end
describe '.listen_address' do
describe '.server_address' 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
context 'when prometheus setting is not present in gitlab.yml' do
......@@ -121,7 +101,7 @@ RSpec.describe Gitlab::Prometheus::Internal do
end
it 'does not fail' do
expect(described_class.listen_address).to be_nil
expect(described_class.server_address).to be_nil
end
end
end
......
......@@ -284,7 +284,7 @@ RSpec.describe Gitlab::Utils::UsageData do
context 'when Prometheus server address is available from settings' do
before do
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
it_behaves_like 'try to query Prometheus with given address'
......
......@@ -95,7 +95,7 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowpl
service.api_url = 'http://localhost:9090'
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
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
stub_config(prometheus: {
enable: true,
listen_address: api_url
server_address: api_url
})
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