Skip to content

Commit

Permalink
Fallback to JSON format for '*/*' content types
Browse files Browse the repository at this point in the history
This change fixes the broken browser metric inspection, as the fallback
content type */* wasn't handled correctly. The normal Prometheus server
- client communication wasn't affected.
  • Loading branch information
grobie committed May 24, 2014
1 parent 68a09df commit a0c61ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/prometheus/client/rack/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def initialize(app, options = {})
@app = app
@registry = options[:registry] || Client.registry
@path = options[:path] || '/metrics'
@accpetable = build_dictionary(FORMATS)
@acceptable = build_dictionary(FORMATS, FALLBACK)
end

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

private

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

parse(accept).each do |content_type, _|
return formats[content_type] if formats.key?(content_type)
Expand Down Expand Up @@ -81,8 +81,8 @@ def not_acceptable(formats)
]
end

def build_dictionary(formats)
formats.each_with_object({}) do |format, memo|
def build_dictionary(formats, fallback)
formats.each_with_object('*/*' => fallback) do |format, memo|
memo[format::CONTENT_TYPE] = format
memo[format::MEDIA_TYPE] = format
end
Expand Down
14 changes: 13 additions & 1 deletion spec/prometheus/client/rack/exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@
end
end

context 'when client does send a Accept header' do
context 'when client does not send a Accept header' do
include_examples 'ok', {}, json
end

context 'when client accpets any media type' do
accept = '*/*'

include_examples 'ok', { 'HTTP_ACCEPT' => accept }, json
end

context 'when client requests application/json' do
accept = 'application/json'

Expand Down Expand Up @@ -110,5 +116,11 @@

include_examples 'not acceptable', 'HTTP_ACCEPT' => accept
end

context 'when client accepts unknown formats and wildcard' do
accept = 'fancy/woo;q=0.3, proto/buf;q=0.7, */*;q=0.1'

include_examples 'ok', { 'HTTP_ACCEPT' => accept }, json
end
end
end

0 comments on commit a0c61ae

Please sign in to comment.