Module: RubyLLM::Support::Instrumentation

Defined in:
lib/ruby_llm/support/instrumentation.rb

Overview

:nodoc:

Constant Summary collapse

WORKFLOW_CONTEXT_KEY =
:ruby_llm_workflow_context

Class Method Summary collapse

Class Method Details

.current_workflowObject



28
29
30
# File 'lib/ruby_llm/support/instrumentation.rb', line 28

def current_workflow
  Thread.current[WORKFLOW_CONTEXT_KEY]
end

.instrument(name, payload = nil, config: nil, **attributes) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_llm/support/instrumentation.rb', line 10

def instrument(name, payload = nil, config: nil, **attributes) # rubocop:disable Metrics/PerceivedComplexity
  payload ||= {}
  payload = payload.merge(attributes) unless attributes.empty?
  workflow_context = current_workflow
  payload = payload.merge(workflow_context) if workflow_context
  instrumenter = (config || RubyLLM.config).instrumenter

  if instrumenter.respond_to?(:instrument)
    if block_given?
      instrumenter.instrument(name, payload) { yield(payload) }
    else
      instrumenter.instrument(name, payload)
    end
  elsif block_given?
    yield(payload)
  end
end

.with_workflow(context) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ruby_llm/support/instrumentation.rb', line 32

def with_workflow(context)
  previous_context = current_workflow
  Thread.current[WORKFLOW_CONTEXT_KEY] = context
  yield
ensure
  Thread.current[WORKFLOW_CONTEXT_KEY] = previous_context
end