diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /lib/gitlab/metrics/elasticsearch_rack_middleware.rb | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'lib/gitlab/metrics/elasticsearch_rack_middleware.rb')
-rw-r--r-- | lib/gitlab/metrics/elasticsearch_rack_middleware.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/gitlab/metrics/elasticsearch_rack_middleware.rb b/lib/gitlab/metrics/elasticsearch_rack_middleware.rb new file mode 100644 index 00000000000..6830eed68d5 --- /dev/null +++ b/lib/gitlab/metrics/elasticsearch_rack_middleware.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module Gitlab + module Metrics + # Rack middleware for tracking Elasticsearch metrics from Grape and Web requests. + class ElasticsearchRackMiddleware + HISTOGRAM_BUCKETS = [0.1, 0.25, 0.5, 1, 2.5, 5, 10, 60].freeze + + def initialize(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 + + def call(env) + transaction = Gitlab::Metrics.current_transaction + + @app.call(env) + ensure + record_metrics(transaction) + end + + private + + def record_metrics(transaction) + labels = transaction.labels + query_time = ::Gitlab::Instrumentation::ElasticsearchTransport.query_time + request_count = ::Gitlab::Instrumentation::ElasticsearchTransport.get_request_count + + @requests_total_counter.increment(labels, request_count) + @requests_duration_histogram.observe(labels, query_time) + end + end + end +end |