Sha256: f6c7a6af13f1bff377d77a3e53af0025e44c112c3e6efb2b4a046e53ae0415fa

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'rack'
require 'prometheus/client/rack/collector'
require 'prometheus/client/rack/exporter'

module Yabeda
  module Prometheus
    module Mmap
      # Rack application or middleware that provides metrics exposition endpoint
      class Exporter < ::Prometheus::Client::Rack::Exporter
        NOT_FOUND_HANDLER = lambda do |_env|
          [404, { 'Content-Type' => 'text/plain' }, ["Not Found\n"]]
        end.freeze

        class << self
          # Allows to use middleware as standalone rack application
          def call(env)
            @app ||= new(NOT_FOUND_HANDLER, path: '/')
            @app.call(env)
          end

          def start_metrics_server!
            Thread.new do
              default_port = ENV.fetch('PORT', 9394)
              ::Rack::Handler::WEBrick.run(
                rack_app,
                Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0',
                Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port),
                AccessLog: []
              )
            end
          end

          def rack_app(exporter = self, path: '/metrics')
            ::Rack::Builder.new do
              use ::Rack::CommonLogger
              use ::Rack::ShowExceptions
              use exporter, path: path
              run NOT_FOUND_HANDLER
            end
          end
        end

        def initialize(app, options = {})
          super(app, options.merge(registry: Yabeda::Prometheus::Mmap.registry))
        end

        def call(env)
          ::Yabeda.collect! if env['PATH_INFO'] == path

          if ::Yabeda.debug?
            result = nil
            ::Yabeda.yabeda_prometheus_mmap.render_duration.measure({}) do
              result = super
            end
            result
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yabeda-prometheus-mmap-0.2.0 lib/yabeda/prometheus/mmap/exporter.rb