Class: Brute::Middleware::OTel::Span
- Defined in:
- lib/brute/middleware/otel/span.rb
Overview
Outermost OTel middleware. Creates a span per LLM pipeline call and passes it through env for inner OTel middlewares to decorate with events and attributes.
When opentelemetry-sdk is not loaded, this is a pure pass-through.
Pipeline position: outermost (wraps everything including retries).
use Brute::Middleware::OTel::Span
use Brute::Middleware::OTel::ToolResults
use Brute::Middleware::OTel::ToolCalls
use Brute::Middleware::OTel::TokenUsage
# ... existing middleware ...
run Brute::Middleware::LLMCall.new
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Brute::Middleware::Base
Instance Method Details
#call(env) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/brute/middleware/otel/span.rb', line 27 def call(env) return @app.call(env) unless defined?(::OpenTelemetry::SDK) provider_name = provider_type(env[:provider]) model = begin; env[:context].model; rescue; nil; end span_name = model ? "llm.call #{model}" : "llm.call" attributes = { "brute.provider" => provider_name, "brute.streaming" => !!env[:streaming], "brute.context_messages" => env[:context]..to_a.size, } attributes["brute.model"] = model.to_s if model attributes["brute.session_id"] = env[:metadata][:session_id].to_s if env.dig(:metadata, :session_id) tracer.in_span(span_name, attributes: attributes, kind: :internal) do |span| env[:span] = span response = @app.call(env) # Record response model if it differs from request model resp_model = begin; response.model; rescue; nil; end span.set_attribute("brute.response_model", resp_model.to_s) if resp_model && resp_model != model response rescue ::StandardError => e span.record_exception(e) span.status = ::OpenTelemetry::Trace::Status.error(e.) raise ensure env.delete(:span) end end |