Sha256: 07a11e1be7a614257af2c5a83f29ae8097be829cadad607ad077830285c1927b

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'erb'
require 'sidekiq/prometheus/exporter/version'
require 'sidekiq/prometheus/exporter/metrics'

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_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__)))

      def self.registered(app)
        app.get('/metrics') do
          Sidekiq::Prometheus::Exporter.call(REQUEST_METHOD => HTTP_GET)
        end
      end

      def self.to_app
        Rack::Builder.app do
          map('/metrics') do
            run Sidekiq::Prometheus::Exporter
          end
        end
      end

      def self.call(env)
        return [404, HEADERS, [NOT_FOUND_TEXT]] if env[REQUEST_METHOD] != HTTP_GET

        body = TEMPLATE.result(Metrics.new.__binding__).chomp!
        [200, HEADERS, [body]]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidekiq-prometheus-exporter-0.1.7 lib/sidekiq/prometheus/exporter.rb
sidekiq-prometheus-exporter-0.1.6 lib/sidekiq/prometheus/exporter.rb
sidekiq-prometheus-exporter-0.1.4 lib/sidekiq/prometheus/exporter.rb
sidekiq-prometheus-exporter-0.1.3 lib/sidekiq/prometheus/exporter.rb
sidekiq-prometheus-exporter-0.1.2 lib/sidekiq/prometheus/exporter.rb