151 lines
4.5 KiB
Ruby
151 lines
4.5 KiB
Ruby
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
|
||
|
||
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
|
||
s_('PrometheusService|Prometheus monitoring')
|
||
end
|
||
|
||
def self.to_param
|
||
'prometheus'
|
||
end
|
||
|
||
def fields
|
||
[
|
||
{
|
||
type: 'text',
|
||
name: 'api_url',
|
||
title: 'API URL',
|
||
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.'),
|
||
required: true
|
||
}
|
||
]
|
||
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
|
||
|
||
def with_reactive_cache(cl, *args)
|
||
yield calculate_reactive_cache(cl, *args)
|
||
end
|
||
|
||
def environment_metrics(environment)
|
||
with_reactive_cache(Gitlab::Prometheus::Queries::EnvironmentQuery.name, environment.id, &method(:rename_data_to_metrics))
|
||
end
|
||
|
||
def deployment_metrics(deployment)
|
||
metrics = with_reactive_cache(Gitlab::Prometheus::Queries::DeploymentQuery.name, deployment.environment.id, deployment.id, &method(:rename_data_to_metrics))
|
||
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
|
||
|
||
def additional_deployment_metrics(deployment)
|
||
with_reactive_cache(Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery.name, deployment.environment.id, deployment.id, &:itself)
|
||
end
|
||
|
||
def matched_metrics
|
||
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, nil, &:itself)
|
||
end
|
||
|
||
def manual_mode?
|
||
false
|
||
end
|
||
|
||
# Cache metrics for specific environment
|
||
def calculate_reactive_cache(query_class_name, environment_id, *args)
|
||
return unless active? && project && !project.pending_delete?
|
||
client = client_for_environment(environment_id)
|
||
|
||
|
||
data = Kernel.const_get(query_class_name).new(client).query(environment_id, *args)
|
||
{
|
||
success: true,
|
||
data: data,
|
||
last_update: Time.now.utc
|
||
}
|
||
rescue Gitlab::PrometheusError => err
|
||
{ success: false, result: err.message }
|
||
end
|
||
|
||
def client(environment_id)
|
||
if manual_mode?
|
||
Gitlab::PrometheusClient.new(api_url: api_url)
|
||
else
|
||
cluster(environment_id)
|
||
end
|
||
end
|
||
|
||
def find_cluster_with_prometheus(environment_id)
|
||
clusters = if environment_id
|
||
::Environment.find_by(id: environment_id).try(:enabled_clusters) || []
|
||
else
|
||
project.clusters.enabled.select { |c| c.environment_scope == '*' || c.environment_scope == '' }
|
||
end
|
||
|
||
clusters.detect { |cluster| cluster.application_prometheus.installed? }
|
||
end
|
||
|
||
private
|
||
|
||
def client_for_environment(environment_id)
|
||
cluster = find_cluster_with_prometheus(environment_id)
|
||
return unless cluster
|
||
|
||
prometheus = cluster.application_prometheus
|
||
|
||
client_through_kube_proxy(cluster.kubeclient,
|
||
'service',
|
||
prometheus.service_name,
|
||
prometheus.service_port,
|
||
prometheus.namespace) if cluster.kubeclient
|
||
end
|
||
|
||
def client_through_kube_proxy(kube_client, kind, name, port, namespace = '')
|
||
rest_client = kube_client.rest_client
|
||
base_url = rest_client.url
|
||
proxy_url = kube_client.proxy_url(kind, name, port, namespace)
|
||
|
||
Rails.logger.warn rest_client[proxy_url.sub(base_url, '')]
|
||
Rails.logger.warn proxy_url.sub(base_url, '')
|
||
|
||
Gitlab::PrometheusClient.new(api_url: api_url, rest_client: rest_client[proxy_url.sub(base_url, '')], headers: kube_client.headers)
|
||
end
|
||
|
||
|
||
def rename_data_to_metrics(metrics)
|
||
metrics[:metrics] = metrics.delete :data
|
||
metrics
|
||
end
|
||
end
|