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 3bbf335.

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 the risk: medium PR risk score: medium label Nov 26, 2026
@sl0thentr0py
sl0thentr0py force-pushed the neel/ruby-llm/chat-instrumentation branch from af0ef83 to 78e88b5 Compare November 26, 2026 13:51
@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 78e88b5 to 21d1084 Compare November 26, 2026 14:05
@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 21d1084 to 6e3a396 Compare November 26, 2026 14:08
@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
Co-Authored-By: OpenAI <noreply@openai.com>
@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
Comment on lines +53 to +68
def with_root_transaction(payload)
transaction = Sentry.start_transaction(
name: span_description(payload),
op: OPERATION,
origin: ORIGIN
)

begin
Sentry.with_scope do |scope|
scope.set_span(transaction)
execute_span(transaction, payload) { yield }
end
ensure
transaction.finish
end
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Standalone RubyLLM chats crash when tracing is disabled or another instrumenter is configured

With Sentry initialized but tracing disabled (the default) or a non-Sentry instrumenter configured, standalone RubyLLM chat calls raise instead of reaching the application block, breaking every chat operation in that configuration.

Evidence
  • with_chat_span calls with_root_transaction for an initialized Sentry scope with no current span; it does not guard against Sentry.start_transaction returning nil.
  • start_transaction returns nil when tracing is disabled or its instrumenter argument (:sentry by default) does not match the configured instrumenter. Tracing is disabled when no valid traces sample rate or sampler is configured.
  • scope.set_span(transaction) rejects nil, and the ensure then calls transaction.finish; the RubyLLM application block is never yielded. The tests configure a traces sample rate of 1.0, so they do not cover this path.
Also found at 1 additional location
  • sentry-agents/spec/sentry/agents/ruby_llm/instrumenter_spec.rb:39-71

Identified by Warden · code-review · 7XS-59X

Comment on lines +42 to +49
message.tool_calls.each do |tool_call|
result << {
type: "tool_call",
id: tool_call.id,
name: tool_call.name,
arguments: tool_call.arguments
}.compact
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

parts iterates tool_calls Hash as ToolCall objects

Non-empty message.tool_calls are a Hash of id=>ToolCall, so one-arg each yields [id, tool_call] arrays and .id/.name raise; request/response message attributes are then dropped by the instrumenter rescue.

Evidence
  • parts does message.tool_calls.each do |tool_call| then reads tool_call.id/name/arguments.
  • RubyLLM stores tool_calls as a Hash (default {}); Hash #each with one block arg yields [key, value], not the ToolCall.
  • The same serializer correctly uses payload[:chat].tools.values for the tools Hash, showing the intended pattern.
  • Instrumenter#set_request_data / #set_response_data call Serializer.messages / normalize_message inside rescue, so a tool-call message drops gen_ai.input.messages / gen_ai.output.messages instead of recording them.
  • Specs only exercise empty tool_calls, so this path is untested.

Identified by Warden · code-review · W8J-BVU

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