Skip to content

Commit

Permalink
Merge 42860ce into 8b20201
Browse files Browse the repository at this point in the history
  • Loading branch information
tragiclifestories committed Dec 26, 2020
2 parents 8b20201 + 42860ce commit 145a015
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/prometheus/middleware/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ def initialize(app, options = {})
@app = app
@registry = options[:registry] || Client.registry
@path = options[:path] || '/metrics'
@port = options[:port]
@acceptable = build_dictionary(FORMATS, FALLBACK)
end

def call(env)
if env['PATH_INFO'] == @path
if metrics_port?(env['SERVER_PORT']) && env['PATH_INFO'] == @path
format = negotiate(env, @acceptable)
format ? respond_with(format) : not_acceptable(FORMATS)
else
Expand Down Expand Up @@ -86,6 +87,10 @@ def build_dictionary(formats, fallback)
memo[format::MEDIA_TYPE] = format
end
end

def metrics_port?(request_port)
@port.nil? || @port.to_s == request_port
end
end
end
end
28 changes: 27 additions & 1 deletion spec/prometheus/middleware/exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
describe Prometheus::Middleware::Exporter do
include Rack::Test::Methods

let(:options) { { registry: registry } }
let(:registry) do
Prometheus::Client::Registry.new
end

let(:app) do
app = ->(_) { [200, { 'Content-Type' => 'text/html' }, ['OK']] }
described_class.new(app, registry: registry)
described_class.new(app, **options)
end

context 'when requesting app endpoints' do
Expand Down Expand Up @@ -96,5 +97,30 @@

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

context 'when a port is specified' do
let(:options) { { registry: registry, port: 9999 } }

context 'when a request is on the specified port' do
it 'responds with 200 OK' do
registry.counter(:foo, docstring: 'foo counter').increment(by: 9)

get 'http://example.org:9999/metrics', nil, {}

expect(last_response.status).to eql(200)
expect(last_response.header['Content-Type']).to eql(text::CONTENT_TYPE)
expect(last_response.body).to eql(text.marshal(registry))
end
end

context 'when a request is not on the specified port' do
it 'returns the app response' do
get 'http://example.org:8888/metrics', nil, {}

expect(last_response).to be_ok
expect(last_response.body).to eql('OK')
end
end
end
end
end

0 comments on commit 145a015

Please sign in to comment.