diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /lib/gitlab/instrumentation_helper.rb | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'lib/gitlab/instrumentation_helper.rb')
-rw-r--r-- | lib/gitlab/instrumentation_helper.rb | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/lib/gitlab/instrumentation_helper.rb b/lib/gitlab/instrumentation_helper.rb index 6b0f01757b7..61de6b02453 100644 --- a/lib/gitlab/instrumentation_helper.rb +++ b/lib/gitlab/instrumentation_helper.rb @@ -7,14 +7,33 @@ module Gitlab DURATION_PRECISION = 6 # microseconds def keys - @keys ||= [:gitaly_calls, - :gitaly_duration_s, - :rugged_calls, - :rugged_duration_s, - :elasticsearch_calls, - :elasticsearch_duration_s, - *::Gitlab::Instrumentation::Redis.known_payload_keys, - *::Gitlab::Metrics::Subscribers::ActiveRecord::DB_COUNTERS] + @keys ||= [ + :cpu_s, + :gitaly_calls, + :gitaly_duration_s, + :rugged_calls, + :rugged_duration_s, + :elasticsearch_calls, + :elasticsearch_duration_s, + :elasticsearch_timed_out_count, + *::Gitlab::Memory::Instrumentation::KEY_MAPPING.values, + *::Gitlab::Instrumentation::Redis.known_payload_keys, + *::Gitlab::Metrics::Subscribers::ActiveRecord::DB_COUNTERS, + *::Gitlab::Metrics::Subscribers::ExternalHttp::KNOWN_PAYLOAD_KEYS, + *::Gitlab::Metrics::Subscribers::RackAttack::PAYLOAD_KEYS + ] + end + + def init_instrumentation_data(request_ip: nil) + # Set `request_start_time` only if this is request + # This is done, as `request_start_time` imply `request_deadline` + if request_ip + Gitlab::RequestContext.instance.client_ip = request_ip + Gitlab::RequestContext.instance.request_start_time = Gitlab::Metrics::System.real_time + end + + Gitlab::RequestContext.instance.start_thread_cpu_time = Gitlab::Metrics::System.thread_cpu_time + Gitlab::RequestContext.instance.thread_memory_allocations = Gitlab::Memory::Instrumentation.start_thread_memory_allocations end def add_instrumentation_data(payload) @@ -24,6 +43,10 @@ module Gitlab instrument_elasticsearch(payload) instrument_throttle(payload) instrument_active_record(payload) + instrument_external_http(payload) + instrument_rack_attack(payload) + instrument_cpu(payload) + instrument_thread_memory_allocations(payload) end def instrument_gitaly(payload) @@ -57,6 +80,15 @@ module Gitlab payload[:elasticsearch_calls] = elasticsearch_calls payload[:elasticsearch_duration_s] = Gitlab::Instrumentation::ElasticsearchTransport.query_time + payload[:elasticsearch_timed_out_count] = Gitlab::Instrumentation::ElasticsearchTransport.get_timed_out_count + end + + def instrument_external_http(payload) + external_http_count = Gitlab::Metrics::Subscribers::ExternalHttp.request_count + + return if external_http_count == 0 + + payload.merge! Gitlab::Metrics::Subscribers::ExternalHttp.payload end def instrument_throttle(payload) @@ -70,6 +102,26 @@ module Gitlab payload.merge!(db_counters) end + def instrument_rack_attack(payload) + rack_attack_redis_count = ::Gitlab::Metrics::Subscribers::RackAttack.payload[:rack_attack_redis_count] + return if rack_attack_redis_count == 0 + + payload.merge!(::Gitlab::Metrics::Subscribers::RackAttack.payload) + end + + def instrument_cpu(payload) + cpu_s = ::Gitlab::Metrics::System.thread_cpu_duration( + ::Gitlab::RequestContext.instance.start_thread_cpu_time) + + payload[:cpu_s] = cpu_s.round(DURATION_PRECISION) if cpu_s + end + + def instrument_thread_memory_allocations(payload) + counters = ::Gitlab::Memory::Instrumentation.measure_thread_memory_allocations( + ::Gitlab::RequestContext.instance.thread_memory_allocations) + payload.merge!(counters) if counters + end + # Returns the queuing duration for a Sidekiq job in seconds, as a float, if the # `enqueued_at` field or `created_at` field is available. # @@ -96,7 +148,7 @@ module Gitlab # # @param [Time] start def self.elapsed_by_absolute_time(start) - (Time.now - start).to_f.round(6) + (Time.now - start).to_f.round(DURATION_PRECISION) end private_class_method :elapsed_by_absolute_time |