diff --git a/sentry-agents/README.md b/sentry-agents/README.md index cf8f8aa1a..f9c17c484 100644 --- a/sentry-agents/README.md +++ b/sentry-agents/README.md @@ -24,6 +24,10 @@ Add the gem to your bundle: gem "sentry-agents" ``` +## Workflows + +`RubyLLM.workflow` and its steps create pipeline grouping spans. Chat and tool descendants carry `gen_ai.pipeline.name` without being labeled as agent runs. + ## GenAI data collection Configure prompt and completion content independently: diff --git a/sentry-agents/lib/sentry/agents/ruby_llm/instrumenter.rb b/sentry-agents/lib/sentry/agents/ruby_llm/instrumenter.rb index 934190f79..cadf540b5 100644 --- a/sentry-agents/lib/sentry/agents/ruby_llm/instrumenter.rb +++ b/sentry-agents/lib/sentry/agents/ruby_llm/instrumenter.rb @@ -9,9 +9,13 @@ module RubyLLM class Instrumenter CHAT_EVENT = "chat.ruby_llm" TOOL_EVENT = "tool_call.ruby_llm" + WORKFLOW_EVENT = "workflow.ruby_llm" + WORKFLOW_STEP_EVENT = "workflow_step.ruby_llm" USAGE_EVENT = "usage.ruby_llm" OPERATION = "gen_ai.chat" TOOL_OPERATION = "gen_ai.execute_tool" + WORKFLOW_OPERATION = "ai.pipeline" + WORKFLOW_STEP_OPERATION = "ai.run" ORIGIN = "auto.ai.ruby_llm" def self.install(config) @@ -24,6 +28,10 @@ def instrument(name, payload) with_chat_span(payload) { yield } if block_given? when TOOL_EVENT with_tool_span(payload) { yield } if block_given? + when WORKFLOW_EVENT + with_operation_span(payload, WORKFLOW_OPERATION, workflow_description(payload)) { yield } if block_given? + when WORKFLOW_STEP_EVENT + with_operation_span(payload, WORKFLOW_STEP_OPERATION, workflow_step_description(payload)) { yield } if block_given? when USAGE_EVENT update_usage(payload) yield if block_given? @@ -115,6 +123,8 @@ def set_request_data(span, payload, operation) set_chat_request_data(span, payload) when TOOL_OPERATION set_tool_request_data(span, payload) + when WORKFLOW_OPERATION, WORKFLOW_STEP_OPERATION + set_pipeline_data(span, payload) end rescue => e Sentry.sdk_logger.debug(Sentry::LOGGER_PROGNAME) { "Failed to set RubyLLM request data: #{e}" } @@ -122,6 +132,7 @@ def set_request_data(span, payload, operation) def set_chat_request_data(span, payload) span.set_data(Sentry::Span::DataConventions::GEN_AI_OPERATION_NAME, "chat") + set_pipeline_data(span, payload) if provider = provider_name(payload[:provider]) span.set_data(Sentry::Span::DataConventions::GEN_AI_PROVIDER_NAME, provider) @@ -165,6 +176,7 @@ def set_chat_request_data(span, payload) def set_tool_request_data(span, payload) span.set_data(Sentry::Span::DataConventions::GEN_AI_OPERATION_NAME, "execute_tool") + set_pipeline_data(span, payload) if tool_name = payload[:tool_name] || payload[:tool]&.name span.set_data(Sentry::Span::DataConventions::GEN_AI_TOOL_NAME, tool_name.to_s) @@ -189,6 +201,12 @@ def set_tool_request_data(span, payload) end end + def set_pipeline_data(span, payload) + if pipeline_name = payload[:workflow_name] + span.set_data(Sentry::Span::DataConventions::GEN_AI_PIPELINE_NAME, pipeline_name.to_s) + end + end + def set_response_data(span, payload, operation) case operation when OPERATION @@ -269,6 +287,14 @@ def tool_description(payload) tool ? "execute_tool #{tool}" : "execute_tool" end + def workflow_description(payload) + payload[:workflow_name] || "workflow" + end + + def workflow_step_description(payload) + payload[:workflow_step_name] || "workflow step" + end + def collect_inputs? Sentry.configuration.data_collection.gen_ai.inputs end diff --git a/sentry-agents/lib/sentry/span/data_conventions.rb b/sentry-agents/lib/sentry/span/data_conventions.rb index 9eb5def5b..e3d67ae19 100644 --- a/sentry-agents/lib/sentry/span/data_conventions.rb +++ b/sentry-agents/lib/sentry/span/data_conventions.rb @@ -19,6 +19,7 @@ module DataConventions GEN_AI_TOOL_DESCRIPTION = "gen_ai.tool.description" GEN_AI_TOOL_CALL_ARGUMENTS = "gen_ai.tool.call.arguments" GEN_AI_TOOL_CALL_RESULT = "gen_ai.tool.call.result" + GEN_AI_PIPELINE_NAME = "gen_ai.pipeline.name" GEN_AI_RESPONSE_MODEL = "gen_ai.response.model" GEN_AI_RESPONSE_FINISH_REASONS = "gen_ai.response.finish_reasons" GEN_AI_OUTPUT_MESSAGES = "gen_ai.output.messages" diff --git a/sentry-agents/spec/sentry/agents/ruby_llm/workflow_spec.rb b/sentry-agents/spec/sentry/agents/ruby_llm/workflow_spec.rb new file mode 100644 index 000000000..9f6f2cda4 --- /dev/null +++ b/sentry-agents/spec/sentry/agents/ruby_llm/workflow_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe "RubyLLM workflows" 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 "groups workflows and steps and propagates the pipeline name" do + result = RubyLLM.workflow("Research") do |workflow| + workflow.step("Draft") do + RubyLLM::Support::Instrumentation.instrument( + "chat.ruby_llm", + provider: "openai", + model: "gpt-4o" + ) do |event| + event[:response] = RubyLLM::Message.new(role: :assistant, content: "done", model: "gpt-4o") + :chat_result + end + end + end + + expect(result).to eq(:chat_result) + Sentry.get_current_client.flush + event = sentry_events.find { |item| item.is_a?(Sentry::TransactionEvent) } + root_trace = event.contexts[:trace] + step = event.spans.find { |span| span[:op] == "ai.run" } + chat = event.spans.find { |span| span[:op] == "gen_ai.chat" } + + expect(event.transaction).to eq("Research") + expect(root_trace[:op]).to eq("ai.pipeline") + expect(root_trace[:data]["gen_ai.pipeline.name"]).to eq("Research") + expect(step[:description]).to eq("Draft") + expect(step[:parent_span_id]).to eq(root_trace[:span_id]) + expect(step[:data]["gen_ai.pipeline.name"]).to eq("Research") + expect(chat[:parent_span_id]).to eq(step[:span_id]) + expect(chat[:data]["gen_ai.pipeline.name"]).to eq("Research") + end +end