lib/sidekiq/prometheus/exporter.rb in sidekiq-prometheus-exporter-0.1.7 vs lib/sidekiq/prometheus/exporter.rb in sidekiq-prometheus-exporter-0.1.8

- old
+ new

@@ -1,42 +1,44 @@ # frozen_string_literal: true -require 'erb' require 'sidekiq/prometheus/exporter/version' -require 'sidekiq/prometheus/exporter/metrics' +require 'sidekiq/prometheus/exporter/exporters' module Sidekiq module Prometheus # Expose Prometheus metrics via Rack application or Sidekiq::Web application module Exporter - HTTP_GET = 'GET'.freeze - NOT_FOUND_TEXT = 'Not Found'.freeze + REQUEST_VERB = 'GET'.freeze REQUEST_METHOD = 'REQUEST_METHOD'.freeze - HEADERS = { - 'Content-Type' => 'text/plain; version=0.0.4', - 'Cache-Control' => 'no-cache' - }.freeze - TEMPLATE = ERB.new(File.read(File.expand_path('exporter/templates/metrics.erb', __dir__))) + NOT_FOUND_TEXT = 'Not Found'.freeze + MOUNT_PATH = '/metrics'.freeze + HEADERS = {'Content-Type' => 'text/plain; version=0.0.4', 'Cache-Control' => 'no-cache'}.freeze + EXPORTERS = Exporters.new - def self.registered(app) - app.get('/metrics') do - Sidekiq::Prometheus::Exporter.call(REQUEST_METHOD => HTTP_GET) + class << self + def configure + yield(EXPORTERS) end - end - def self.to_app - Rack::Builder.app do - map('/metrics') do - run Sidekiq::Prometheus::Exporter + def registered(app) + app.get(MOUNT_PATH) do + Sidekiq::Prometheus::Exporter.call(REQUEST_METHOD => REQUEST_VERB) end end - end - def self.call(env) - return [404, HEADERS, [NOT_FOUND_TEXT]] if env[REQUEST_METHOD] != HTTP_GET + def to_app + Rack::Builder.app do + map(MOUNT_PATH) do + run Sidekiq::Prometheus::Exporter + end + end + end - body = TEMPLATE.result(Metrics.new.__binding__).chomp! - [200, HEADERS, [body]] + def call(env) + return [404, HEADERS, [NOT_FOUND_TEXT]] if env[REQUEST_METHOD] != REQUEST_VERB + + [200, HEADERS, [EXPORTERS.to_s]] + end end end end end