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-18 12:07:38 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-18 12:07:38 +0300
commit1a9d9cc14ec54036548824e3ce17da03960f5f81 (patch)
tree4b93fa74f393a1978ea9c2628516eb4a449b8704 /app/services/prometheus/proxy_variable_substitution_service.rb
parentdad534d98a3f86bfa079b7ebd980448641cc9c7c (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.rb55
1 files changed, 48 insertions, 7 deletions
diff --git a/app/services/prometheus/proxy_variable_substitution_service.rb b/app/services/prometheus/proxy_variable_substitution_service.rb
index ca56292e9d6..b34afaf80b8 100644
--- a/app/services/prometheus/proxy_variable_substitution_service.rb
+++ b/app/services/prometheus/proxy_variable_substitution_service.rb
@@ -4,7 +4,10 @@ module Prometheus
class ProxyVariableSubstitutionService < BaseService
include Stepable
- steps :add_params_to_result, :substitute_ruby_variables
+ steps :validate_variables,
+ :add_params_to_result,
+ :substitute_ruby_variables,
+ :substitute_liquid_variables
def initialize(environment, params = {})
@environment, @params = environment, params.deep_dup
@@ -16,24 +19,45 @@ module Prometheus
private
+ def validate_variables(_result)
+ return success unless variables
+
+ unless variables.is_a?(Array) && variables.size.even?
+ return error(_('Optional parameter "variables" must be an array of keys and values. Ex: [key1, value1, key2, value2]'))
+ end
+
+ success
+ end
+
def add_params_to_result(result)
result[:params] = params
success(result)
end
+ def substitute_liquid_variables(result)
+ return success(result) unless query(result)
+
+ result[:params][:query] =
+ TemplateEngines::LiquidService.new(query(result)).render(full_context)
+
+ success(result)
+ rescue TemplateEngines::LiquidService::RenderError => e
+ error(e.message)
+ end
+
def substitute_ruby_variables(result)
- return success(result) unless query
+ return success(result) unless query(result)
# The % operator doesn't replace variables if the hash contains string
# keys.
- result[:params][:query] = query % predefined_context.symbolize_keys
+ result[:params][:query] = query(result) % predefined_context.symbolize_keys
success(result)
rescue TypeError, ArgumentError => exception
log_error(exception.message)
- Gitlab::ErrorTracking.track_exception(exception, extra: {
- template_string: query,
+ Gitlab::ErrorTracking.track_exception(exception, {
+ template_string: query(result),
variables: predefined_context
})
@@ -44,8 +68,25 @@ module Prometheus
@predefined_context ||= Gitlab::Prometheus::QueryVariables.call(@environment)
end
- def query
- params[:query]
+ def full_context
+ @full_context ||= predefined_context.reverse_merge(variables_hash)
+ end
+
+ def variables
+ params[:variables]
+ end
+
+ def variables_hash
+ # .each_slice(2) converts ['key1', 'value1', 'key2', 'value2'] into
+ # [['key1', 'value1'], ['key2', 'value2']] which is then converted into
+ # a hash by to_h: {'key1' => 'value1', 'key2' => 'value2'}
+ # to_h will raise an ArgumentError if the number of elements in the original
+ # array is not even.
+ variables&.each_slice(2).to_h
+ end
+
+ def query(result)
+ result[:params][:query]
end
end
end