2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class PrometheusService < MonitoringService
|
2018-03-27 19:54:05 +05:30
|
|
|
include PrometheusAdapter
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
# Access to prometheus is directly through the API
|
|
|
|
prop_accessor :api_url
|
2018-03-17 18:26:18 +05:30
|
|
|
boolean_accessor :manual_configuration
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
with_options presence: true, if: :manual_configuration? do
|
2018-11-08 19:23:39 +05:30
|
|
|
validates :api_url, public_url: true
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
before_save :synchronize_service_state
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def show_active_box?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def editable?
|
2018-12-13 13:39:08 +05:30
|
|
|
manual_configuration? || !prometheus_available?
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def title
|
|
|
|
'Prometheus'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2018-03-17 18:26:18 +05:30
|
|
|
s_('PrometheusService|Time-series monitoring service')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
'prometheus'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
2018-03-17 18:26:18 +05:30
|
|
|
return [] unless editable?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
[
|
2018-03-17 18:26:18 +05:30
|
|
|
{
|
|
|
|
type: 'checkbox',
|
|
|
|
name: 'manual_configuration',
|
|
|
|
title: s_('PrometheusService|Active'),
|
|
|
|
required: true
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'api_url',
|
|
|
|
title: 'API URL',
|
2018-03-17 18:26:18 +05:30
|
|
|
placeholder: s_('PrometheusService|Prometheus API Base URL, like http://prometheus.example.com/'),
|
2017-09-10 17:25:29 +05:30
|
|
|
required: true
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check we can connect to the Prometheus API
|
|
|
|
def test(*args)
|
2018-03-27 19:54:05 +05:30
|
|
|
Gitlab::PrometheusClient.new(prometheus_client).ping
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
{ success: true, result: 'Checked API endpoint' }
|
2018-03-27 19:54:05 +05:30
|
|
|
rescue Gitlab::PrometheusClient::Error => err
|
2017-08-17 22:00:37 +05:30
|
|
|
{ success: false, result: err }
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def prometheus_client
|
2019-03-13 22:55:13 +05:30
|
|
|
RestClient::Resource.new(api_url, max_redirects: 0) if should_return_client?
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def prometheus_available?
|
2018-03-17 18:26:18 +05:30
|
|
|
return false if template?
|
|
|
|
return false unless project
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
project.clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-13 22:55:13 +05:30
|
|
|
def should_return_client?
|
|
|
|
api_url && manual_configuration? && active? && valid?
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def synchronize_service_state
|
2018-12-13 13:39:08 +05:30
|
|
|
self.active = prometheus_available? || manual_configuration?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|