lib/opentelemetry/instrumentation/rspec/formatter.rb in opentelemetry-instrumentation-rspec-0.1.0 vs lib/opentelemetry/instrumentation/rspec/formatter.rb in opentelemetry-instrumentation-rspec-0.2.0

- old
+ new

@@ -70,15 +70,29 @@ pop_and_finalize_span do |span| result = notification.example.execution_result span.set_attribute('rspec.example.result', result.status.to_s) - if (exception = result.exception) - span.record_exception(exception) - span.set_attribute('rspec.example.failure_message', exception.message) if exception.is_a? ::RSpec::Expectations::ExpectationNotMetError - span.status = OpenTelemetry::Trace::Status.error(exception.message) + add_exception_and_failures(span, result.exception) + end + end + + def add_exception_and_failures(span, exception) + return if exception.nil? + + exception_message = strip_console_codes(exception.message) + span.status = OpenTelemetry::Trace::Status.error(exception_message) + + span.set_attribute('rspec.example.failure_message', exception_message) if exception.is_a? ::RSpec::Expectations::ExpectationNotMetError + + if exception.is_a? ::RSpec::Core::MultipleExceptionError + exception.all_exceptions.each do |error| + record_stripped_exception(span, error) end + span.set_attribute('rspec.example.failure_message', multiple_failure_message(exception)) if exception.failures.any? + else + span.record_exception(exception, attributes: { 'exception.message' => exception_message }) end end def track_span(span) token = OpenTelemetry::Context.attach( @@ -93,9 +107,22 @@ yield span if block_given? span.finish(end_timestamp: current_timestamp) OpenTelemetry::Context.detach(token) + end + + def record_stripped_exception(span, error) + error_message = strip_console_codes(error.message) + span.record_exception(error, attributes: { 'exception.message' => error_message }) + end + + def strip_console_codes(string) + string.gsub(/\e\[([;\d]+)?m/, '') + end + + def multiple_failure_message(exception) + exception.failures.map(&:message).map(&method(:strip_console_codes)).join("\n\n") end end end end end