2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Prometheus
|
|
|
|
class ProxyVariableSubstitutionService < BaseService
|
|
|
|
include Stepable
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
VARIABLE_INTERPOLATION_REGEX = /
|
|
|
|
{{ # Variable needs to be wrapped in these chars.
|
|
|
|
\s* # Allow whitespace before and after the variable name.
|
|
|
|
(?<variable> # Named capture.
|
|
|
|
\w+ # Match one or more word characters.
|
|
|
|
)
|
|
|
|
\s*
|
|
|
|
}}
|
|
|
|
/x.freeze
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
steps :validate_variables,
|
|
|
|
:add_params_to_result,
|
2020-04-22 19:07:51 +05:30
|
|
|
:substitute_params,
|
2020-05-24 23:13:21 +05:30
|
|
|
:substitute_variables
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
def initialize(environment, params = {})
|
|
|
|
@environment, @params = environment, params.deep_dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
execute_steps
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def validate_variables(_result)
|
|
|
|
return success unless variables
|
|
|
|
|
2020-05-30 21:06:31 +05:30
|
|
|
unless variables.is_a?(ActionController::Parameters)
|
|
|
|
return error(_('Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'))
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def add_params_to_result(result)
|
|
|
|
result[:params] = params
|
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def substitute_params(result)
|
|
|
|
start_time = result[:params][:start_time]
|
|
|
|
end_time = result[:params][:end_time]
|
|
|
|
|
|
|
|
result[:params][:start] = start_time if start_time
|
|
|
|
result[:params][:end] = end_time if end_time
|
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def substitute_variables(result)
|
2020-03-13 15:44:24 +05:30
|
|
|
return success(result) unless query(result)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result[:params][:query] = gsub(query(result), full_context)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def gsub(string, context)
|
|
|
|
# Search for variables of the form `{{variable}}` in the string and replace
|
|
|
|
# them with their value.
|
|
|
|
string.gsub(VARIABLE_INTERPOLATION_REGEX) do |match|
|
|
|
|
# Replace with the value of the variable, or if there is no such variable,
|
|
|
|
# replace the invalid variable with itself. So,
|
|
|
|
# `up{instance="{{invalid_variable}}"}` will remain
|
|
|
|
# `up{instance="{{invalid_variable}}"}` after substitution.
|
|
|
|
context.fetch($~[:variable], match)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def predefined_context
|
2020-05-24 23:13:21 +05:30
|
|
|
Gitlab::Prometheus::QueryVariables.call(@environment).stringify_keys
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def full_context
|
|
|
|
@full_context ||= predefined_context.reverse_merge(variables_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables
|
|
|
|
params[:variables]
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables_hash
|
2020-05-30 21:06:31 +05:30
|
|
|
variables.to_h
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def query(result)
|
|
|
|
result[:params][:query]
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|