open-telemetry/opentelemetry-ruby

Couldn't catch exception from Rails controller

xuan-cao-swi opened this issue · 1 comments

Description of the bug

If rails controller has error, then there is no exception_event from span.

For example, below is a sample rails controller, and create_error is not defined from User, so it should create error

class UsersController < ApplicationController
  # curl http://0.0.0.0:8002/users/new
  def new
    @user = User.new
    @user.create_error
  end
end

The sample span from above call

span_data: #<struct OpenTelemetry::SDK::Trace::SpanData name="UsersController#new", 
kind=:server, status=#<OpenTelemetry::Trace::Status:0x0000ffffa17e7588 @code=2, @description="">,
parent_span_id="\x00\x00\x00\x00\x00\x00\x00\x00", total_recorded_attributes=12, total_recorded_events=0,
total_recorded_links=0, start_timestamp=1693241683805542134, end_timestamp=1693241684087133009, 
attributes={"http.method"=>"GET", "http.host"=>"0.0.0.0:8002", "http.scheme"=>"http", "http.target"=>"/users/new",
 "http.user_agent"=>"curl/7.74.0", "code.namespace"=>"UsersController", "code.function"=>"new", 
"http.status_code"=>500}, links=nil, events=nil, resource=#<OpenTelemetry::SDK::Resources::Resource:0x0000ffffa4c8bf50 
@attributes={"service.name"=>"unknown_service", "process.pid"=>151, "process.command"=>"bin/rails", 
"process.runtime.name"=>"ruby", "process.runtime.version"=>"3.1.0", "process.runtime.description"=>"ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [aarch64-linux]", 
"telemetry.sdk.name"=>"opentelemetry", "telemetry.sdk.language"=>"ruby", "telemetry.sdk.version"=>"1.2.0"}>, 
instrumentation_scope=#<struct OpenTelemetry::SDK::InstrumentationScope 
name="OpenTelemetry::Instrumentation::Rack", version="0.22.1">, span_id="F\x7F#\xA7\x80D\e%", 
trace_id="\x83\x14\n\x9D\xB68M\x9B\x00\xD3x\xFE\xDF\xE0)0", trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x0000ffffa5c08bb8 @flags=1>, 
tracestate=#<OpenTelemetry::Trace::Tracestate:0x0000ffffa4bd91c0 @hash={"sw"=>"0000000000000000-01"}>>

Even though the status_code is 500, but there is no exception.

I checked the opentelemetry-instrumentation-rack between L81-L82, there is no record_exception even the status is 500. I added request_span.record_exception(Exception.new) if status == 500 between 81 and 82, and rack span can create exception_event.

I tried to produce error from action_view, and the exception was caught in span.

I am wondering is there particular reason or the error should be handled in somewhere else?

Similar issue I found is open-telemetry/opentelemetry-ruby-contrib#56

Share details about your runtime

Operating system details: Debian GNU/Linux aarch64 GNU/Linux
RUBY_ENGINE: "ruby"
RUBY_VERSION: "3.1.0"
RUBY_DESCRIPTION: "ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [aarch64-linux]"

Share a simplified reproduction if possible

opentelemetry-version

opentelemetry-api (1.2.2)
opentelemetry-common (0.19.6)
opentelemetry-instrumentation-action_pack (0.5.0)
opentelemetry-instrumentation-action_view (0.4.0)
opentelemetry-instrumentation-active_job (0.4.0)
opentelemetry-instrumentation-active_model_serializers (0.19.1)
opentelemetry-instrumentation-active_record (0.5.0)
opentelemetry-instrumentation-active_support (0.3.0)
opentelemetry-instrumentation-all (0.31.0)
opentelemetry-instrumentation-rack (0.22.1)
opentelemetry-instrumentation-rails (0.25.0)
opentelemetry-registry (0.3.0)
opentelemetry-sdk (1.2.0)
opentelemetry-semantic_conventions (1.10.0)