2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DeploymentMetrics
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
attr_reader :project, :deployment
|
|
|
|
|
|
|
|
delegate :cluster, to: :deployment
|
|
|
|
|
|
|
|
def initialize(project, deployment)
|
|
|
|
@project = project
|
|
|
|
@deployment = deployment
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_metrics?
|
|
|
|
deployment.success? && prometheus_adapter&.can_query?
|
|
|
|
end
|
|
|
|
|
|
|
|
def metrics
|
|
|
|
return {} unless has_metrics?
|
|
|
|
|
|
|
|
metrics = prometheus_adapter.query(:deployment, deployment)
|
|
|
|
metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def additional_metrics
|
|
|
|
return {} unless has_metrics?
|
|
|
|
|
|
|
|
metrics = prometheus_adapter.query(:additional_metrics_deployment, deployment)
|
|
|
|
metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prometheus_adapter
|
|
|
|
strong_memoize(:prometheus_adapter) do
|
|
|
|
service = project.find_or_initialize_service('prometheus')
|
|
|
|
|
|
|
|
if service.can_query?
|
|
|
|
service
|
|
|
|
else
|
|
|
|
cluster_prometheus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cluster_prometheus
|
2019-10-12 21:52:04 +05:30
|
|
|
cluster.application_prometheus if cluster&.application_prometheus_available?
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|