Skip to content

Commit

Permalink
Allow use of Hash-shaped stats
Browse files Browse the repository at this point in the history
Future puma versions will be outputting a Hash instead
of a string of JSON, so the parsing step is no longer needed.

Also update key use to always use symbols, since this allows
better object reuse (and the builtin #stats method will be
returning Symbol-keyed hashes natively)
  • Loading branch information
julik authored and harmjanblok committed Jun 15, 2020
1 parent 5d16f94 commit 7618013
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/puma/metrics/app.rb
Expand Up @@ -14,13 +14,22 @@ def initialize(launcher)
end

def call(_env)
@parser.parse JSON.parse(@launcher.stats)
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
8 changes: 4 additions & 4 deletions lib/puma/metrics/parser.rb
Expand Up @@ -10,10 +10,10 @@ def initialize(clustered = false)
register_clustered_metrics if clustered
end

def parse(stats, labels = {})
stats.each do |key, value|
value.each { |s| parse(s, labels.merge(index: s['index'])) } if key == 'worker_status'
parse(value, labels) if key == 'last_status'
def parse(symbol_keyed_stats, labels = {})
symbol_keyed_stats.each do |key, value|
value.each { |s| parse(s, labels.merge(index: s[:index])) } if key == :worker_status
parse(value, labels) if key == :last_status
update_metric(key, value, labels)
end
end
Expand Down

0 comments on commit 7618013

Please sign in to comment.