Commit def19634 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '32353-prometheus-internal' into 'master'

Create lib to read internal Prometheus address from config

See merge request gitlab-org/gitlab!19503
parents fa85cc33 9d39585d
......@@ -176,19 +176,11 @@ module Gitlab
end
def prometheus_enabled?
Gitlab.config.prometheus.enable if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
log_error('prometheus.enable is not present in config/gitlab.yml')
false
::Gitlab::Prometheus::Internal.prometheus_enabled?
end
def prometheus_listen_address
Gitlab.config.prometheus.listen_address.to_s if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
log_error('Prometheus listen_address is not present in config/gitlab.yml')
nil
::Gitlab::Prometheus::Internal.listen_address
end
def instance_admins
......@@ -231,23 +223,7 @@ module Gitlab
end
def internal_prometheus_listen_address_uri
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
::Gitlab::Prometheus::Internal.uri
end
def prometheus_service_attributes
......
# frozen_string_literal: true
module Gitlab
module Prometheus
class Internal
def self.uri
return if listen_address.blank?
if listen_address.starts_with?('0.0.0.0:')
# 0.0.0.0:9090
port = ':' + listen_address.split(':').second
'http://localhost' + port
elsif listen_address.starts_with?(':')
# :9090
'http://localhost' + listen_address
elsif listen_address.starts_with?('http')
# https://localhost:9090
listen_address
else
# localhost:9090
'http://' + listen_address
end
end
def self.listen_address
Gitlab.config.prometheus.listen_address.to_s if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
Gitlab::AppLogger.error('Prometheus listen_address is not present in config/gitlab.yml')
nil
end
def self.prometheus_enabled?
Gitlab.config.prometheus.enable if Gitlab.config.prometheus
rescue Settingslogic::MissingSetting
Gitlab::AppLogger.error('prometheus.enable is not present in config/gitlab.yml')
false
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Prometheus::Internal do
let(:listen_address) { 'localhost:9090' }
let(:prometheus_settings) do
{
enable: true,
listen_address: listen_address
}
end
before do
stub_config(prometheus: prometheus_settings)
end
describe '.uri' do
shared_examples 'returns valid uri' do |uri_string|
it do
expect(described_class.uri).to eq(uri_string)
expect { Addressable::URI.parse(described_class.uri) }.not_to raise_error
end
end
it_behaves_like 'returns valid uri', 'http://localhost:9090'
context 'with non default prometheus address' do
let(:listen_address) { 'https://localhost:9090' }
it_behaves_like 'returns valid uri', 'https://localhost:9090'
context 'with :9090 symbol' do
let(:listen_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' }
it_behaves_like 'returns valid uri', 'http://localhost:9090'
end
end
context 'when listen_address is nil' do
let(:listen_address) { nil }
it 'does not fail' do
expect(described_class.uri).to eq(nil)
end
end
context 'when prometheus listen address is blank in gitlab.yml' do
let(:listen_address) { '' }
it 'does not configure prometheus' do
expect(described_class.uri).to eq(nil)
end
end
end
describe 'prometheus_enabled?' do
it 'returns correct value' do
expect(described_class.prometheus_enabled?).to eq(true)
end
context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do
{
enable: false,
listen_address: listen_address
}
end
it 'returns correct value' do
expect(described_class.prometheus_enabled?).to eq(false)
end
end
context 'when prometheus setting is not present in gitlab.yml' do
before do
allow(Gitlab.config).to receive(:prometheus).and_raise(Settingslogic::MissingSetting)
end
it 'does not fail' do
expect(described_class.prometheus_enabled?).to eq(false)
end
end
end
describe '.listen_address' do
it 'returns correct value' do
expect(described_class.listen_address).to eq(listen_address)
end
context 'when prometheus setting is not present in gitlab.yml' do
before do
allow(Gitlab.config).to receive(:prometheus).and_raise(Settingslogic::MissingSetting)
end
it 'does not fail' do
expect(described_class.listen_address).to eq(nil)
end
end
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