class Yabeda::Prometheus::Exporter

Rack application or middleware that provides metrics exposition endpoint

Constants

NOT_FOUND_HANDLER

Public Class Methods

call(env) click to toggle source

Allows to use middleware as standalone rack application

# File lib/yabeda/prometheus/exporter.rb, line 16
def call(env)
  @app ||= new(NOT_FOUND_HANDLER, path: "/")
  @app.call(env)
end
new(app, options = {}) click to toggle source
Calls superclass method
# File lib/yabeda/prometheus/exporter.rb, line 43
def initialize(app, options = {})
  super(app, options.merge(registry: Yabeda::Prometheus.registry))
end
rack_app(exporter = self, path: "/metrics") click to toggle source
# File lib/yabeda/prometheus/exporter.rb, line 33
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
start_metrics_server!() click to toggle source
# File lib/yabeda/prometheus/exporter.rb, line 21
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

Public Instance Methods

call(env) click to toggle source
Calls superclass method
# File lib/yabeda/prometheus/exporter.rb, line 47
def call(env)
  ::Yabeda.collect! if env["PATH_INFO"] == path

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