2017-03-07 11:57:42 -05:00
class PrometheusService < MonitoringService
2017-05-09 00:15:34 -04:00
include ReactiveService
2017-03-07 11:57:42 -05:00
self . reactive_cache_lease_timeout = 30 . seconds
self . reactive_cache_refresh_interval = 30 . seconds
self . reactive_cache_lifetime = 1 . minute
# 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
2017-03-07 11:57:42 -05:00
validates :api_url , url : true
end
2018-01-02 16:40:03 -05:00
before_save :synchronize_service_state!
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-02-06 19:53:18 -05:00
manual_configuration? || ! prometheus_installed?
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/' ) ,
help : s_ ( 'PrometheusService|By default, Prometheus listens on ‘ http://localhost:9090’ . It’ s not recommended to change the default address and port as this might affect or conflict with other services running on the GitLab server.' ) ,
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 )
client . ping
{ 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
2017-05-09 16:24:24 -04:00
def environment_metrics ( environment )
2018-02-23 12:58:40 -05:00
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: EnvironmentQuery . name , environment . id , & rename_field ( :data , :metrics ) )
2017-04-26 16:09:03 -04:00
end
def deployment_metrics ( deployment )
2018-02-23 12:58:40 -05:00
metrics = with_reactive_cache ( Gitlab :: Prometheus :: Queries :: DeploymentQuery . name , deployment . environment . id , deployment . id , & rename_field ( :data , :metrics ) )
2017-06-22 15:19:55 -04:00
metrics & . merge ( deployment_time : deployment . created_at . to_i ) || { }
2017-03-07 11:57:42 -05:00
end
2017-06-06 20:36:59 -04:00
def additional_environment_metrics ( environment )
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: AdditionalMetricsEnvironmentQuery . name , environment . id , & :itself )
end
def additional_deployment_metrics ( deployment )
2018-01-02 14:24:12 -05:00
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: AdditionalMetricsDeploymentQuery . name , deployment . environment . id , deployment . id , & :itself )
2017-06-06 20:36:59 -04:00
end
def matched_metrics
2018-02-06 19:35:57 -05:00
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: MatchedMetricsQuery . name , & :itself )
2018-01-02 14:24:12 -05:00
end
2017-03-07 11:57:42 -05:00
# Cache metrics for specific environment
2018-02-06 19:35:57 -05:00
def calculate_reactive_cache ( query_class_name , * args )
2017-03-07 11:57:42 -05:00
return unless active? && project && ! project . pending_delete?
2018-01-04 11:11:39 -05:00
2018-02-06 19:35:57 -05:00
environment_id = args . first
2018-01-02 15:42:24 -05:00
client = client ( environment_id )
2018-01-02 14:24:12 -05:00
2018-02-06 19:35:57 -05:00
data = Kernel . const_get ( query_class_name ) . new ( client ) . query ( * args )
2017-03-07 11:57:42 -05:00
{
success : true ,
2017-05-10 05:25:30 -04:00
data : data ,
2017-03-07 11:57:42 -05:00
last_update : Time . now . utc
}
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 . message }
end
2018-01-04 07:56:07 -05:00
def client ( environment_id = nil )
2018-01-02 16:40:03 -05:00
if manual_configuration?
2018-01-02 15:42:24 -05:00
Gitlab :: PrometheusClient . new ( RestClient :: Resource . new ( api_url ) )
2018-01-02 14:24:12 -05:00
else
2018-01-02 16:56:12 -05:00
cluster = cluster_with_prometheus ( environment_id )
2018-02-23 12:58:40 -05:00
raise Gitlab :: PrometheusClient :: Error , " couldn't find cluster with Prometheus installed " unless cluster
2018-01-02 15:42:24 -05:00
2018-01-04 11:11:39 -05:00
rest_client = client_from_cluster ( cluster )
2018-02-23 12:58:40 -05:00
raise Gitlab :: PrometheusClient :: Error , " couldn't create proxy Prometheus client " unless rest_client
2018-01-04 11:11:39 -05:00
2018-01-04 07:56:07 -05:00
Gitlab :: PrometheusClient . new ( rest_client )
2018-01-02 14:24:12 -05:00
end
end
2018-01-03 20:45:57 -05:00
def prometheus_installed?
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-01-04 07:56:07 -05:00
project . clusters . enabled . any? { | cluster | cluster . application_prometheus & . installed? }
2018-01-03 20:45:57 -05:00
end
2018-01-02 15:42:24 -05:00
private
2018-01-02 16:56:12 -05:00
def cluster_with_prometheus ( environment_id = nil )
2018-01-02 14:24:12 -05:00
clusters = if environment_id
2018-01-04 07:56:07 -05:00
:: Environment . find_by ( id : environment_id ) . try do | env |
# sort results by descending order based on environment_scope being longer
# thus more closely matching environment slug
project . clusters . enabled . for_environment ( env ) . sort_by { | c | c . environment_scope & . length } . reverse!
end
2018-01-02 14:24:12 -05:00
else
2018-01-04 07:56:07 -05:00
project . clusters . enabled . for_all_environments
2018-01-02 14:24:12 -05:00
end
2018-01-04 07:56:07 -05:00
clusters & . detect { | cluster | cluster . application_prometheus & . installed? }
end
def client_from_cluster ( cluster )
cluster . application_prometheus . proxy_client
2017-03-07 11:57:42 -05:00
end
2017-05-10 05:25:30 -04:00
2018-02-23 12:58:40 -05:00
def rename_field ( old_field , new_field )
- > ( metrics ) do
metrics [ new_field ] = metrics . delete ( old_field )
metrics
end
2017-05-10 05:25:30 -04:00
end
2018-01-02 16:40:03 -05:00
def synchronize_service_state!
2018-02-06 19:53:18 -05:00
self . active = prometheus_installed? || 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