diff options
Diffstat (limited to 'lib/gitlab/prometheus')
-rw-r--r-- | lib/gitlab/prometheus/query_dispatch.rb | 50 | ||||
-rw-r--r-- | lib/gitlab/prometheus/querying_adapter.rb | 2 |
2 files changed, 51 insertions, 1 deletions
diff --git a/lib/gitlab/prometheus/query_dispatch.rb b/lib/gitlab/prometheus/query_dispatch.rb new file mode 100644 index 00000000000..df0d3f21602 --- /dev/null +++ b/lib/gitlab/prometheus/query_dispatch.rb @@ -0,0 +1,50 @@ +module Gitlab + module Prometheus + module QueryDispatch + extend ActiveSupport::Concern + + # def environment_metrics(environment) + # query(environment).environment_metrics(environment) + # end + # + # def deployment_metrics(deployment) + # query(deployment.environment).deployment_metrics(deployment) + # end + # + # def additional_environment_metrics(environment) + # query(environment).additional_environment_metrics(environment) + # end + # + # def additional_deployment_metrics(deployment) + # query(deployment.environment).additional_deployment_metrics(deployment) + # end + # + # def matched_metrics + # query.matched_metrics + # end + # + + included do + def query_prometheus(environment = false) + prometheus_application(environment) || prometheus_service + QueryingAdapter.new(prometheus_application(environment) || prometheus_service) + end + + private + + def prometheus_application(environment = nil) + clusters = if environment + # sort results by descending order based on environment_scope being longer + # thus more closely matching environment slug + project.clusters.enabled.for_environment(environment).sort_by { |c| c.environment_scope&.length }.reverse! + else + project.clusters.enabled.for_all_environments + end + + cluster = clusters&.detect { |cluster| cluster.application_prometheus&.installed? } + cluster&.application_prometheus + end + end + end + end +end diff --git a/lib/gitlab/prometheus/querying_adapter.rb b/lib/gitlab/prometheus/querying_adapter.rb index 97aaf6d319e..aaf40619f42 100644 --- a/lib/gitlab/prometheus/querying_adapter.rb +++ b/lib/gitlab/prometheus/querying_adapter.rb @@ -33,7 +33,7 @@ module Gitlab end # Cache metrics for specific environment - def calculate_reactive_cache(client, query_class_name, environment_id, *args) + def self.calculate_reactive_cache(client, query_class_name, environment_id, *args) return unless active? && project && !project.pending_delete? data = Kernel.const_get(query_class_name).new(client).query(environment_id, *args) |