Class: FiberAudit::Runtime::Policy
- Inherits:
-
Data
- Object
- Data
- FiberAudit::Runtime::Policy
- Defined in:
- lib/fiber_audit/runtime/policy.rb
Instance Attribute Summary collapse
-
#fail_open ⇒ Object
readonly
Returns the value of attribute fail_open.
-
#max_events_per_second ⇒ Object
readonly
Returns the value of attribute max_events_per_second.
-
#max_events_per_session ⇒ Object
readonly
Returns the value of attribute max_events_per_session.
-
#max_record_bytes ⇒ Object
readonly
Returns the value of attribute max_record_bytes.
-
#max_session_bytes ⇒ Object
readonly
Returns the value of attribute max_session_bytes.
-
#redaction ⇒ Object
readonly
Returns the value of attribute redaction.
-
#sampling_rate ⇒ Object
readonly
Returns the value of attribute sampling_rate.
Instance Method Summary collapse
- #fail_open? ⇒ Boolean
-
#initialize(**values) ⇒ Policy
constructor
A new instance of Policy.
- #rate_allowed?(emitted_in_window:) ⇒ Boolean
- #record_size_allowed?(bytes:) ⇒ Boolean
- #sample?(draw:) ⇒ Boolean
- #session_bytes_allowed?(written_bytes:, next_record_bytes:) ⇒ Boolean
- #session_event_allowed?(emitted_events:) ⇒ Boolean
- #strict_redaction? ⇒ Boolean
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_open ⇒ Object (readonly)
Returns the value of attribute fail_open
7 8 9 |
# File 'lib/fiber_audit/runtime/policy.rb', line 7 def fail_open @fail_open end |
#max_events_per_second ⇒ Object (readonly)
Returns the value of attribute 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_session ⇒ Object (readonly)
Returns the value of attribute 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_bytes ⇒ Object (readonly)
Returns the value of attribute 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_bytes ⇒ Object (readonly)
Returns the value of attribute max_session_bytes
7 8 9 |
# File 'lib/fiber_audit/runtime/policy.rb', line 7 def max_session_bytes @max_session_bytes end |
#redaction ⇒ Object (readonly)
Returns the value of attribute redaction
7 8 9 |
# File 'lib/fiber_audit/runtime/policy.rb', line 7 def redaction @redaction end |
#sampling_rate ⇒ Object (readonly)
Returns the value of attribute 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
64 65 66 |
# File 'lib/fiber_audit/runtime/policy.rb', line 64 def fail_open? fail_open end |
#rate_allowed?(emitted_in_window:) ⇒ 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
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
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
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
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
68 69 70 |
# File 'lib/fiber_audit/runtime/policy.rb', line 68 def strict_redaction? redaction == :strict end |