Class: Gitlab::Metrics::Subscribers::RackAttack

Inherits:
ActiveSupport::Subscriber
  • Object
show all
Defined in:
lib/gitlab/metrics/subscribers/rack_attack.rb

Overview

  • Adds logging for all Rack Attack blocks and throttling events.

  • Instrument the cache operations of RackAttack to use in structured

logs. Two fields are exposed:

+ rack_attack_redis_count: the number of redis calls triggered by
RackAttack in a request.
+ rack_attack_redis_duration_s: the total duration of all redis calls
triggered by RackAttack in a request.

Constant Summary collapse

INSTRUMENTATION_STORE_KEY =
:rack_attack_instrumentation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.payloadObject


18
19
20
21
22
23
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 18

def self.payload
  Gitlab::SafeRequestStore[INSTRUMENTATION_STORE_KEY] ||= {
    rack_attack_redis_count: 0,
    rack_attack_redis_duration_s: 0.0
  }
end

Instance Method Details

#blocklist(event) ⇒ Object


39
40
41
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 39

def blocklist(event)
  log_into_auth_logger(event, status: 403)
end

#redis(event) ⇒ Object


25
26
27
28
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 25

def redis(event)
  self.class.payload[:rack_attack_redis_count] += 1
  self.class.payload[:rack_attack_redis_duration_s] += event.duration.to_f / 1000
end

#safelist(event) ⇒ Object


30
31
32
33
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 30

def safelist(event)
  req = event.payload[:request]
  Gitlab::Instrumentation::Throttle.safelist = req.env['rack.attack.matched']
end

#throttle(event) ⇒ Object


35
36
37
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 35

def throttle(event)
  log_into_auth_logger(event, status: 429)
end

#track(event) ⇒ Object


43
44
45
# File 'lib/gitlab/metrics/subscribers/rack_attack.rb', line 43

def track(event)
  log_into_auth_logger(event, status: nil)
end