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
with_options presence : true , if : :activated? do
validates :api_url , url : true
end
after_save :clear_reactive_cache!
def initialize_properties
if properties . nil?
self . properties = { }
end
end
def title
'Prometheus'
end
def description
'Prometheus monitoring'
end
def self . to_param
'prometheus'
end
def fields
[
{
type : 'text' ,
name : 'api_url' ,
title : 'API URL' ,
2017-06-01 03:55:16 -04:00
placeholder : 'Prometheus API Base URL, like http://prometheus.example.com/' ,
2017-06-07 04:27:52 -04:00
help : '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' }
rescue Gitlab :: PrometheusError = > err
{ success : false , result : err }
end
2017-05-09 16:24:24 -04:00
def environment_metrics ( environment )
2017-05-10 05:25:30 -04:00
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: EnvironmentQuery . name , environment . id , & method ( :rename_data_to_metrics ) )
2017-04-26 16:09:03 -04:00
end
def deployment_metrics ( deployment )
2017-05-10 05:25:30 -04:00
metrics = with_reactive_cache ( Gitlab :: Prometheus :: Queries :: DeploymentQuery . name , deployment . id , & method ( :rename_data_to_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 )
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: AdditionalMetricsDeploymentQuery . name , deployment . id , & :itself )
end
def matched_metrics
2017-06-08 06:29:53 -04:00
with_reactive_cache ( Gitlab :: Prometheus :: Queries :: MatchedMetricsQuery . name , & :itself )
end
2017-03-07 11:57:42 -05:00
# Cache metrics for specific environment
2017-04-26 16:09:03 -04:00
def calculate_reactive_cache ( query_class_name , * args )
2017-03-07 11:57:42 -05:00
return unless active? && project && ! project . pending_delete?
2017-05-10 05:25:30 -04: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
}
rescue Gitlab :: PrometheusError = > err
{ success : false , result : err . message }
end
def client
2017-04-26 16:09:03 -04:00
@prometheus || = Gitlab :: PrometheusClient . new ( api_url : api_url )
2017-03-07 11:57:42 -05:00
end
2017-05-10 05:25:30 -04:00
private
def rename_data_to_metrics ( metrics )
metrics [ :metrics ] = metrics . delete :data
metrics
end
2017-03-07 11:57:42 -05:00
end