2019-12-08 22:07:57 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Prometheus
|
|
|
|
class ProxyVariableSubstitutionService < BaseService
|
|
|
|
include Stepable
|
|
|
|
|
2020-05-11 17:09:40 -04:00
|
|
|
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
|
|
|
|
|
2019-12-18 04:07:38 -05:00
|
|
|
steps :validate_variables,
|
|
|
|
:add_params_to_result,
|
2020-03-23 20:09:24 -04:00
|
|
|
:substitute_params,
|
2020-05-12 20:07:50 -04:00
|
|
|
:substitute_variables
|
2019-12-08 22:07:57 -05:00
|
|
|
|
2020-07-03 14:08:58 -04:00
|
|
|
# @param environment [Environment]
|
|
|
|
# @param params [Hash<Symbol,Any>]
|
|
|
|
# @param params - query [String] The Prometheus query string.
|
|
|
|
# @param params - start [String] (optional) A time string in the rfc3339 format.
|
|
|
|
# @param params - start_time [String] (optional) A time string in the rfc3339 format.
|
|
|
|
# @param params - end [String] (optional) A time string in the rfc3339 format.
|
|
|
|
# @param params - end_time [String] (optional) A time string in the rfc3339 format.
|
|
|
|
# @param params - variables [ActionController::Parameters] (optional) Variables with their values.
|
|
|
|
# The keys in the Hash should be the name of the variable. The value should be the value of the
|
|
|
|
# variable. Ex: `ActionController::Parameters.new(variable1: 'value 1', variable2: 'value 2').permit!`
|
|
|
|
# @return [Prometheus::ProxyVariableSubstitutionService]
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# Prometheus::ProxyVariableSubstitutionService.new(environment, {
|
|
|
|
# params: {
|
|
|
|
# start_time: '2020-07-03T06:08:36Z',
|
|
|
|
# end_time: '2020-07-03T14:08:52Z',
|
|
|
|
# query: 'up{instance="{{instance}}"}',
|
|
|
|
# variables: { instance: 'srv1' }
|
|
|
|
# }
|
|
|
|
# })
|
2019-12-08 22:07:57 -05:00
|
|
|
def initialize(environment, params = {})
|
|
|
|
@environment, @params = environment, params.deep_dup
|
|
|
|
end
|
|
|
|
|
2020-07-03 14:08:58 -04:00
|
|
|
# @return - params [Hash<Symbol,Any>] Returns a Hash containing a params key which is
|
|
|
|
# similar to the `params` that is passed to the initialize method with 2 differences:
|
|
|
|
# 1. Variables in the query string are substituted with their values.
|
|
|
|
# If a variable present in the query string has no known value (values
|
|
|
|
# are obtained from the `variables` Hash in `params` or from
|
|
|
|
# `Gitlab::Prometheus::QueryVariables.call`), it will not be substituted.
|
|
|
|
# 2. `start` and `end` keys are added, with their values copied from `start_time`
|
|
|
|
# and `end_time`.
|
|
|
|
#
|
|
|
|
# Example output:
|
|
|
|
#
|
|
|
|
# {
|
|
|
|
# params: {
|
|
|
|
# start_time: '2020-07-03T06:08:36Z',
|
|
|
|
# start: '2020-07-03T06:08:36Z',
|
|
|
|
# end_time: '2020-07-03T14:08:52Z',
|
|
|
|
# end: '2020-07-03T14:08:52Z',
|
|
|
|
# query: 'up{instance="srv1"}',
|
|
|
|
# variables: { instance: 'srv1' }
|
|
|
|
# }
|
|
|
|
# }
|
2019-12-08 22:07:57 -05:00
|
|
|
def execute
|
|
|
|
execute_steps
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-18 04:07:38 -05:00
|
|
|
def validate_variables(_result)
|
|
|
|
return success unless variables
|
|
|
|
|
2020-05-27 14:08:14 -04:00
|
|
|
unless variables.is_a?(ActionController::Parameters)
|
|
|
|
return error(_('Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'))
|
2019-12-18 04:07:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2019-12-08 22:07:57 -05:00
|
|
|
def add_params_to_result(result)
|
|
|
|
result[:params] = params
|
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-03-23 20:09:24 -04:00
|
|
|
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-12 20:07:50 -04:00
|
|
|
def substitute_variables(result)
|
2019-12-18 04:07:38 -05:00
|
|
|
return success(result) unless query(result)
|
|
|
|
|
2020-06-04 02:08:42 -04:00
|
|
|
result[:params][:query] = gsub(query(result), full_context(result))
|
2019-12-18 04:07:38 -05:00
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-05-11 17:09:40 -04:00
|
|
|
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
|
|
|
|
|
2020-06-04 02:08:42 -04:00
|
|
|
def predefined_context(result)
|
|
|
|
Gitlab::Prometheus::QueryVariables.call(
|
|
|
|
@environment,
|
|
|
|
start_time: start_timestamp(result),
|
|
|
|
end_time: end_timestamp(result)
|
|
|
|
).stringify_keys
|
2019-12-08 22:07:57 -05:00
|
|
|
end
|
|
|
|
|
2020-06-04 02:08:42 -04:00
|
|
|
def full_context(result)
|
|
|
|
@full_context ||= predefined_context(result).reverse_merge(variables_hash)
|
2019-12-18 04:07:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def variables
|
|
|
|
params[:variables]
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables_hash
|
2020-05-27 14:08:14 -04:00
|
|
|
variables.to_h
|
2019-12-18 04:07:38 -05:00
|
|
|
end
|
|
|
|
|
2020-06-04 02:08:42 -04:00
|
|
|
def start_timestamp(result)
|
|
|
|
Time.rfc3339(result[:params][:start])
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_timestamp(result)
|
|
|
|
Time.rfc3339(result[:params][:end])
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
|
2019-12-18 04:07:38 -05:00
|
|
|
def query(result)
|
|
|
|
result[:params][:query]
|
2019-12-08 22:07:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|