mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
Introducing the Prometheus Stats code into ABS showed that the Clarity could be improved a bit with better variable naming, some refactoring to reduce repitition and documenting the Metrics table itself. Filtering these changes back to the vmpooler code base.
59 lines
1.8 KiB
Ruby
59 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Vmpooler
|
|
class API < Sinatra::Base
|
|
# Load API components
|
|
%w[helpers dashboard reroute v1 request_logger].each do |lib|
|
|
require "vmpooler/api/#{lib}"
|
|
end
|
|
# Load dashboard components
|
|
require 'vmpooler/dashboard'
|
|
|
|
def self.execute(torun, config, redis, metrics, logger)
|
|
self.settings.set :config, config
|
|
self.settings.set :redis, redis unless redis.nil?
|
|
self.settings.set :metrics, metrics
|
|
self.settings.set :checkoutlock, Mutex.new
|
|
|
|
# Deflating in all situations
|
|
# https://www.schneems.com/2017/11/08/80-smaller-rails-footprint-with-rack-deflate/
|
|
use Rack::Deflater
|
|
|
|
# not_found clause placed here to fix rspec test issue.
|
|
not_found do
|
|
content_type :json
|
|
|
|
result = {
|
|
ok: false
|
|
}
|
|
|
|
JSON.pretty_generate(result)
|
|
end
|
|
|
|
if metrics.respond_to?(:setup_prometheus_metrics)
|
|
# Prometheus metrics are only setup if actually specified
|
|
# in the config file.
|
|
metrics.setup_prometheus_metrics(torun)
|
|
|
|
# Using customised collector that filters out hostnames on API paths
|
|
require 'vmpooler/metrics/promstats/collector_middleware'
|
|
require 'prometheus/middleware/exporter'
|
|
use Vmpooler::Metrics::Promstats::CollectorMiddleware, metrics_prefix: "#{metrics.prometheus_prefix}_http"
|
|
use Prometheus::Middleware::Exporter, path: metrics.prometheus_endpoint
|
|
end
|
|
|
|
if torun.include? :api
|
|
# Enable API request logging only if required
|
|
use Vmpooler::API::RequestLogger, logger: logger if config[:config]['request_logger']
|
|
|
|
use Vmpooler::Dashboard
|
|
use Vmpooler::API::Dashboard
|
|
use Vmpooler::API::Reroute
|
|
use Vmpooler::API::V1
|
|
end
|
|
|
|
# Get thee started O WebServer
|
|
self.run!
|
|
end
|
|
end
|
|
end
|