Class: Aikido::Zen::Collector::SinkStats Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/collector/sink_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 data specific to a single Sink.

Defined Under Namespace

Classes: CompressedTiming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ SinkStats

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 SinkStats.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aikido/zen/collector/sink_stats.rb', line 32

def initialize(name, config)
  @name = name
  @config = config

  @scans = 0
  @errors = 0

  @attacks = 0
  @blocked_attacks = 0

  @timings = Set.new
  @compressed_timings = CappedSet.new(@config.max_compressed_stats)
end

Instance Attribute Details

#attacksInteger

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 number of scans where an attack was detected.

Returns:

  • (Integer)

    number of scans where an attack was detected.



18
19
20
# File 'lib/aikido/zen/collector/sink_stats.rb', line 18

def attacks
  @attacks
end

#blocked_attacksInteger

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 number of scans where an attack was detected and blocked by the Zen.

Returns:

  • (Integer)

    number of scans where an attack was detected and blocked by the Zen.



22
23
24
# File 'lib/aikido/zen/collector/sink_stats.rb', line 22

def blocked_attacks
  @blocked_attacks
end

#compressed_timingsArray<CompressedTiming>

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 list of aggregated stats.

Returns:



30
31
32
# File 'lib/aikido/zen/collector/sink_stats.rb', line 30

def compressed_timings
  @compressed_timings
end

#errorsInteger

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 number of scans where our scanners raised an error that was handled.

Returns:

  • (Integer)

    number of scans where our scanners raised an error that was handled.



15
16
17
# File 'lib/aikido/zen/collector/sink_stats.rb', line 15

def errors
  @errors
end

#scansInteger

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 number of total calls to Sink#scan.

Returns:

  • (Integer)

    number of total calls to Sink#scan.



11
12
13
# File 'lib/aikido/zen/collector/sink_stats.rb', line 11

def scans
  @scans
end

#timingsSet<Float>

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 keeps the duration of individual scans. If this grows to match Config#max_performance_samples, the set is cleared and the data is aggregated into #compressed_timings.

Returns:

  • (Set<Float>)

    keeps the duration of individual scans. If this grows to match Config#max_performance_samples, the set is cleared and the data is aggregated into #compressed_timings.



27
28
29
# File 'lib/aikido/zen/collector/sink_stats.rb', line 27

def timings
  @timings
end

Instance Method Details

#add_timing(duration) ⇒ Object

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.



46
47
48
49
# File 'lib/aikido/zen/collector/sink_stats.rb', line 46

def add_timing(duration)
  compress_timings if @timings.size >= @config.max_performance_samples
  @timings << duration
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.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aikido/zen/collector/sink_stats.rb', line 63

def as_json
  {
    total: @scans,
    interceptorThrewError: @errors,
    withoutContext: 0,
    attacksDetected: {
      total: @attacks,
      blocked: @blocked_attacks
    },
    compressedTimings: @compressed_timings.as_json
  }
end

#compress_timings(at: Time.now.utc) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aikido/zen/collector/sink_stats.rb', line 51

def compress_timings(at: Time.now.utc)
  return if @timings.empty?

  list = @timings.sort
  @timings.clear

  mean = list.sum / list.size
  percentiles = percentiles(list, 50, 75, 90, 95, 99)

  @compressed_timings << CompressedTiming.new(mean, percentiles, at)
end