debian-mirror-gitlab/app/models/project_services/prometheus_service.rb

166 lines
4.8 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
class PrometheusService < MonitoringService
include ReactiveService
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-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
2017-08-17 22:00:37 +05:30
validates :api_url, url: true
end
2018-03-17 18:26:18 +05:30
before_save :synchronize_service_state!
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?
manual_configuration? || !prometheus_installed?
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/'),
help: s_('PrometheusService|By default, Prometheus listens on http://localhost:9090. Its not recommended to change the default address and port as this might affect or conflict with other services running on the GitLab server.'),
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)
client.ping
{ success: true, result: 'Checked API endpoint' }
rescue Gitlab::PrometheusError => err
{ success: false, result: err }
end
2017-09-10 17:25:29 +05:30
def environment_metrics(environment)
with_reactive_cache(Gitlab::Prometheus::Queries::EnvironmentQuery.name, environment.id, &method(:rename_data_to_metrics))
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
def deployment_metrics(deployment)
2018-03-17 18:26:18 +05:30
metrics = with_reactive_cache(Gitlab::Prometheus::Queries::DeploymentQuery.name, deployment.environment.id, deployment.id, &method(:rename_data_to_metrics))
2017-09-10 17:25:29 +05:30
metrics&.merge(deployment_time: deployment.created_at.to_i) || {}
end
def additional_environment_metrics(environment)
with_reactive_cache(Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery.name, environment.id, &:itself)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def additional_deployment_metrics(deployment)
2018-03-17 18:26:18 +05:30
with_reactive_cache(Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery.name, deployment.environment.id, deployment.id, &:itself)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def matched_metrics
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, *args)
return unless active? && project && !project.pending_delete?
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
environment_id = args.first
client = client(environment_id)
2017-09-10 17:25:29 +05:30
data = Kernel.const_get(query_class_name).new(client).query(*args)
2017-08-17 22:00:37 +05:30
{
success: true,
2017-09-10 17:25:29 +05:30
data: data,
2017-08-17 22:00:37 +05:30
last_update: Time.now.utc
}
rescue Gitlab::PrometheusError => err
{ success: false, result: err.message }
end
2018-03-17 18:26:18 +05:30
def client(environment_id = nil)
if manual_configuration?
Gitlab::PrometheusClient.new(RestClient::Resource.new(api_url))
else
cluster = cluster_with_prometheus(environment_id)
raise Gitlab::PrometheusError, "couldn't find cluster with Prometheus installed" unless cluster
rest_client = client_from_cluster(cluster)
raise Gitlab::PrometheusError, "couldn't create proxy Prometheus client" unless rest_client
Gitlab::PrometheusClient.new(rest_client)
end
end
def prometheus_installed?
return false if template?
return false unless project
project.clusters.enabled.any? { |cluster| cluster.application_prometheus&.installed? }
2017-09-10 17:25:29 +05:30
end
private
2018-03-17 18:26:18 +05:30
def cluster_with_prometheus(environment_id = nil)
clusters = if environment_id
::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
else
project.clusters.enabled.for_all_environments
end
clusters&.detect { |cluster| cluster.application_prometheus&.installed? }
end
def client_from_cluster(cluster)
cluster.application_prometheus.proxy_client
end
2017-09-10 17:25:29 +05:30
def rename_data_to_metrics(metrics)
metrics[:metrics] = metrics.delete :data
metrics
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def synchronize_service_state!
self.active = prometheus_installed? || manual_configuration?
true
end
2017-08-17 22:00:37 +05:30
end