Module: LlmCostTracker::Integrations::Registry

Defined in:
lib/llm_cost_tracker/integrations/registry.rb

Constant Summary collapse

DEFAULT_INTEGRATIONS =
{
  openai: Openai,
  anthropic: Anthropic,
  ruby_llm: RubyLlm
}.freeze
MUTEX =
Monitor.new

Class Method Summary collapse

Class Method Details

.checks(names = LlmCostTracker.configuration.instrumented_integrations) ⇒ Object



33
34
35
36
37
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 33

def checks(names = LlmCostTracker.configuration.instrumented_integrations)
  return [Base::Result.new(:integrations, :ok, "no SDK integrations enabled")] if names.empty?

  normalize(names).map { |name| fetch(name).status }
end

.fetch(name) ⇒ Object



43
44
45
46
47
48
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 43

def fetch(name)
  integrations.fetch(name.to_sym) do
    message = "Unknown integration: #{name.inspect}. Use one of: #{names.join(', ')}"
    raise LlmCostTracker::Error, message
  end
end

.install!(names = LlmCostTracker.configuration.instrumented_integrations) ⇒ Object



29
30
31
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 29

def install!(names = LlmCostTracker.configuration.instrumented_integrations)
  normalize(names).each { |name| fetch(name).install }
end

.integrationsObject



58
59
60
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 58

def integrations
  @integrations || MUTEX.synchronize { @integrations ||= DEFAULT_INTEGRATIONS.dup.freeze }
end

.namesObject



50
51
52
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 50

def names
  integrations.keys
end

.normalize(names) ⇒ Object



39
40
41
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 39

def normalize(names)
  Array(names).flatten.map(&:to_sym).uniq
end

.register(name, integration) ⇒ Object



22
23
24
25
26
27
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 22

def register(name, integration)
  key = name.to_sym
  validate_integration!(integration)
  MUTEX.synchronize { @integrations = integrations.merge(key => integration).freeze }
  integration
end

.reset!Object



54
55
56
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 54

def reset!
  MUTEX.synchronize { @integrations = DEFAULT_INTEGRATIONS.dup.freeze }
end

.validate_integration!(integration) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/llm_cost_tracker/integrations/registry.rb', line 62

def validate_integration!(integration)
  return if integration.respond_to?(:install) && integration.respond_to?(:status)

  raise ArgumentError, "integration must respond to install and status"
end