Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sentry-agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions sentry-agents/lib/sentry/agents/ruby_llm/instrumenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down Expand Up @@ -115,13 +123,16 @@ 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}" }
end

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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions sentry-agents/lib/sentry/span/data_conventions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 53 additions & 0 deletions sentry-agents/spec/sentry/agents/ruby_llm/workflow_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading