Welcome to mirror list, hosted at ThFree Co, Russian Federation.

prometheus_api_controller.rb « environments « projects « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c6c6513a78a5e72f153192ed21909119c64ece7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

class Projects::Environments::PrometheusApiController < Projects::ApplicationController
  before_action :authorize_read_prometheus!
  before_action :environment

  def proxy
    result = Prometheus::ProxyService.new(
      environment,
      proxy_method,
      proxy_path,
      proxy_params
    ).execute

    if result.nil?
      return render status: :no_content, json: {
        status: _('processing'),
        message: _('Not ready yet. Try again later.')
      }
    end

    if result[:status] == :success
      render status: result[:http_status], json: result[:body]
    else
      render(
        status: result[:http_status] || :bad_request,
        json: { status: result[:status], message: result[:message] }
      )
    end
  end

  private

  def query_context
    Gitlab::Prometheus::QueryVariables.call(environment)
  end

  def environment
    @environment ||= project.environments.find(params[:id])
  end

  def proxy_method
    request.method
  end

  def proxy_path
    params[:proxy_path]
  end

  def proxy_params
    substitute_query_variables(params).permit!
  end

  def substitute_query_variables(params)
    query = params[:query]
    return params unless query

    params.merge(query: query % query_context)
  end
end