forked from harmjanblok/puma-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
35 lines (31 loc) · 878 Bytes
/
app.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
# frozen_string_literal: true
require 'json'
require 'prometheus/client/formats/text'
require 'puma/metrics/parser'
module Puma
module Metrics
class App
def initialize(launcher)
@launcher = launcher
clustered = (@launcher.options[:workers] || 0) > 0
@parser = Parser.new clustered
end
def call(_env)
retrieve_and_parse_stats!
[
200,
{ 'Content-Type' => 'text/plain' },
[Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry)]
]
end
def retrieve_and_parse_stats!
puma_stats = @launcher.stats
if puma_stats.is_a?(Hash) # Modern Puma outputs stats as a Symbol-keyed Hash
@parser.parse(puma_stats)
else
@parser.parse(JSON.parse(puma_stats, symbolize_names: true))
end
end
end
end
end