Module: OpenTelemetry::Instrumentation::RubyLLM::Patches::Chat

Defined in:
lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb

Instance Method Summary collapse

Instance Method Details

#completeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 13

def complete(&)
  provider = @model&.provider || "unknown"
  model_id = @model&.id || "unknown"

  attributes = {
    "gen_ai.operation.name" => "chat",
    "gen_ai.provider.name" => provider,
    "gen_ai.request.model" => model_id,
  }

  tracer.in_span("chat #{model_id}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::CLIENT) do |span|
    begin
      result = super
    rescue => e
      span.record_exception(e)
      span.status = OpenTelemetry::Trace::Status.error(e.message)
      span.set_attribute("error.type", e.class.name)
      raise
    end

    if @messages.last
      response = @messages.last
      span.set_attribute("gen_ai.response.model", response.model_id) if response.model_id
      span.set_attribute("gen_ai.usage.input_tokens", response.input_tokens) if response.input_tokens
      span.set_attribute("gen_ai.usage.output_tokens", response.output_tokens) if response.output_tokens
      span.set_attribute("gen_ai.request.temperature", @temperature) if @temperature

      if capture_content?
        system_messages = @messages.select { |m| m.role == :system }
        input_messages = @messages[0..-2].reject { |m| m.role == :system }

        unless system_messages.empty?
          span.set_attribute("gen_ai.system_instructions", format_system_instructions(system_messages))
        end

        span.set_attribute("gen_ai.input.messages", format_messages(input_messages))
        span.set_attribute("gen_ai.output.messages", format_messages([response]))
      end
    end

    @otel_attributes&.each { |key, value| span.set_attribute(key, value.respond_to?(:call) ? value.call : value) }

    result
  end
end

#execute_tool(tool_call) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 59

def execute_tool(tool_call)
  attributes = {
    "gen_ai.tool.name" => tool_call.name,
    "gen_ai.tool.call.id" => tool_call.id,
    "gen_ai.tool.call.arguments" => tool_call.arguments.to_json,
    "gen_ai.tool.type" => "function"
  }

  tracer.in_span("execute_tool #{tool_call.name}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::INTERNAL) do |span|
    result = super
    result_str = result.is_a?(::RubyLLM::Tool::Halt) ? result.content.to_s : result.to_s
    span.set_attribute("gen_ai.tool.call.result", result_str[0..500])
    result
  end
end

#with_otel_attributes(attributes) ⇒ Object



8
9
10
11
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 8

def with_otel_attributes(attributes)
  @otel_attributes = attributes
  self
end