-
Notifications
You must be signed in to change notification settings - Fork 17
/
exporter.rb
62 lines (54 loc) · 1.72 KB
/
exporter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require "prometheus/middleware/exporter"
require "rack"
module Yabeda
module Prometheus
# Rack application or middleware that provides metrics exposition endpoint
class Exporter < ::Prometheus::Middleware::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!(**rack_app_options)
Thread.new do
default_port = ENV.fetch("PORT", 9394)
::Rack::Handler::WEBrick.run(
rack_app(**rack_app_options),
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", logger: Logger.new(IO::NULL))
::Rack::Builder.new do
use ::Rack::CommonLogger, logger
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.registry))
end
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
end
end
end