|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "ruby_llm/agent" |
| 4 | +require "ruby_llm/chat" |
| 5 | + |
| 6 | +module Sentry |
| 7 | + module Agents |
| 8 | + module RubyLLM |
| 9 | + module AgentRuns |
| 10 | + AGENT_NAME_IVAR = :@sentry_agents_agent_name |
| 11 | + DEPTH_IVAR = :@sentry_agents_agent_run_depth |
| 12 | + OPERATION = "gen_ai.invoke_agent" |
| 13 | + ORIGIN = "auto.ai.ruby_llm" |
| 14 | + |
| 15 | + module AgentIdentityPatch |
| 16 | + def apply_configuration(chat, input_values:, persist_instructions:, **options) |
| 17 | + result = super(chat, input_values: input_values, persist_instructions: persist_instructions, **options) |
| 18 | + chat.instance_variable_set(AGENT_NAME_IVAR, name.to_s) |
| 19 | + result |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + module ChatPatch |
| 24 | + def complete(*args, **kwargs, &block) |
| 25 | + AgentRuns.with(self) { super(*args, **kwargs, &block) } |
| 26 | + end |
| 27 | + |
| 28 | + def generate(*args, **kwargs, &block) |
| 29 | + AgentRuns.with(self) { super(*args, **kwargs, &block) } |
| 30 | + end |
| 31 | + |
| 32 | + def run_tools(*args, **kwargs, &block) |
| 33 | + AgentRuns.with(self) { super(*args, **kwargs, &block) } |
| 34 | + end |
| 35 | + |
| 36 | + def step(*args, **kwargs, &block) |
| 37 | + AgentRuns.with(self) { super(*args, **kwargs, &block) } |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + class << self |
| 42 | + def install |
| 43 | + install_agent_identity |
| 44 | + install_chat_patch |
| 45 | + end |
| 46 | + |
| 47 | + def with(chat) |
| 48 | + agent_name = chat.instance_variable_get(AGENT_NAME_IVAR) |
| 49 | + return yield if agent_name.nil? || agent_name.empty? |
| 50 | + |
| 51 | + depth = chat.instance_variable_get(DEPTH_IVAR).to_i |
| 52 | + return yield if depth.positive? |
| 53 | + |
| 54 | + chat.instance_variable_set(DEPTH_IVAR, depth + 1) |
| 55 | + with_span(chat, agent_name) { yield } |
| 56 | + ensure |
| 57 | + if depth&.positive? |
| 58 | + depth.zero? ? chat.remove_instance_variable(DEPTH_IVAR) : chat.instance_variable_set(DEPTH_IVAR, depth) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def install_agent_identity |
| 65 | + target = ::RubyLLM::Agent.singleton_class |
| 66 | + target.prepend(AgentIdentityPatch) unless target.ancestors.include?(AgentIdentityPatch) |
| 67 | + end |
| 68 | + |
| 69 | + def install_chat_patch |
| 70 | + target = ::RubyLLM::Chat |
| 71 | + target.prepend(ChatPatch) unless target.ancestors.include?(ChatPatch) |
| 72 | + end |
| 73 | + |
| 74 | + def with_span(chat, agent_name) |
| 75 | + return yield unless Sentry.initialized? |
| 76 | + |
| 77 | + scope = Sentry.get_current_scope |
| 78 | + current_span = scope&.get_span |
| 79 | + if current_span |
| 80 | + with_child_span(scope, current_span, chat, agent_name) { yield } |
| 81 | + else |
| 82 | + with_root_transaction(chat, agent_name) { yield } |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def with_child_span(scope, current_span, chat, agent_name) |
| 87 | + span = nil |
| 88 | + begin |
| 89 | + span = current_span.start_child(op: OPERATION, description: description(agent_name), origin: ORIGIN) |
| 90 | + scope.set_span(span) |
| 91 | + rescue StandardError => error |
| 92 | + safe_finish(span) |
| 93 | + report(error) |
| 94 | + return yield |
| 95 | + end |
| 96 | + |
| 97 | + execute_span(span, scope: scope, previous_span: current_span, chat: chat, agent_name: agent_name) { yield } |
| 98 | + end |
| 99 | + |
| 100 | + def with_root_transaction(chat, agent_name) |
| 101 | + transaction = begin |
| 102 | + Sentry.start_transaction(name: description(agent_name), op: OPERATION, origin: ORIGIN) |
| 103 | + rescue StandardError => error |
| 104 | + report(error) |
| 105 | + nil |
| 106 | + end |
| 107 | + return yield unless transaction |
| 108 | + |
| 109 | + application_called = false |
| 110 | + begin |
| 111 | + Sentry.with_scope do |scope| |
| 112 | + scope.set_span(transaction) |
| 113 | + execute_span(transaction, chat: chat, agent_name: agent_name) do |
| 114 | + application_called = true |
| 115 | + yield |
| 116 | + end |
| 117 | + end |
| 118 | + rescue StandardError => error |
| 119 | + raise if application_called |
| 120 | + |
| 121 | + safe_finish(transaction) |
| 122 | + report(error) |
| 123 | + yield |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + def execute_span(span, chat:, agent_name:, scope: nil, previous_span: nil) |
| 128 | + result = nil |
| 129 | + begin |
| 130 | + set_data(span, chat, agent_name) |
| 131 | + result = yield |
| 132 | + rescue StandardError => error |
| 133 | + set_error(span, error) |
| 134 | + raise |
| 135 | + ensure |
| 136 | + safe_restore_scope(scope, previous_span) if scope && previous_span |
| 137 | + safe_finish(span) |
| 138 | + end |
| 139 | + result |
| 140 | + end |
| 141 | + |
| 142 | + def set_data(span, chat, agent_name) |
| 143 | + span.set_data("gen_ai.operation.name", "invoke_agent") |
| 144 | + span.set_data("gen_ai.agent.name", agent_name) |
| 145 | + workflow = ::RubyLLM::Support::Instrumentation.current_workflow |
| 146 | + pipeline = workflow && workflow[:workflow_name] |
| 147 | + span.set_data("gen_ai.pipeline.name", pipeline) if pipeline |
| 148 | + rescue StandardError => error |
| 149 | + report(error) |
| 150 | + end |
| 151 | + |
| 152 | + def set_error(span, error) |
| 153 | + safe_set_data(span, "error.type", error.class.name) |
| 154 | + safe_set_status(span, "internal_error") |
| 155 | + end |
| 156 | + |
| 157 | + def safe_set_data(span, key, value) |
| 158 | + span.set_data(key, value) |
| 159 | + rescue StandardError => error |
| 160 | + report(error) |
| 161 | + end |
| 162 | + |
| 163 | + def safe_set_status(span, status) |
| 164 | + span.set_status(status) |
| 165 | + rescue StandardError => error |
| 166 | + report(error) |
| 167 | + end |
| 168 | + |
| 169 | + def safe_restore_scope(scope, span) |
| 170 | + scope.set_span(span) |
| 171 | + rescue StandardError => error |
| 172 | + report(error) |
| 173 | + end |
| 174 | + |
| 175 | + def safe_finish(span) |
| 176 | + span&.finish |
| 177 | + rescue StandardError => error |
| 178 | + report(error) |
| 179 | + end |
| 180 | + |
| 181 | + def report(error) |
| 182 | + Sentry.configuration.sdk_logger.debug("sentry-agents instrumentation failed: #{error.class}: #{error.message}") |
| 183 | + rescue StandardError |
| 184 | + nil |
| 185 | + end |
| 186 | + |
| 187 | + def description(agent_name) |
| 188 | + "invoke_agent #{agent_name}" |
| 189 | + end |
| 190 | + end |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | +end |
0 commit comments