Class: Chronos::Application::RemoteConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/application/remote_configuration.rb

Overview

Runtime policy restricted to server-authorized scalar options.

Examples:

remote.apply("sampling_rate" => 0.5, "kill_switch" => false)

Constant Summary collapse

SUPPORTED_EVENT_TYPES =
["exception"].freeze
MAX_IGNORED_FINGERPRINTS =
100
MAX_FINGERPRINT_BYTES =
256
MIN_PAYLOAD_SIZE =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ RemoteConfiguration

Returns a new instance of RemoteConfiguration.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chronos/application/remote_configuration.rb', line 23

def initialize(config, options = {})
  @config = config
  @random = options[:random] || proc { rand }
  @mutex = Mutex.new
  @local_event_types = allowed_event_types(config.enabled_event_types)
  @values = {
    "sampling_rate" => config.sampling_rate.to_f,
    "enabled_event_types" => @local_event_types,
    "max_payload_size" => config.max_payload_size,
    "ignored_fingerprints" => [],
    "send_interval" => 0.0,
    "kill_switch" => false
  }
  @ignored_fingerprints = [].freeze
end

Instance Attribute Details

#ignored_fingerprintsObject (readonly)

Returns the value of attribute ignored_fingerprints.



21
22
23
# File 'lib/chronos/application/remote_configuration.rb', line 21

def ignored_fingerprints
  @ignored_fingerprints
end

Instance Method Details

#apply(document) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chronos/application/remote_configuration.rb', line 39

def apply(document)
  normalized = normalize(document)
  return false unless normalized

  @mutex.synchronize do
    @values = @values.merge(normalized)
    @ignored_fingerprints = @values["ignored_fingerprints"].dup.freeze
  end
  true
rescue StandardError
  false
end

#capture?(event_type, fingerprint = nil) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/chronos/application/remote_configuration.rb', line 52

def capture?(event_type, fingerprint = nil)
  values = snapshot
  return false if values["kill_switch"]
  return false unless values["enabled_event_types"].include?(event_type.to_s)
  return false if fingerprint && values["ignored_fingerprints"].include?(fingerprint.to_s)

  rate = values["sampling_rate"]
  rate >= 1.0 || (rate > 0.0 && @random.call.to_f < rate)
end

#enabled_event?(event_type) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/chronos/application/remote_configuration.rb', line 62

def enabled_event?(event_type)
  snapshot["enabled_event_types"].include?(event_type.to_s)
end

#ignored_fingerprint?(fingerprint) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/chronos/application/remote_configuration.rb', line 66

def ignored_fingerprint?(fingerprint)
  snapshot["ignored_fingerprints"].include?(fingerprint.to_s)
end

#kill_switch?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/chronos/application/remote_configuration.rb', line 82

def kill_switch?
  snapshot["kill_switch"]
end

#max_payload_sizeObject



74
75
76
# File 'lib/chronos/application/remote_configuration.rb', line 74

def max_payload_size
  snapshot["max_payload_size"]
end

#sampling_rateObject



70
71
72
# File 'lib/chronos/application/remote_configuration.rb', line 70

def sampling_rate
  snapshot["sampling_rate"]
end

#send_intervalObject



78
79
80
# File 'lib/chronos/application/remote_configuration.rb', line 78

def send_interval
  snapshot["send_interval"]
end

#to_hObject



86
87
88
# File 'lib/chronos/application/remote_configuration.rb', line 86

def to_h
  snapshot
end