Skip to content

Commit c7b4f09

Browse files
committed
Adjust error handling to scope rescue to API calls only
The inner rescue now wraps only the super call, so instrumentation bugs don't get misrecorded as API errors on the span.
1 parent bfd5de8 commit c7b4f09

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

  • lib/opentelemetry/instrumentation/ruby_llm/patches

lib/opentelemetry/instrumentation/ruby_llm/patches/chat.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ def ask(message, &block)
1818
tracer.in_span("chat #{model_id}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::CLIENT) do |span|
1919
begin
2020
result = super
21-
22-
if @messages.last
23-
response = @messages.last
24-
span.set_attribute("gen_ai.response.model", response.model_id) if response.model_id
25-
span.set_attribute("gen_ai.usage.input_tokens", response.input_tokens) if response.input_tokens
26-
span.set_attribute("gen_ai.usage.output_tokens", response.output_tokens) if response.output_tokens
27-
span.set_attribute("gen_ai.request.temperature", @temperature) if @temperature
28-
end
29-
30-
result
3121
rescue => e
3222
span.record_exception(e)
3323
span.status = OpenTelemetry::Trace::Status.error(e.message)
3424
span.set_attribute("error.type", e.class.name)
3525
raise
3626
end
27+
28+
if @messages.last
29+
response = @messages.last
30+
span.set_attribute("gen_ai.response.model", response.model_id) if response.model_id
31+
span.set_attribute("gen_ai.usage.input_tokens", response.input_tokens) if response.input_tokens
32+
span.set_attribute("gen_ai.usage.output_tokens", response.output_tokens) if response.output_tokens
33+
span.set_attribute("gen_ai.request.temperature", @temperature) if @temperature
34+
end
35+
36+
result
3737
end
3838
rescue StandardError => e
3939
OpenTelemetry.handle_error(exception: e)

0 commit comments

Comments
 (0)