2020-05-27 06:08:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'elasticsearch-transport'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Instrumentation
|
|
|
|
module ElasticsearchTransportInterceptor
|
2020-06-29 18:08:57 +00:00
|
|
|
def perform_request(method, path, params = {}, body = nil, headers = nil)
|
2020-05-27 06:08:13 +00:00
|
|
|
start = Time.now
|
2020-06-29 18:08:57 +00:00
|
|
|
headers = (headers || {})
|
|
|
|
.reverse_merge({ 'X-Opaque-Id': Labkit::Correlation::CorrelationId.current_or_new_id })
|
2021-02-16 21:09:23 +00:00
|
|
|
response = super
|
2020-05-27 06:08:13 +00:00
|
|
|
ensure
|
|
|
|
if ::Gitlab::SafeRequestStore.active?
|
|
|
|
duration = (Time.now - start)
|
|
|
|
|
|
|
|
::Gitlab::Instrumentation::ElasticsearchTransport.increment_request_count
|
2021-02-16 21:09:23 +00:00
|
|
|
|
|
|
|
if response&.body && response.body.is_a?(Hash) && response.body['timed_out']
|
|
|
|
::Gitlab::Instrumentation::ElasticsearchTransport.increment_timed_out_count
|
|
|
|
end
|
|
|
|
|
2020-05-27 06:08:13 +00:00
|
|
|
::Gitlab::Instrumentation::ElasticsearchTransport.add_duration(duration)
|
2020-06-29 18:08:57 +00:00
|
|
|
::Gitlab::Instrumentation::ElasticsearchTransport.add_call_details(duration, method, path, params, body)
|
2020-05-27 06:08:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ElasticsearchTransport
|
|
|
|
ELASTICSEARCH_REQUEST_COUNT = :elasticsearch_request_count
|
|
|
|
ELASTICSEARCH_CALL_DURATION = :elasticsearch_call_duration
|
|
|
|
ELASTICSEARCH_CALL_DETAILS = :elasticsearch_call_details
|
2021-02-16 21:09:23 +00:00
|
|
|
ELASTICSEARCH_TIMED_OUT_COUNT = :elasticsearch_timed_out_count
|
2020-05-27 06:08:13 +00:00
|
|
|
|
|
|
|
def self.get_request_count
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_REQUEST_COUNT] || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.increment_request_count
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_REQUEST_COUNT] ||= 0
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_REQUEST_COUNT] += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.detail_store
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_CALL_DETAILS] ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.query_time
|
|
|
|
query_time = ::Gitlab::SafeRequestStore[ELASTICSEARCH_CALL_DURATION] || 0
|
|
|
|
query_time.round(::Gitlab::InstrumentationHelper::DURATION_PRECISION)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_duration(duration)
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_CALL_DURATION] ||= 0
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_CALL_DURATION] += duration
|
|
|
|
end
|
|
|
|
|
2021-02-16 21:09:23 +00:00
|
|
|
def self.increment_timed_out_count
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_TIMED_OUT_COUNT] ||= 0
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_TIMED_OUT_COUNT] += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_timed_out_count
|
|
|
|
::Gitlab::SafeRequestStore[ELASTICSEARCH_TIMED_OUT_COUNT] || 0
|
|
|
|
end
|
|
|
|
|
2020-06-29 18:08:57 +00:00
|
|
|
def self.add_call_details(duration, method, path, params, body)
|
2020-05-27 06:08:13 +00:00
|
|
|
return unless Gitlab::PerformanceBar.enabled_for_request?
|
|
|
|
|
|
|
|
detail_store << {
|
2020-06-29 18:08:57 +00:00
|
|
|
method: method,
|
|
|
|
path: path,
|
|
|
|
params: params,
|
|
|
|
body: body,
|
2020-05-27 06:08:13 +00:00
|
|
|
duration: duration,
|
|
|
|
backtrace: ::Gitlab::BacktraceCleaner.clean_backtrace(caller)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::Elasticsearch::Transport::Client
|
|
|
|
prepend ::Gitlab::Instrumentation::ElasticsearchTransportInterceptor
|
|
|
|
end
|