Skip to content

feat(agents): Instrument RubyLLM chats - #3183

Draft
sl0thentr0py wants to merge 1 commit into
neel/ruby-llm/gen-ai-privacyfrom
neel/ruby-llm/chat-instrumentation
Draft

sl0thentr0py wants to merge 1 commit into
neel/ruby-llm/gen-ai-privacyfrom
neel/ruby-llm/chat-instrumentation

Conversation

@sl0thentr0py

@sl0thentr0py sl0thentr0py commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Instruments RubyLLM 2.2 chat operations through the configured RubyLLM instrumenter.

  • Creates gen_ai.chat root transactions when no Sentry span is active and child spans with Sentry.with_child_span otherwise.
  • Records request and response attributes plus privacy-gated normalized messages, system instructions, and tool definitions.
  • Accounts for per-attempt input, output, cache, and reasoning tokens from usage.ruby_llm events.
  • Uses RubyLLM's public 2.2 message, tool, token, and OpenTelemetry APIs.
  • Supports isolated RubyLLM contexts through explicit installation.

Testing

  • Covers standalone and child chat spans, privacy gates, usage aggregation, response serialization, exceptions, and isolated contexts.
  • Verified with RubyLLM 2.2.3 on the Ruby 3.2 and Ruby 4.0 test cells.

Issues

@sl0thentr0py
sl0thentr0py added this pull request to stack #3182 November 4, 2026 16:10
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from f1a41f0 to 8734db7 Compare November 4, 2026 16:14

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8734db7. Configure here.

@sl0thentr0py
sl0thentr0py marked this pull request as draft November 4, 2026 16:54
@linear-code

linear-code Bot commented Nov 4, 2026

Copy link
Copy Markdown

RUBY-217

@crmne

crmne commented Nov 7, 2026

Copy link
Copy Markdown

Hi Neel, RubyLLM creator here. Thanks for building this, it's great to see Sentry tracing RubyLLM natively.

One request before this lands: please don't monkey-patch RubyLLM's internals. ToolConcurrency.run, Agent.apply_configuration and Support::Instrumentation.current_workflow are private (:nodoc:), and they change between releases. The ToolConcurrency patch already breaks with RubyLLM 2.2, which ships Tuesday: every chat whose model calls a tool raises ArgumentError: unknown keywords: :exclusive, :paused, and ~> 2.0 will install it.

You don't need the patches anymore. I added what you were patching around to RubyLLM's public API, and it's on main for 2.2:

  • Tool spans keep their parent. If the configured instrumenter responds to capture_context and with_context, RubyLLM carries that context into every tool worker, in both :threads and :fibers mode. Implement those two on your wrapper and drop the ToolConcurrency patch. Docs and an example: docs/_reference/instrumentation-events.md.
  • Agent runs are an event. agent.ruby_llm wraps each agent run, for plain agents and for Rails records from Agent.create and find, with agent_name, agent_description, chat_id and the workflow fields. chat.ruby_llm and tool_call.ruby_llm carry agent_name too. Build invoke_agent from it and drop the Agent and Chat patches.

And in general: if you need a hook RubyLLM doesn't have, open an issue or ping me before patching. I'm happy to add it to the library, and it's usually quick. That way nothing breaks when RubyLLM moves.

Smaller notes on this PR, none blocking:

  • instrumenter.rb:217: gen_ai.response.finish_reasons should be an array, as Bugbot says. The keyword-arguments note is a false positive.
  • Provider names: RubyLLM maps its slugs to the well-known values in lib/ruby_llm/open_telemetry/attributes.rb (bedrock to aws.bedrock, gemini to gcp.gemini, and so on).
  • payload[:streaming] maps to gen_ai.request.stream, the thinking effort to gen_ai.request.reasoning.level.
  • Providers often quote the request in error bodies; recommending RubyLLM.config.redact_error_messages = true when GenAI inputs are off would keep prompts out of captured exceptions.

@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 8734db7 to 013260a Compare November 26, 2026 13:14
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 5a8f3dc.

Main concerns: data sent to Sentry, public contract, host performance.

This label is a hint for reviewers, not an approval.

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 013260a to a64478f Compare November 26, 2026 13:19
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from a64478f to 0fdbccc Compare November 26, 2026 13:20
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 0fdbccc to 7bf4f38 Compare November 26, 2026 13:24
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 6e3a396 to 5d28a19 Compare November 26, 2026 14:23
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 5d28a19 to fdad57b Compare November 26, 2026 14:37
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from fdad57b to a34c005 Compare November 26, 2026 14:44
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from a34c005 to 2641bdb Compare November 26, 2026 14:49
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 2641bdb to 3bbf335 Compare November 26, 2026 16:38
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 3bbf335 to 98116b8 Compare November 30, 2026 12:15
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 30, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 98116b8 to 14a1c01 Compare November 30, 2026 12:44
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 30, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 14a1c01 to 21ea28a Compare November 30, 2026 12:45
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 30, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from 21ea28a to aef8576 Compare November 30, 2026 13:15
Co-Authored-By: OpenAI <noreply@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants