Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-09 06:07:57 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-09 06:07:57 +0300
commit330eac18cef61a4f58b3601265f7b631d2311cd0 (patch)
tree87eec5d8c441581938ca908ce30199832e118b85 /app/services/prometheus/proxy_variable_substitution_service.rb
parent3359a5a56337b93cd34b9914b6468395bfb6c514 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/prometheus/proxy_variable_substitution_service.rb')
-rw-r--r--app/services/prometheus/proxy_variable_substitution_service.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/app/services/prometheus/proxy_variable_substitution_service.rb b/app/services/prometheus/proxy_variable_substitution_service.rb
new file mode 100644
index 00000000000..d3d56987f07
--- /dev/null
+++ b/app/services/prometheus/proxy_variable_substitution_service.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+module Prometheus
+ class ProxyVariableSubstitutionService < BaseService
+ include Stepable
+
+ steps :add_params_to_result, :substitute_ruby_variables
+
+ def initialize(environment, params = {})
+ @environment, @params = environment, params.deep_dup
+ end
+
+ def execute
+ execute_steps
+ end
+
+ private
+
+ def add_params_to_result(result)
+ result[:params] = params
+
+ success(result)
+ end
+
+ def substitute_ruby_variables(result)
+ return success(result) unless query
+
+ # The % operator doesn't replace variables if the hash contains string
+ # keys.
+ result[:params][:query] = query % predefined_context.symbolize_keys
+
+ success(result)
+ rescue TypeError, ArgumentError => exception
+ log_error(exception.message)
+ Gitlab::Sentry.track_acceptable_exception(exception, extra: {
+ template_string: query,
+ variables: predefined_context
+ })
+
+ error(_('Malformed string'))
+ end
+
+ def predefined_context
+ @predefined_context ||= Gitlab::Prometheus::QueryVariables.call(@environment)
+ end
+
+ def query
+ params[:query]
+ end
+ end
+end