From a0c61ae85f9c058800fa215827508410f04bf84f Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Fri, 23 May 2014 21:39:38 -0400 Subject: [PATCH] Fallback to JSON format for '*/*' content types 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. --- lib/prometheus/client/rack/exporter.rb | 12 ++++++------ spec/prometheus/client/rack/exporter_spec.rb | 14 +++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/prometheus/client/rack/exporter.rb b/lib/prometheus/client/rack/exporter.rb index 3a06f472..d42849f0 100644 --- a/lib/prometheus/client/rack/exporter.rb +++ b/lib/prometheus/client/rack/exporter.rb @@ -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) @@ -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) @@ -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 diff --git a/spec/prometheus/client/rack/exporter_spec.rb b/spec/prometheus/client/rack/exporter_spec.rb index 5b75e897..256933c9 100644 --- a/spec/prometheus/client/rack/exporter_spec.rb +++ b/spec/prometheus/client/rack/exporter_spec.rb @@ -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' @@ -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