Class: FiberAudit::Runtime::Policy

Inherits:
Data
  • Object
show all
Defined in:
lib/fiber_audit/runtime/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**values) ⇒ Policy

Returns a new instance of Policy.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fiber_audit/runtime/policy.rb', line 16

def initialize(**values)
  unknown = values.keys - Policy::DEFAULTS.keys
  raise RuntimeContractError, "unknown runtime policy field: #{unknown.first}" unless unknown.empty?

  fields = Policy::DEFAULTS.merge(values)
  redaction = normalize_redaction(fields[:redaction])
  sampling_rate = normalize_sampling_rate(fields[:sampling_rate])
  limits = Policy::LIMITS.to_h do |name, range|
    [name, normalize_limit(fields[name], name, range)]
  end
  unless limits[:max_session_bytes] >= limits[:max_record_bytes]
    raise RuntimeContractError, 'max_session_bytes must be >= max_record_bytes'
  end
  raise RuntimeContractError, 'fail_open must be a Boolean' unless [true, false].include?(fields[:fail_open])

  super(
    redaction: redaction,
    sampling_rate: sampling_rate,
    **limits,
    fail_open: fields[:fail_open]
  )
end

Instance Attribute Details

#fail_openObject (readonly)

Returns the value of attribute fail_open

Returns:

  • (Object)

    the current value of fail_open



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def fail_open
  @fail_open
end

#max_events_per_secondObject (readonly)

Returns the value of attribute max_events_per_second

Returns:

  • (Object)

    the current value of max_events_per_second



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def max_events_per_second
  @max_events_per_second
end

#max_events_per_sessionObject (readonly)

Returns the value of attribute max_events_per_session

Returns:

  • (Object)

    the current value of max_events_per_session



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def max_events_per_session
  @max_events_per_session
end

#max_record_bytesObject (readonly)

Returns the value of attribute max_record_bytes

Returns:

  • (Object)

    the current value of max_record_bytes



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def max_record_bytes
  @max_record_bytes
end

#max_session_bytesObject (readonly)

Returns the value of attribute max_session_bytes

Returns:

  • (Object)

    the current value of max_session_bytes



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def max_session_bytes
  @max_session_bytes
end

#redactionObject (readonly)

Returns the value of attribute redaction

Returns:

  • (Object)

    the current value of redaction



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def redaction
  @redaction
end

#sampling_rateObject (readonly)

Returns the value of attribute sampling_rate

Returns:

  • (Object)

    the current value of sampling_rate



7
8
9
# File 'lib/fiber_audit/runtime/policy.rb', line 7

def sampling_rate
  @sampling_rate
end

Instance Method Details

#fail_open?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/fiber_audit/runtime/policy.rb', line 64

def fail_open?
  fail_open
end

#rate_allowed?(emitted_in_window:) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/fiber_audit/runtime/policy.rb', line 46

def rate_allowed?(emitted_in_window:)
  count_below_limit?(emitted_in_window, max_events_per_second, 'emitted_in_window')
end

#record_size_allowed?(bytes:) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/fiber_audit/runtime/policy.rb', line 54

def record_size_allowed?(bytes:)
  size_allowed?(bytes, max_record_bytes, 'bytes')
end

#sample?(draw:) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



39
40
41
42
43
44
# File 'lib/fiber_audit/runtime/policy.rb', line 39

def sample?(draw:)
  value = Validation.finite_number(draw, 'draw').to_f
  raise RuntimeContractError, 'draw must be in 0.0...1.0' unless value >= 0.0 && value < 1.0

  value < sampling_rate
end

#session_bytes_allowed?(written_bytes:, next_record_bytes:) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/fiber_audit/runtime/policy.rb', line 58

def session_bytes_allowed?(written_bytes:, next_record_bytes:)
  written = Validation.integer(written_bytes, 'written_bytes')
  following = Validation.integer(next_record_bytes, 'next_record_bytes')
  written + following <= max_session_bytes
end

#session_event_allowed?(emitted_events:) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fiber_audit/runtime/policy.rb', line 50

def session_event_allowed?(emitted_events:)
  count_below_limit?(emitted_events, max_events_per_session, 'emitted_events')
end

#strict_redaction?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/fiber_audit/runtime/policy.rb', line 68

def strict_redaction?
  redaction == :strict
end