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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#otel_conversation_idObject



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

def otel_conversation_id
  id = @otel_attributes&.[]("gen_ai.conversation.id") || @otel_conversation_id
  id.respond_to?(:call) ? id.call : id
end

Instance Method Details

#completeObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 20

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,
  }
  conversation_id = otel_conversation_id
  attributes["gen_ai.conversation.id"] = conversation_id if conversation_id
  # Per GenAI semconv: set `gen_ai.request.stream` if and only if
  # the request is streaming. Absence means non-streaming.
  attributes["gen_ai.request.stream"] = true if block_given?

  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

      # Prompt-cache token accessors were added in ruby_llm 1.9.0 (commit 869a755f).
      if response.respond_to?(:cached_tokens) && response.cached_tokens
        span.set_attribute("gen_ai.usage.cache_read.input_tokens", response.cached_tokens)
      end

      # Prompt-cache token accessors were added in ruby_llm 1.9.0 (commit 869a755f).
      if response.respond_to?(:cache_creation_tokens) && response.cache_creation_tokens
        span.set_attribute("gen_ai.usage.cache_creation.input_tokens", response.cache_creation_tokens)
      end

      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", MessageFormatter.format_system_instructions(system_messages))
        end

        span.set_attribute("gen_ai.input.messages", MessageFormatter.format_input_messages(input_messages))
        span.set_attribute("gen_ai.output.messages", MessageFormatter.format_output_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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 81

def execute_tool(tool_call)
  attributes = {
    "gen_ai.operation.name" => "execute_tool",
    "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",
    "gen_ai.tool.description" => tools[tool_call.name.to_sym]&.description
  }.compact

  tracer.in_span("execute_tool #{tool_call.name}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::INTERNAL) 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

    # `RubyLLM::Tool::Halt#to_s` returns `@content.to_s`, so a single
    # `to_s` covers both the Halt and plain-result cases.
    span.set_attribute("gen_ai.tool.call.result", result.to_s[0, tool_result_max_length])

    result
  end
end

#with_otel_attributes(attributes) ⇒ Object



15
16
17
18
# File 'lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb', line 15

def with_otel_attributes(attributes)
  @otel_attributes = attributes
  self
end