Commit 205ecdb5 authored by Dylan Griffith's avatar Dylan Griffith

Refactor simplify rack metrics for elasticsearch/redis

Firstly we move the initialization of the metrics into initialize so
that they aren't invoked multiple times.

Secondly we remove the conditional logic which is avoiding adding
metrics when metrics are disabled. This is not necessary because these
[rack middleware are only inserted when metrics are
enabled](https://gitlab.com/gitlab-org/gitlab/-/blob/9c8f75e10b4dff4d3d13c6cf1ab75c0da252a352/config/initializers/zz_metrics.rb#L136)
and there will always be a transaction present as it is [unconditionally
created in
`Gitlab::Metrics::RackMiddleware`](https://gitlab.com/gitlab-org/gitlab/-/blob/9c8f75e10b4dff4d3d13c6cf1ab75c0da252a352/lib/gitlab/metrics/rack_middleware.rb#L13).
parent 635a9c8c
...@@ -8,6 +8,14 @@ module Gitlab ...@@ -8,6 +8,14 @@ module Gitlab
def initialize(app) def initialize(app)
@app = app @app = app
@requests_total_counter = Gitlab::Metrics.counter(:http_elasticsearch_requests_total,
'Amount of calls to Elasticsearch servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS)
@requests_duration_histogram = Gitlab::Metrics.histogram(:http_elasticsearch_requests_duration_seconds,
'Query time for Elasticsearch servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS,
HISTOGRAM_BUCKETS)
end end
def call(env) def call(env)
...@@ -15,7 +23,7 @@ module Gitlab ...@@ -15,7 +23,7 @@ module Gitlab
@app.call(env) @app.call(env)
ensure ensure
record_metrics(transaction) if transaction record_metrics(transaction)
end end
private private
...@@ -25,14 +33,8 @@ module Gitlab ...@@ -25,14 +33,8 @@ module Gitlab
query_time = ::Gitlab::Instrumentation::ElasticsearchTransport.query_time query_time = ::Gitlab::Instrumentation::ElasticsearchTransport.query_time
request_count = ::Gitlab::Instrumentation::ElasticsearchTransport.get_request_count request_count = ::Gitlab::Instrumentation::ElasticsearchTransport.get_request_count
Gitlab::Metrics.counter(:http_elasticsearch_requests_total, @requests_total_counter.increment(labels, request_count)
'Amount of calls to Elasticsearch servers during web requests', @requests_duration_histogram.observe(labels, query_time)
Gitlab::Metrics::Transaction::BASE_LABELS).increment(labels, request_count)
Gitlab::Metrics.histogram(:http_elasticsearch_requests_duration_seconds,
'Query time for Elasticsearch servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS,
HISTOGRAM_BUCKETS).observe(labels, query_time)
end end
end end
end end
......
...@@ -6,6 +6,14 @@ module Gitlab ...@@ -6,6 +6,14 @@ module Gitlab
class RedisRackMiddleware class RedisRackMiddleware
def initialize(app) def initialize(app)
@app = app @app = app
@requests_total_counter = Gitlab::Metrics.counter(:http_redis_requests_total,
'Amount of calls to Redis servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS)
@requests_duration_histogram = Gitlab::Metrics.histogram(:http_redis_requests_duration_seconds,
'Query time for Redis servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS,
Gitlab::Instrumentation::Redis::QUERY_TIME_BUCKETS)
end end
def call(env) def call(env)
...@@ -13,7 +21,7 @@ module Gitlab ...@@ -13,7 +21,7 @@ module Gitlab
@app.call(env) @app.call(env)
ensure ensure
record_metrics(transaction) if transaction record_metrics(transaction)
end end
private private
...@@ -23,14 +31,8 @@ module Gitlab ...@@ -23,14 +31,8 @@ module Gitlab
query_time = Gitlab::Instrumentation::Redis.query_time query_time = Gitlab::Instrumentation::Redis.query_time
request_count = Gitlab::Instrumentation::Redis.get_request_count request_count = Gitlab::Instrumentation::Redis.get_request_count
Gitlab::Metrics.counter(:http_redis_requests_total, @requests_total_counter.increment(labels, request_count)
'Amount of calls to Redis servers during web requests', @requests_duration_histogram.observe(labels, query_time)
Gitlab::Metrics::Transaction::BASE_LABELS).increment(labels, request_count)
Gitlab::Metrics.histogram(:http_redis_requests_duration_seconds,
'Query time for Redis servers during web requests',
Gitlab::Metrics::Transaction::BASE_LABELS,
Gitlab::Instrumentation::Redis::QUERY_TIME_BUCKETS).observe(labels, query_time)
end end
end end
end end
......
...@@ -9,24 +9,6 @@ describe Gitlab::Metrics::ElasticsearchRackMiddleware do ...@@ -9,24 +9,6 @@ describe Gitlab::Metrics::ElasticsearchRackMiddleware do
let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) } let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
describe '#call' do describe '#call' do
context 'when metrics are disabled' do
before do
allow(Gitlab::Metrics).to receive(:current_transaction).and_return(nil)
end
it 'calls the app' do
expect(middleware.call(env)).to eq('app call result')
end
it 'does not record metrics' do
expect(Gitlab::Metrics).not_to receive(:counter)
expect(Gitlab::Metrics).not_to receive(:histogram)
middleware.call(env)
end
end
context 'when metrics are enabled' do
let(:counter) { instance_double(Prometheus::Client::Counter, increment: nil) } let(:counter) { instance_double(Prometheus::Client::Counter, increment: nil) }
let(:histogram) { instance_double(Prometheus::Client::Histogram, observe: nil) } let(:histogram) { instance_double(Prometheus::Client::Histogram, observe: nil) }
let(:elasticsearch_query_time) { 0.1 } let(:elasticsearch_query_time) { 0.1 }
...@@ -72,5 +54,4 @@ describe Gitlab::Metrics::ElasticsearchRackMiddleware do ...@@ -72,5 +54,4 @@ describe Gitlab::Metrics::ElasticsearchRackMiddleware do
expect { middleware.call(env) }.to raise_error(StandardError) expect { middleware.call(env) }.to raise_error(StandardError)
end end
end end
end
end end
...@@ -13,24 +13,6 @@ describe Gitlab::Metrics::RedisRackMiddleware do ...@@ -13,24 +13,6 @@ describe Gitlab::Metrics::RedisRackMiddleware do
end end
describe '#call' do describe '#call' do
context 'when metrics are disabled' do
before do
allow(Gitlab::Metrics).to receive(:current_transaction).and_return(nil)
end
it 'calls the app' do
expect(middleware.call(env)).to eq('wub wub')
end
it 'does not record metrics' do
expect(Gitlab::Metrics).not_to receive(:counter)
expect(Gitlab::Metrics).not_to receive(:histogram)
middleware.call(env)
end
end
context 'when metrics are enabled' do
let(:counter) { double(Prometheus::Client::Counter, increment: nil) } let(:counter) { double(Prometheus::Client::Counter, increment: nil) }
let(:histogram) { double(Prometheus::Client::Histogram, observe: nil) } let(:histogram) { double(Prometheus::Client::Histogram, observe: nil) }
let(:redis_query_time) { 0.1 } let(:redis_query_time) { 0.1 }
...@@ -76,5 +58,4 @@ describe Gitlab::Metrics::RedisRackMiddleware do ...@@ -76,5 +58,4 @@ describe Gitlab::Metrics::RedisRackMiddleware do
expect { middleware.call(env) }.to raise_error(StandardError) expect { middleware.call(env) }.to raise_error(StandardError)
end end
end end
end
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