Class: GitLab::Exporter::WebExporter

Inherits:
Sinatra::Base
  • Object
show all
Extended by:
TLSHelper
Defined in:
lib/gitlab_exporter/web_exporter.rb

Overview

Metrics web exporter

Defined Under Namespace

Classes: MemoryKillerMiddleware, RunGC

Constant Summary collapse

DEFAULT_WEB_SERVER =
"webrick".freeze

Constants included from TLSHelper

TLSHelper::CERT_REGEX

Class Method Summary collapse

Methods included from TLSHelper

load_ca_certs_bundle, validate_tls_config, webrick_tls_config

Class Method Details

.loggerObject



75
76
77
# File 'lib/gitlab_exporter/web_exporter.rb', line 75

def logger
  request.logger
end

.set_puma_tls(config) ⇒ Object

rubocop:disable Naming/AccessorMethodName



115
116
117
118
119
120
121
122
123
124
# File 'lib/gitlab_exporter/web_exporter.rb', line 115

def set_puma_tls(config) # rubocop:disable Naming/AccessorMethodName
  listen_address = config.fetch(:listen_address, "0.0.0.0")
  listen_port = config.fetch(:listen_port, 8443)
  tls_cert_path = CGI.escape(config.fetch(:tls_cert_path))
  tls_key_path = CGI.escape(config.fetch(:tls_key_path))

  bind_string = "ssl://#{listen_address}:#{listen_port}?cert=#{tls_cert_path}&key=#{tls_key_path}"

  set(:bind, bind_string)
end

.set_tls_config(config) ⇒ Object

rubocop:disable Naming/AccessorMethodName



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab_exporter/web_exporter.rb', line 94

def set_tls_config(config) # rubocop:disable Naming/AccessorMethodName
  validate_tls_config(config)

  web_server = config.fetch(:name, DEFAULT_WEB_SERVER)
  if web_server == "webrick"
    set_webrick_tls(config)
  elsif web_server == "puma"
    set_puma_tls(config)
  else
    fail "TLS not supported for web server `#{web_server}`."
  end
end

.set_webrick_tls(config) ⇒ Object

rubocop:disable Naming/AccessorMethodName



107
108
109
110
111
112
113
# File 'lib/gitlab_exporter/web_exporter.rb', line 107

def set_webrick_tls(config) # rubocop:disable Naming/AccessorMethodName
  server_settings = {}
  server_settings.merge!(webrick_tls_config(config))

  set(:bind, config.fetch(:listen_address, "0.0.0.0"))
  set(:server_settings, server_settings)
end

.setup(config) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab_exporter/web_exporter.rb', line 62

def setup(config)
  setup_server(config[:server])
  setup_probes(config[:probes])

  memory_threshold = (config[:server] && config[:server][:memory_threshold]) || 1024
  use MemoryKillerMiddleware, memory_threshold
  use Rack::Logger
  use RunGC

  # Defrag heap after everything is loaded into memory.
  GC.compact
end

.setup_probes(config) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gitlab_exporter/web_exporter.rb', line 126

def setup_probes(config)
  (config || {}).each do |probe_name, params|
    opts =
      if params.delete(:multiple)
        params
      else
        { probe_name => params }
      end

    get "/#{probe_name}" do
      content_type "text/plain; version=0.0.4"
      prober = Prober.new(metrics: PrometheusMetrics.new(include_timestamp: false), logger: logger, **opts)

      prober.probe_all
      prober.write_to(response)

      response
    end
  end
end

.setup_server(config) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gitlab_exporter/web_exporter.rb', line 79

def setup_server(config)
  config ||= {}

  set(:server, config.fetch(:name, DEFAULT_WEB_SERVER))
  set(:port, config.fetch(:listen_port, 9168))

  # Depending on whether TLS is enabled or not, bind string
  # will be different.
  if config.fetch(:tls_enabled, "false").to_s == "true"
    set_tls_config(config)
  else
    set(:bind, config.fetch(:listen_address, "0.0.0.0"))
  end
end