2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
class PrometheusService < MonitoringService
|
2018-02-23 15:33:33 -05:00
|
|
|
include PrometheusAdapter
|
2017-03-07 11:57:42 -05:00
|
|
|
|
|
|
|
# Access to prometheus is directly through the API
|
|
|
|
prop_accessor :api_url
|
2018-01-02 16:40:03 -05:00
|
|
|
boolean_accessor :manual_configuration
|
2017-03-07 11:57:42 -05:00
|
|
|
|
2018-01-02 16:40:03 -05:00
|
|
|
with_options presence: true, if: :manual_configuration? do
|
2018-06-01 07:43:53 -04:00
|
|
|
validates :api_url, public_url: true
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
|
2018-02-23 19:06:08 -05:00
|
|
|
before_save :synchronize_service_state
|
2018-01-02 16:40:03 -05:00
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
2018-01-04 11:11:39 -05:00
|
|
|
self.properties = {}
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-02 16:40:03 -05:00
|
|
|
def show_active_box?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-01-31 16:55:47 -05:00
|
|
|
def editable?
|
2018-10-15 05:03:15 -04:00
|
|
|
manual_configuration? || !prometheus_available?
|
2018-01-31 16:55:47 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
def title
|
|
|
|
'Prometheus'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2018-01-31 16:22:03 -05:00
|
|
|
s_('PrometheusService|Time-series monitoring service')
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
'prometheus'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
2018-01-31 16:55:47 -05:00
|
|
|
return [] unless editable?
|
2018-01-31 18:12:24 -05:00
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
[
|
2018-01-04 11:11:39 -05:00
|
|
|
{
|
2018-01-31 16:55:47 -05:00
|
|
|
type: 'checkbox',
|
2018-01-30 05:28:20 -05:00
|
|
|
name: 'manual_configuration',
|
2018-01-31 16:55:47 -05:00
|
|
|
title: s_('PrometheusService|Active'),
|
|
|
|
required: true
|
2018-01-02 16:40:03 -05:00
|
|
|
},
|
2017-03-07 11:57:42 -05:00
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'api_url',
|
|
|
|
title: 'API URL',
|
2017-11-15 03:42:37 -05:00
|
|
|
placeholder: s_('PrometheusService|Prometheus API Base URL, like http://prometheus.example.com/'),
|
2017-05-22 06:07:12 -04:00
|
|
|
required: true
|
2017-03-07 11:57:42 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check we can connect to the Prometheus API
|
|
|
|
def test(*args)
|
2018-02-23 15:33:33 -05:00
|
|
|
Gitlab::PrometheusClient.new(prometheus_client).ping
|
2017-03-07 11:57:42 -05:00
|
|
|
|
|
|
|
{ success: true, result: 'Checked API endpoint' }
|
2018-02-23 12:58:40 -05:00
|
|
|
rescue Gitlab::PrometheusClient::Error => err
|
2017-03-07 11:57:42 -05:00
|
|
|
{ success: false, result: err }
|
|
|
|
end
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
def prometheus_client
|
|
|
|
RestClient::Resource.new(api_url) if api_url && manual_configuration? && active?
|
2018-01-02 14:24:12 -05:00
|
|
|
end
|
|
|
|
|
2018-10-15 05:03:15 -04:00
|
|
|
def prometheus_available?
|
2018-02-06 19:53:18 -05:00
|
|
|
return false if template?
|
|
|
|
return false unless project
|
2018-01-30 05:35:30 -05:00
|
|
|
|
2018-10-15 05:03:15 -04:00
|
|
|
project.clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
|
2018-01-03 20:45:57 -05:00
|
|
|
end
|
|
|
|
|
2018-01-02 15:42:24 -05:00
|
|
|
private
|
|
|
|
|
2018-02-23 19:06:08 -05:00
|
|
|
def synchronize_service_state
|
2018-10-15 05:03:15 -04:00
|
|
|
self.active = prometheus_available? || manual_configuration?
|
2018-01-03 20:45:57 -05:00
|
|
|
|
|
|
|
true
|
2018-01-02 16:40:03 -05:00
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|