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.

API:

  • private

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.

API:

  • private



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aikido/zen/collector/sink_stats.rb', line 35

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:

  • number of scans where an attack was detected.

API:

  • private



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

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:

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

API:

  • private



25
26
27
# File 'lib/aikido/zen/collector/sink_stats.rb', line 25

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:

  • list of aggregated stats.

API:

  • private



33
34
35
# File 'lib/aikido/zen/collector/sink_stats.rb', line 33

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:

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

API:

  • private



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

def errors
  @errors
end

#kindString

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 category of operation this sink represents.

Returns:

  • category of operation this sink represents.

API:

  • private



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

def kind
  @kind
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:

  • number of total calls to Sink#scan.

API:

  • private



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

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:

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

API:

  • private



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

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.

API:

  • private



49
50
51
52
# File 'lib/aikido/zen/collector/sink_stats.rb', line 49

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.

API:

  • private



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aikido/zen/collector/sink_stats.rb', line 66

def as_json
  {
    "kind" => @kind,
    "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.

API:

  • private



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aikido/zen/collector/sink_stats.rb', line 54

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