Class: Prometheus::ProxyVariableSubstitutionService

Inherits:
BaseService
  • Object
show all
Includes:
Stepable
Defined in:
app/services/prometheus/proxy_variable_substitution_service.rb

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Stepable

#execute_steps, #steps

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(environment, params = {}) ⇒ 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' }
  }
})

Parameters:

  • environment (Environment)
  • params (Hash<Symbol,Any>) (defaults to: {})
  • params (defaults to: {})
    • query [String] The Prometheus query string.

  • params (defaults to: {})
    • start [String] (optional) A time string in the rfc3339 format.

  • params (defaults to: {})
    • start_time [String] (optional) A time string in the rfc3339 format.

  • params (defaults to: {})
    • end [String] (optional) A time string in the rfc3339 format.

  • params (defaults to: {})
    • end_time [String] (optional) A time string in the rfc3339 format.

  • params (defaults to: {})
    • 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!`


43
44
45
# File 'app/services/prometheus/proxy_variable_substitution_service.rb', line 43

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

Instance Method Details

#executeObject

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' }
}

}

Returns:

    • 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`.
    

68
69
70
# File 'app/services/prometheus/proxy_variable_substitution_service.rb', line 68

def execute
  execute_steps
end