Remove outer rescue blocks that caused duplicate RubyLLM calls#20
Closed
ebeigarts wants to merge 2 commits into
Closed
Remove outer rescue blocks that caused duplicate RubyLLM calls#20ebeigarts wants to merge 2 commits into
ebeigarts wants to merge 2 commits into
Conversation
Instrument the embeddings endpoint following the OpenTelemetry GenAI semantic conventions for embedding spans. The patch is applied via singleton_class.prepend on RubyLLM::Embedding, wrapping the class-level embed method with a CLIENT span. Span attributes: - gen_ai.operation.name: "embeddings" - gen_ai.provider.name: resolved provider (e.g. "openai") - gen_ai.request.model / gen_ai.response.model: model identifiers - gen_ai.usage.input_tokens: token count from the response - gen_ai.embeddings.dimension.count: length of the embedding vector - error.type: exception class on API failures Error handling follows the same pattern as Chat instrumentation: API errors are recorded on the span and re-raised, while instrumentation failures are swallowed to avoid breaking the underlying embed call. Tests cover span creation with all attributes, error recording on API failure, and resilience when instrumentation itself fails. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The outer `rescue StandardError` blocks in `complete`, `execute_tool`, and `embed` were catching errors from instrumentation, logging them via `OpenTelemetry.handle_error`, and then calling `super` — which re-executed the original RubyLLM method, resulting in duplicate API calls to the LLM provider. This is safe to remove because `Tracer#in_span` already handles exceptions internally by recording them and re-raising: https://github.com/open-telemetry/opentelemetry-ruby/blob/0b94ef6086facf3c7ad584485bb3825b0ab90e39/api/lib/opentelemetry/trace/tracer.rb#L34-L44 This pattern is also not used in other opentelemetry-ruby-contrib instrumentation libraries: https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-ruby-contrib+handle_error&type=code The inner `begin/rescue` blocks inside each span are preserved as they add the `error.type` attribute per GenAI semantic conventions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Author
Contributor
|
I cherry picked this into main, so I'm closing. Thank you for catching this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The outer
rescue StandardErrorblocks incomplete,execute_tool,and
embedwere catching errors from instrumentation, logging themvia
OpenTelemetry.handle_error, and then callingsuper— whichre-executed the original RubyLLM method, resulting in duplicate API
calls to the LLM provider.
This is safe to remove because
Tracer#in_spanalready handlesexceptions internally by recording them and re-raising:
https://github.com/open-telemetry/opentelemetry-ruby/blob/0b94ef6086facf3c7ad584485bb3825b0ab90e39/api/lib/opentelemetry/trace/tracer.rb#L34-L44
This pattern is also not used in other opentelemetry-ruby-contrib
instrumentation libraries:
https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-ruby-contrib+handle_error&type=code
The inner
begin/rescueblocks inside each span are preserved as theyadd the
error.typeattribute per GenAI semantic conventions.