diff options
Diffstat (limited to 'lib/gitlab/metrics')
-rw-r--r-- | lib/gitlab/metrics/subscribers/active_record.rb | 22 | ||||
-rw-r--r-- | lib/gitlab/metrics/subscribers/external_http.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/metrics/subscribers/ldap.rb | 12 | ||||
-rw-r--r-- | lib/gitlab/metrics/subscribers/load_balancing.rb | 14 | ||||
-rw-r--r-- | lib/gitlab/metrics/subscribers/rack_attack.rb | 4 |
5 files changed, 41 insertions, 31 deletions
diff --git a/lib/gitlab/metrics/subscribers/active_record.rb b/lib/gitlab/metrics/subscribers/active_record.rb index 10bb358a292..dd99d4d770c 100644 --- a/lib/gitlab/metrics/subscribers/active_record.rb +++ b/lib/gitlab/metrics/subscribers/active_record.rb @@ -21,6 +21,8 @@ module Gitlab SQL_WAL_LOCATION_REGEX = /(pg_current_wal_insert_lsn\(\)::text|pg_last_wal_replay_lsn\(\)::text)/.freeze + InstrumentationStorage = ::Gitlab::Instrumentation::Storage + # This event is published from ActiveRecordBaseTransactionMetrics and # used to record a database transaction duration when calling # ApplicationRecord.transaction {} block. @@ -56,20 +58,20 @@ module Gitlab end def self.db_counter_payload - return {} unless Gitlab::SafeRequestStore.active? + return {} unless InstrumentationStorage.active? {}.tap do |payload| db_counter_keys.each do |key| - payload[key] = Gitlab::SafeRequestStore[key].to_i + payload[key] = InstrumentationStorage[key].to_i end - if ::Gitlab::SafeRequestStore.active? + if InstrumentationStorage.active? load_balancing_metric_counter_keys.each do |counter| - payload[counter] = ::Gitlab::SafeRequestStore[counter].to_i + payload[counter] = InstrumentationStorage[counter].to_i end load_balancing_metric_duration_keys.each do |duration| - payload[duration] = ::Gitlab::SafeRequestStore[duration].to_f.round(3) + payload[duration] = InstrumentationStorage[duration].to_f.round(3) end end end @@ -100,16 +102,16 @@ module Gitlab buckets ::Gitlab::Metrics::Subscribers::ActiveRecord::SQL_DURATION_BUCKET end - return unless ::Gitlab::SafeRequestStore.active? + return unless InstrumentationStorage.active? duration = event.duration / 1000.0 duration_key = compose_metric_key(:duration_s, db_role) - ::Gitlab::SafeRequestStore[duration_key] = (::Gitlab::SafeRequestStore[duration_key].presence || 0) + duration + InstrumentationStorage[duration_key] = (InstrumentationStorage[duration_key].presence || 0) + duration # Per database metrics db_config_name = db_config_name(event.payload) duration_key = compose_metric_key(:duration_s, nil, db_config_name) - ::Gitlab::SafeRequestStore[duration_key] = (::Gitlab::SafeRequestStore[duration_key].presence || 0) + duration + InstrumentationStorage[duration_key] = (InstrumentationStorage[duration_key].presence || 0) + duration end def ignored_query?(payload) @@ -135,14 +137,14 @@ module Gitlab current_transaction&.increment(prometheus_key, 1, { db_config_name: db_config_name }) - Gitlab::SafeRequestStore[log_key] = Gitlab::SafeRequestStore[log_key].to_i + 1 + InstrumentationStorage[log_key] = InstrumentationStorage[log_key].to_i + 1 # To avoid confusing log keys we only log the db_config_name metrics # when we are also logging the db_role. Otherwise it will be hard to # tell if the log key is referring to a db_role OR a db_config_name. if db_role.present? && db_config_name.present? log_key = compose_metric_key(counter, nil, db_config_name) - Gitlab::SafeRequestStore[log_key] = Gitlab::SafeRequestStore[log_key].to_i + 1 + InstrumentationStorage[log_key] = InstrumentationStorage[log_key].to_i + 1 end end diff --git a/lib/gitlab/metrics/subscribers/external_http.rb b/lib/gitlab/metrics/subscribers/external_http.rb index 87756b14887..a5bfc80b3bf 100644 --- a/lib/gitlab/metrics/subscribers/external_http.rb +++ b/lib/gitlab/metrics/subscribers/external_http.rb @@ -6,6 +6,8 @@ module Gitlab # Class for tracking the total time spent in external HTTP # See more at https://gitlab.com/gitlab-org/labkit-ruby/-/blob/v0.14.0/lib/gitlab-labkit.rb#L18 class ExternalHttp < ActiveSupport::Subscriber + InstrumentationStorage = ::Gitlab::Instrumentation::Storage + attach_to :external_http DEFAULT_STATUS_CODE = 'undefined' @@ -19,19 +21,19 @@ module Gitlab MAX_SLOW_REQUESTS = 10 def self.detail_store - ::Gitlab::SafeRequestStore[DETAIL_STORE] ||= [] + InstrumentationStorage[DETAIL_STORE] ||= [] end def self.duration - Gitlab::SafeRequestStore[DURATION].to_f + InstrumentationStorage[DURATION].to_f end def self.request_count - Gitlab::SafeRequestStore[COUNTER].to_i + InstrumentationStorage[COUNTER].to_i end def self.slow_requests - Gitlab::SafeRequestStore[SLOW_REQUESTS] + InstrumentationStorage[SLOW_REQUESTS] end def self.top_slowest_requests @@ -82,14 +84,14 @@ module Gitlab end def add_to_request_store(payload) - return unless Gitlab::SafeRequestStore.active? + return unless InstrumentationStorage.active? - Gitlab::SafeRequestStore[COUNTER] = Gitlab::SafeRequestStore[COUNTER].to_i + 1 - Gitlab::SafeRequestStore[DURATION] = Gitlab::SafeRequestStore[DURATION].to_f + payload[:duration].to_f + InstrumentationStorage[COUNTER] = InstrumentationStorage[COUNTER].to_i + 1 + InstrumentationStorage[DURATION] = InstrumentationStorage[DURATION].to_f + payload[:duration].to_f if payload[:duration].to_f > THRESHOLD_SLOW_REQUEST_S - Gitlab::SafeRequestStore[SLOW_REQUESTS] ||= [] - Gitlab::SafeRequestStore[SLOW_REQUESTS] << { + InstrumentationStorage[SLOW_REQUESTS] ||= [] + InstrumentationStorage[SLOW_REQUESTS] << { method: payload[:method], host: payload[:host], port: payload[:port], diff --git a/lib/gitlab/metrics/subscribers/ldap.rb b/lib/gitlab/metrics/subscribers/ldap.rb index 3dae2d1fd88..409ecbb6e8a 100644 --- a/lib/gitlab/metrics/subscribers/ldap.rb +++ b/lib/gitlab/metrics/subscribers/ldap.rb @@ -8,6 +8,8 @@ module Gitlab # at the end of the event key, e.g. `open.net_ldap` attach_to :net_ldap + InstrumentationStorage = ::Gitlab::Instrumentation::Storage + COUNTER = :net_ldap_count DURATION = :net_ldap_duration_s @@ -26,12 +28,12 @@ module Gitlab class << self # @return [Integer] the total number of LDAP requests def count - Gitlab::SafeRequestStore[COUNTER].to_i + InstrumentationStorage[COUNTER].to_i end # @return [Float] the total duration spent on LDAP requests def duration - Gitlab::SafeRequestStore[DURATION].to_f + InstrumentationStorage[DURATION].to_f end # Used in Gitlab::InstrumentationHelper to merge the LDAP stats @@ -71,10 +73,10 @@ module Gitlab # Track these events as statistics for the current requests, for logging purposes def add_to_request_store(event) - return unless Gitlab::SafeRequestStore.active? + return unless InstrumentationStorage.active? - Gitlab::SafeRequestStore[COUNTER] = self.class.count + 1 - Gitlab::SafeRequestStore[DURATION] = self.class.duration + convert_to_seconds(event.duration) + InstrumentationStorage[COUNTER] = self.class.count + 1 + InstrumentationStorage[DURATION] = self.class.duration + convert_to_seconds(event.duration) end # Converts the observed events into Prometheus metrics diff --git a/lib/gitlab/metrics/subscribers/load_balancing.rb b/lib/gitlab/metrics/subscribers/load_balancing.rb index bd77e8c3c3f..d7fe33dbe89 100644 --- a/lib/gitlab/metrics/subscribers/load_balancing.rb +++ b/lib/gitlab/metrics/subscribers/load_balancing.rb @@ -6,11 +6,13 @@ module Gitlab class LoadBalancing < ActiveSupport::Subscriber attach_to :load_balancing + InstrumentationStorage = ::Gitlab::Instrumentation::Storage + PROMETHEUS_COUNTER = :gitlab_transaction_caught_up_replica_pick_count_total LOG_COUNTERS = { true => :caught_up_replica_pick_ok, false => :caught_up_replica_pick_fail }.freeze def caught_up_replica_pick(event) - return unless Gitlab::SafeRequestStore.active? + return unless InstrumentationStorage.active? result = event.payload[:result] counter_name = counter(result) @@ -20,17 +22,17 @@ module Gitlab # we want to update Prometheus counter after the controller/action are set def web_transaction_completed(_event) - return unless Gitlab::SafeRequestStore.active? + return unless InstrumentationStorage.active? LOG_COUNTERS.keys.each { |result| increment_prometheus_for_result_label(result) } end def self.load_balancing_payload - return {} unless Gitlab::SafeRequestStore.active? + return {} unless InstrumentationStorage.active? {}.tap do |payload| LOG_COUNTERS.values.each do |counter| - value = Gitlab::SafeRequestStore[counter] + value = InstrumentationStorage[counter] payload[counter] = value.to_i if value end @@ -40,12 +42,12 @@ module Gitlab private def increment(counter) - Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1 + InstrumentationStorage[counter] = InstrumentationStorage[counter].to_i + 1 end def increment_prometheus_for_result_label(label_value) counter_name = counter(label_value) - return unless (counter_value = Gitlab::SafeRequestStore[counter_name]) + return unless (counter_value = InstrumentationStorage[counter_name]) increment_prometheus(labels: { result: label_value }, value: counter_value.to_i) end diff --git a/lib/gitlab/metrics/subscribers/rack_attack.rb b/lib/gitlab/metrics/subscribers/rack_attack.rb index 2196122df01..705536039ed 100644 --- a/lib/gitlab/metrics/subscribers/rack_attack.rb +++ b/lib/gitlab/metrics/subscribers/rack_attack.rb @@ -13,10 +13,12 @@ module Gitlab class RackAttack < ActiveSupport::Subscriber attach_to 'rack_attack' + InstrumentationStorage = ::Gitlab::Instrumentation::Storage + INSTRUMENTATION_STORE_KEY = :rack_attack_instrumentation def self.payload - Gitlab::SafeRequestStore[INSTRUMENTATION_STORE_KEY] ||= { + InstrumentationStorage[INSTRUMENTATION_STORE_KEY] ||= { rack_attack_redis_count: 0, rack_attack_redis_duration_s: 0.0 } |