Class: Yabeda::Prometheus::Mmap::Exporter

Inherits:
Prometheus::Client::Rack::Exporter
  • Object
show all
Defined in:
lib/yabeda/prometheus/mmap/exporter.rb

Overview

Rack application or middleware that provides metrics exposition endpoint

Direct Known Subclasses

Exporter

Constant Summary collapse

NOT_FOUND_HANDLER =
lambda do |_env|
  [404, { 'Content-Type' => 'text/plain' }, ["Not Found\n"]]
end.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Exporter

Returns a new instance of Exporter.



58
59
60
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 58

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

Class Method Details

.call(env) ⇒ Object

Allows to use middleware as standalone rack application



18
19
20
21
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 18

def call(env)
  @app ||= new(NOT_FOUND_HANDLER, path: '/')
  @app.call(env)
end

.rack_app(exporter = self, path: '/metrics') ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 48

def rack_app(exporter = self, path: '/metrics')
  ::Rack::Builder.new do
    use ::Rack::CommonLogger if ENV['PROMETHEUS_EXPORTER_LOG_REQUESTS'] != 'false'
    use ::Rack::ShowExceptions
    use exporter, path: path
    run NOT_FOUND_HANDLER
  end
end

.rack_handlerObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 36

def rack_handler
  if Gem.loaded_specs['rack']&.version&.>= Gem::Version.new('3.0')
    require 'rackup'
    ::Rackup::Handler::WEBrick
  else
    ::Rack::Handler::WEBrick
  end
rescue LoadError
  warn 'Please add gems rackup and webrick to your Gemfile to expose Yabeda metrics from prometheus-mmap'
  ::Rack::Handler::WEBrick
end

.start_metrics_server!Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 23

def start_metrics_server!
  Thread.new do
    default_port = ENV.fetch('PORT', 9394)

    rack_handler.run(
      rack_app,
      Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0',
      Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port),
      AccessLog: []
    )
  end
end

Instance Method Details

#call(env) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yabeda/prometheus/mmap/exporter.rb', line 62

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