Skip to content

Commit

Permalink
Simplify Prometheus exporter response format negotation
Browse files Browse the repository at this point in the history
  • Loading branch information
grobie committed Feb 24, 2017
1 parent 226451c commit 5c38163
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/prometheus/middleware/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(app, options = {})

def call(env)
if env['PATH_INFO'] == @path
format = negotiate(env['HTTP_ACCEPT'], @acceptable)
format = negotiate(env, @acceptable)
format ? respond_with(format) : not_acceptable(FORMATS)
else
@app.call(env)
Expand All @@ -35,18 +35,16 @@ def call(env)

private

def negotiate(accept, formats)
accept = '*/*' if accept.to_s.empty?

parse(accept).each do |content_type, _|
def negotiate(env, formats)
parse(env.fetch('HTTP_ACCEPT', '*/*')).each do |content_type, _|
return formats[content_type] if formats.key?(content_type)
end

nil
end

def parse(header)
header.to_s.split(/\s*,\s*/).map do |type|
header.split(/\s*,\s*/).map do |type|
attributes = type.split(/\s*;\s*/)
quality = extract_quality(attributes)

Expand Down

0 comments on commit 5c38163

Please sign in to comment.