Class: Gruf::Instrumentation::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/instrumentation/registry.rb

Overview

Registry of all hooks added

Defined Under Namespace

Classes: StrategyDescendantError

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Gruf::Instrumentation::Base|NilClass

Return a strategy type registry via a hash accessor syntax

Returns:



59
60
61
# File 'lib/gruf/instrumentation/registry.rb', line 59

def [](name)
  _registry[name.to_sym]
end

.add(name, strategy = nil, &block) ⇒ Class

Add an authentication strategy, either through a class or a block

Gruf::Instrumentation::Registry.add(:my_instrumentor, MyInstrumentor)

a block that can be built as a strategy instead

Parameters:

  • name (String)

    The name to represent the strategy as

  • strategy (Gruf::Hooks::Base|NilClass) (defaults to: nil)

    (Optional) The strategy class to add. If nil, will expect

Returns:

  • (Class)

    The strategy that was added

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/gruf/instrumentation/registry.rb', line 44

def add(name, strategy = nil, &block)
  base = Gruf::Instrumentation::Base
  strategy ||= Class.new(base)
  strategy.class_eval(&block) if block_given?

  raise StrategyDescendantError, "Hooks must descend from #{base}" unless strategy.ancestors.include?(base)

  _registry[name.to_sym] = strategy
end

.any?Boolean

Return true if there are any registered instrumentation strategies

Returns:

  • (Boolean)

    Return true if there are any registered instrumentation strategies



82
83
84
# File 'lib/gruf/instrumentation/registry.rb', line 82

def any?
  to_h.keys.count > 0
end

.clearHash

Returns Clear all existing strategies from the registry.

Returns:

  • (Hash)

    Clear all existing strategies from the registry



89
90
91
# File 'lib/gruf/instrumentation/registry.rb', line 89

def clear
  @_registry = {}
end

.eachObject

Iterate over each hook in the registry



66
67
68
69
70
# File 'lib/gruf/instrumentation/registry.rb', line 66

def each
  _registry.each do |name, s|
    yield name, s
  end
end

.to_hHash<Class>

Return the registry represented as a Hash object

Returns:

  • (Hash<Class>)

    Return the registry represented as a Hash object



75
76
77
# File 'lib/gruf/instrumentation/registry.rb', line 75

def to_h
  _registry
end