Repository navigation
feat(agents): Instrument RubyLLM tools #3184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sl0thentr0py
wants to merge
1
commit into
neel/ruby-llm/chat-instrumentation
from
neel/ruby-llm/tool-instrumentation
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
74 changes: 74 additions & 0 deletions
74
sentry-agents/lib/sentry/agents/ruby_llm/tool_concurrency.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "ruby_llm/chat/tool_concurrency" | ||
|
|
||
| module Sentry | ||
| module Agents | ||
| module RubyLLM | ||
| class HubContext | ||
| def self.capture | ||
| return unless Sentry.initialized? | ||
|
|
||
| hub = Sentry.get_current_hub | ||
| new(hub, hub.current_scope&.get_span) | ||
| rescue StandardError => error | ||
| report(error) | ||
| nil | ||
| end | ||
|
|
||
| def initialize(hub, span) | ||
| @hub = hub | ||
| @span = span | ||
| end | ||
|
|
||
| def with_hub | ||
| previous_hub = Sentry.send(:get_current_hub_internal) | ||
| begin | ||
| hub = @hub.clone | ||
| hub.current_scope.set_span(@span) if @span | ||
| Sentry.send(:set_current_hub_internal, hub) | ||
| rescue StandardError => error | ||
| self.class.report(error) | ||
| return yield | ||
| end | ||
|
|
||
| begin | ||
| yield | ||
| ensure | ||
| Sentry.send(:set_current_hub_internal, previous_hub) | ||
| end | ||
| end | ||
|
|
||
| class << self | ||
| def report(error) | ||
| Sentry.configuration.sdk_logger.debug("sentry-agents instrumentation failed: #{error.class}: #{error.message}") | ||
| rescue StandardError | ||
| nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| module ToolConcurrency | ||
| module Patch | ||
| def run(mode, tool_calls, on_result: nil, &execute) | ||
| context = HubContext.capture | ||
| return super unless context | ||
|
|
||
| super(mode, tool_calls, on_result: on_result) do |tool_call| | ||
| context.with_hub { execute.call(tool_call) } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| module_function | ||
|
|
||
| def install | ||
| target = ::RubyLLM::Chat::ToolConcurrency | ||
| return if target.singleton_class.ancestors.include?(Patch) | ||
|
|
||
| target.singleton_class.prepend(Patch) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
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
48 changes: 48 additions & 0 deletions
48
sentry-agents/spec/sentry/agents/ruby_llm/tool_concurrency_spec.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Sentry::Agents::RubyLLM::ToolConcurrency do | ||
| before do | ||
| RubyLLM.config.instrumenter = nil | ||
| Sentry.init do |config| | ||
| config.dsn = Sentry::TestHelper::DUMMY_DSN | ||
| config.sdk_logger = Logger.new(nil) | ||
| config.background_worker_threads = 0 | ||
| config.transport.transport_class = Sentry::DummyTransport | ||
| config.traces_sample_rate = 1.0 | ||
| config.data_collection.gen_ai.inputs = true | ||
| config.data_collection.gen_ai.outputs = true | ||
| end | ||
| end | ||
|
|
||
| after do | ||
| RubyLLM.config.instrumenter = nil | ||
| end | ||
|
|
||
| it "propagates an isolated hub while retaining the parent span recorder" do | ||
| parent = Sentry.start_transaction(name: "parent", op: "test") | ||
| Sentry.get_current_scope.set_span(parent) | ||
| adapter = Sentry::Agents::RubyLLM::Instrumenter.new | ||
| seen_spans = Queue.new | ||
|
|
||
| results = RubyLLM::Chat::ToolConcurrency.run(:threads, { first: "first", second: "second" }) do |tool_call| | ||
| payload = { tool_name: tool_call, tool_arguments: {} } | ||
| RubyLLM::Support::Instrumentation.instrument("tool_call.ruby_llm", payload) do |event| | ||
| seen_spans << Sentry.get_current_scope.get_span | ||
| event[:result] = tool_call | ||
| tool_call | ||
| end | ||
| end | ||
|
|
||
| expect(results.map(&:last)).to contain_exactly("first", "second") | ||
| expect(seen_spans.size).to eq(2) | ||
| expect(seen_spans.size.times.map { seen_spans.pop.parent_span_id }).to all(eq(parent.span_id)) | ||
| expect(Sentry.get_current_scope.get_span).to eq(parent) | ||
|
|
||
| parent.finish | ||
| event = sentry_events.find { |item| item.is_a?(Sentry::TransactionEvent) } | ||
| expect(event.spans.size).to eq(2) | ||
| expect(event.spans.map { |span| span[:op] }).to all(eq("gen_ai.execute_tool")) | ||
| end | ||
| end |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fiber tool runs corrupt hub state
Medium Severity
with_hubsave/restores the current hub via thread-local storage, andrunapplies that wrap in every concurrency mode. RubyLLM:fibersruns tools as cooperative fibers on the same thread, so overlappingwith_hubcalls overwrite each other's saved hub. After the batch finishes, the request thread can be left with a stale cloned hub instead of the original, so later spans and events lose the request scope.Additional Locations (1)
sentry-agents/lib/sentry/agents/ruby_llm/tool_concurrency.rb#L52-L59Reviewed by Cursor Bugbot for commit 01f1aad. Configure here.