Class: Bitfab::ReplayRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/bitfab/replay_registry.rb

Overview

Project-owned registry of production replay roots.

Constant Summary collapse

OPTION_NAMES =
%i[
  limit trace_ids name max_concurrency code_change_description
  code_change_files experiment_group_id dataset_id grader_ids mock
  mock_override adapt_inputs db_branch
].freeze

Instance Method Summary collapse

Constructor Details

#initializeReplayRegistry

Returns a new instance of ReplayRegistry.



27
28
29
# File 'lib/bitfab/replay_registry.rb', line 27

def initialize
  @entries = {}
end

Instance Method Details

#fetch(name) ⇒ Object



68
69
70
# File 'lib/bitfab/replay_registry.rb', line 68

def fetch(name)
  @entries.fetch(name)
end

#namesObject



31
32
33
# File 'lib/bitfab/replay_registry.rb', line 31

def names
  @entries.keys.freeze
end

#register(name, receiver, method_name, client: nil, trace_function_key: nil, options_factory: nil, **options) ⇒ Object

Register the exact traced receiver method production invokes.

A bitfab_span method carries its trace function key automatically. Handler-instrumented or otherwise plain methods must pass trace_function_key: explicitly. Pass executable per-function replay behavior such as mock_override: and adapt_inputs: as keyword options; the installed command forwards them to Bitfab::Client#replay.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bitfab/replay_registry.rb', line 42

def register(name, receiver, method_name, client: nil, trace_function_key: nil, options_factory: nil, **options)
  raise ArgumentError, "Replay registry names cannot be empty." if name.to_s.empty?
  raise ArgumentError, "Replay registry already contains '#{name}'." if @entries.key?(name.to_s)
  if options_factory && !options_factory.respond_to?(:call)
    raise ArgumentError, "Replay registry options_factory must be callable."
  end

  key = trace_function_key || Traceable.trace_function_key_for(receiver, method_name)
  unless key
    raise ArgumentError,
      "Replay registry entry uses a plain method. Set trace_function_key: " \
        "to the key its production handler records."
  end
  validate_options(options)

  @entries[name.to_s] = ReplayRegistration.new(
    client: client || Bitfab.client,
    receiver:,
    method_name: method_name.to_sym,
    trace_function_key: key,
    options: options.freeze,
    options_factory:
  )
  self
end

#validate_options(options) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
# File 'lib/bitfab/replay_registry.rb', line 72

def validate_options(options)
  unknown = options.keys - OPTION_NAMES
  return if unknown.empty?

  raise ArgumentError,
    "Unknown replay registry option(s): #{unknown.sort.join(", ")}. " \
      "Allowed options: #{OPTION_NAMES.sort.join(", ")}."
end