Class: Aikido::Zen::Collector::Stats Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/collector/stats.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Tracks information about how the Aikido Agent is used in the app.

Instance Method Summary collapse

Constructor Details

#initialize(config = Aikido::Zen.config) ⇒ Stats

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Stats.



16
17
18
19
20
21
22
23
# File 'lib/aikido/zen/collector/stats.rb', line 16

def initialize(config = Aikido::Zen.config)
  super()
  @config = config
  @sinks = Hash.new { |h, k| h[k] = Collector::SinkStats.new(k, @config) }
  @started_at = @ended_at = nil
  @requests = 0
  @aborted_requests = 0
end

Instance Method Details

#add_attack(attack, being_blocked:) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • attack (Aikido::Zen::Attack)
  • being_blocked (Boolean)

    whether the Agent blocked the request where this Attack happened or not.

Returns:

  • (self)


79
80
81
82
83
84
# File 'lib/aikido/zen/collector/stats.rb', line 79

def add_attack(attack, being_blocked:)
  stats = @sinks[attack.sink.name]
  stats.attacks += 1
  stats.blocked_attacks += 1 if being_blocked
  self
end

#add_requestself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (self)


60
61
62
63
# File 'lib/aikido/zen/collector/stats.rb', line 60

def add_request
  @requests += 1
  self
end

#add_scan(scan) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (self)


67
68
69
70
71
72
73
# File 'lib/aikido/zen/collector/stats.rb', line 67

def add_scan(scan)
  stats = @sinks[scan.sink.name]
  stats.scans += 1
  stats.errors += 1 if scan.errors?
  stats.add_timing(scan.duration)
  self
end

#any?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


31
32
33
# File 'lib/aikido/zen/collector/stats.rb', line 31

def any?
  !empty?
end

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/aikido/zen/collector/stats.rb', line 86

def as_json
  total_attacks, total_blocked = aggregate_attacks_from_sinks
  {
    startedAt: @started_at.to_i * 1000,
    endedAt: (@ended_at.to_i * 1000 if @ended_at),
    sinks: @sinks.transform_values(&:as_json),
    requests: {
      total: @requests,
      aborted: @aborted_requests,
      attacksDetected: {
        total: total_attacks,
        blocked: total_blocked
      }
    }
  }
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


26
27
28
# File 'lib/aikido/zen/collector/stats.rb', line 26

def empty?
  @requests.zero? && @sinks.empty?
end

#flush(at: Time.now.utc) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the end time for these stats block, freezes it to avoid any more writing to them, and compresses the timing stats in anticipation of sending these to the Aikido servers.

Parameters:

  • at (Time) (defaults to: Time.now.utc)

    the time at which we’re resetting, which is set as the ending time for the returned copy.

Returns:

  • (self)


51
52
53
54
55
56
57
# File 'lib/aikido/zen/collector/stats.rb', line 51

def flush(at: Time.now.utc)
  # Make sure the timing stats are compressed before copying, since we
  # need these compressed when we serialize this for the API.
  @sinks.each_value { |sink| sink.compress_timings(at: at) }
  @ended_at = at
  freeze
end

#start(at = Time.now.utc) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Track the timestamp we start tracking this series of stats.

Parameters:

  • at (Time) (defaults to: Time.now.utc)

Returns:

  • (self)


39
40
41
42
# File 'lib/aikido/zen/collector/stats.rb', line 39

def start(at = Time.now.utc)
  @started_at = at
  self
end