Commit 2d0741e5 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Rename ConnectionRackMiddleware to RequestsRackMiddleware.

+ fix tests after metrics rename
parent 79465fb3
...@@ -123,7 +123,7 @@ Gitlab::Metrics::UnicornSampler.initialize_instance(Settings.monitoring.unicorn_ ...@@ -123,7 +123,7 @@ Gitlab::Metrics::UnicornSampler.initialize_instance(Settings.monitoring.unicorn_
Gitlab::Application.configure do |config| Gitlab::Application.configure do |config|
# 0 should be Sentry to catch errors in this middleware # 0 should be Sentry to catch errors in this middleware
config.middleware.insert(1, Gitlab::Metrics::ConnectionRackMiddleware) config.middleware.insert(1, Gitlab::Metrics::RequestsRackMiddleware)
end end
if Gitlab::Metrics.enabled? if Gitlab::Metrics.enabled?
......
module Gitlab module Gitlab
module Metrics module Metrics
class ConnectionRackMiddleware class RequestsRackMiddleware
def initialize(app) def initialize(app)
@app = app @app = app
end end
def self.rack_request_count def self.http_request_total
@rack_request_count ||= Gitlab::Metrics.counter(:http_requests_total, 'Rack request count') @http_request_total ||= Gitlab::Metrics.counter(:http_requests_total, 'Request count')
end end
def self.rack_uncaught_errors_count def self.rack_uncaught_errors_count
@rack_uncaught_errors_count ||= Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Rack connections handling uncaught errors count') @rack_uncaught_errors_count ||= Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Request handling uncaught errors count')
end end
def self.rack_execution_time def self.http_request_duration_seconds
@rack_execution_time ||= Gitlab::Metrics.histogram(:http_request_duration_seconds, 'Rack connection handling execution time', @http_request_duration_seconds ||= Gitlab::Metrics.histogram(:http_request_duration_seconds, 'Request handling execution time',
{}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 1.5, 2, 2.5, 3, 5, 7, 10]) {}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
end end
def call(env) def call(env)
method = env['REQUEST_METHOD'].downcase method = env['REQUEST_METHOD'].downcase
started = Time.now.to_f started = Time.now.to_f
begin begin
ConnectionRackMiddleware.rack_request_count.increment(method: method) RequestsRackMiddleware.http_request_total.increment(method: method)
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
elapsed = Time.now.to_f - started
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method, status: status }, elapsed)
[status, headers, body] [status, headers, body]
rescue rescue
ConnectionRackMiddleware.rack_uncaught_errors_count.increment RequestsRackMiddleware.rack_uncaught_errors_count.increment
raise raise
ensure
elapsed = Time.now.to_f - started
ConnectionRackMiddleware.rack_execution_time.observe({method: method, status: status}, elapsed)
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Metrics::ConnectionRackMiddleware do describe Gitlab::Metrics::RequestsRackMiddleware do
let(:app) { double('app') } let(:app) { double('app') }
subject { described_class.new(app) } subject { described_class.new(app) }
...@@ -23,7 +23,7 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do ...@@ -23,7 +23,7 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do
end end
it 'increments requests count' do it 'increments requests count' do
expect(described_class).to receive_message_chain(:rack_request_count, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get')
subject.call(env) subject.call(env)
end end
...@@ -32,16 +32,22 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do ...@@ -32,16 +32,22 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do
execution_time = 10 execution_time = 10
allow(app).to receive(:call) do |*args| allow(app).to receive(:call) do |*args|
Timecop.freeze(execution_time.seconds) Timecop.freeze(execution_time.seconds)
[200, nil, nil]
end end
expect(described_class).to receive_message_chain(:rack_execution_time, :observe).with({status: 200, method: 'get'}, execution_time) expect(described_class).to receive_message_chain(:http_request_duration_seconds, :observe).with({ status: 200, method: 'get' }, execution_time)
subject.call(env) subject.call(env)
end end
end end
context '@app.call throws exception' do context '@app.call throws exception' do
let(:rack_response_count) { double('rack_response_count') } let(:http_request_duration_seconds) { double('http_request_duration_seconds') }
before do
allow(app).to receive(:call).and_raise(StandardError)
allow(described_class).to receive(:http_request_duration_seconds).and_return(http_request_duration_seconds)
end
it 'increments exceptions count' do it 'increments exceptions count' do
expect(described_class).to receive_message_chain(:rack_uncaught_errors_count, :increment) expect(described_class).to receive_message_chain(:rack_uncaught_errors_count, :increment)
...@@ -50,25 +56,13 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do ...@@ -50,25 +56,13 @@ describe Gitlab::Metrics::ConnectionRackMiddleware do
end end
it 'increments requests count' do it 'increments requests count' do
expect(described_class).to receive_message_chain(:rack_request_count, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get')
expect { subject.call(env) }.to raise_error(StandardError)
end
it "does't increment response count" do
expect(described_class.rack_response_count).not_to receive(:increment)
expect { subject.call(env) }.to raise_error(StandardError) expect { subject.call(env) }.to raise_error(StandardError)
end end
it 'measures execution time' do it "does't measure request execution time" do
execution_time = 10 expect(described_class.http_request_duration_seconds).not_to receive(:increment)
allow(app).to receive(:call) do |*args|
Timecop.freeze(execution_time.seconds)
raise StandardError
end
expect(described_class).to receive_message_chain(:rack_execution_time, :observe).with({}, execution_time)
expect { subject.call(env) }.to raise_error(StandardError) expect { subject.call(env) }.to raise_error(StandardError)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment