proxy_variable_substitution_service.rb 3.4 KB
Newer Older
1 2 3 4 5 6
# frozen_string_literal: true

module Prometheus
  class ProxyVariableSubstitutionService < BaseService
    include Stepable

7 8 9 10 11 12 13 14 15 16
    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

17 18
    steps :validate_variables,
      :add_params_to_result,
19
      :substitute_params,
20 21
      :substitute_ruby_variables,
      :substitute_liquid_variables
22 23 24 25 26 27 28 29 30 31 32

    def initialize(environment, params = {})
      @environment, @params = environment, params.deep_dup
    end

    def execute
      execute_steps
    end

    private

33 34 35 36 37 38 39 40 41 42
    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

43 44 45 46 47 48
    def add_params_to_result(result)
      result[:params] = params

      success(result)
    end

49 50 51 52 53 54 55 56 57 58
    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

59 60 61
    def substitute_liquid_variables(result)
      return success(result) unless query(result)

62
      result[:params][:query] = gsub(query(result), full_context)
63 64 65 66

      success(result)
    end

67
    def substitute_ruby_variables(result)
68
      return success(result) unless query(result)
69 70 71

      # The % operator doesn't replace variables if the hash contains string
      # keys.
72
      result[:params][:query] = query(result) % predefined_context.symbolize_keys
73 74 75 76

      success(result)
    rescue TypeError, ArgumentError => exception
      log_error(exception.message)
77 78
      Gitlab::ErrorTracking.track_exception(exception, {
        template_string: query(result),
79 80 81 82 83 84
        variables: predefined_context
      })

      error(_('Malformed string'))
    end

85 86 87 88 89 90 91 92 93 94 95 96
    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
    end

97 98 99 100
    def predefined_context
      @predefined_context ||= Gitlab::Prometheus::QueryVariables.call(@environment)
    end

101
    def full_context
102
      @full_context ||= predefined_context.stringify_keys.reverse_merge(variables_hash)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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]
120 121 122
    end
  end
end