18794: Add version and config metrics to workbench1.
[arvados.git] / apps / workbench / app / controllers / management_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'app_version'
6
7 class ManagementController < ApplicationController
8   skip_around_action :thread_clear
9   skip_around_action :set_thread_api_token
10   skip_around_action :require_thread_api_token
11   skip_before_action :ensure_arvados_api_exists
12   skip_before_action :accept_uuid_as_id_param
13   skip_before_action :check_user_agreements
14   skip_before_action :check_user_profile
15   skip_before_action :load_filters_and_paging_params
16   skip_before_action :find_object_by_uuid
17
18   before_action :check_auth_header
19
20   def check_auth_header
21     mgmt_token = Rails.configuration.ManagementToken
22     auth_header = request.headers['Authorization']
23
24     if mgmt_token.empty?
25       render :json => {:errors => "disabled"}, :status => 404
26     elsif !auth_header
27       render :json => {:errors => "authorization required"}, :status => 401
28     elsif auth_header != 'Bearer '+mgmt_token
29       render :json => {:errors => "authorization error"}, :status => 403
30     end
31   end
32
33   def metrics
34     render content_type: 'text/plain', plain: <<~EOF
35 # HELP arvados_config_load_timestamp_seconds Time when config file was loaded.
36 # TYPE arvados_config_load_timestamp_seconds gauge
37 arvados_config_load_timestamp_seconds{sha256="#{Rails.configuration.SourceSHA256}"} #{Rails.configuration.LoadTimestamp.to_f}
38 # HELP arvados_config_source_timestamp_seconds Timestamp of config file when it was loaded.
39 # TYPE arvados_config_source_timestamp_seconds gauge
40 arvados_config_source_timestamp_seconds{sha256="#{Rails.configuration.SourceSHA256}"} #{Rails.configuration.SourceTimestamp.to_f}
41 # HELP arvados_version_running Indicated version is running.
42 # TYPE arvados_version_running gauge
43 arvados_version_running{version="#{AppVersion.package_version}"} 1
44 EOF
45   end
46
47   def health
48     resp = {"health" => "OK"}
49     render json: resp
50   end
51 end