From 644acd81306119a35c82b5e1a08e335248ffa079 Mon Sep 17 00:00:00 2001 From: Dick Davis Date: Fri, 29 Sep 2023 19:21:16 -0500 Subject: [PATCH] Remove misguided attempt at tracing (#29) --- lib/event_logger_rails/extensions/loggable.rb | 37 +--------- .../extensions/loggable_spec.rb | 73 ++----------------- 2 files changed, 9 insertions(+), 101 deletions(-) diff --git a/lib/event_logger_rails/extensions/loggable.rb b/lib/event_logger_rails/extensions/loggable.rb index 82d8e88..63ff34f 100644 --- a/lib/event_logger_rails/extensions/loggable.rb +++ b/lib/event_logger_rails/extensions/loggable.rb @@ -7,46 +7,11 @@ module Extensions module Loggable def log_event(event, level: :warn, data: {}) logger_trace.disable if instance_variable_defined?(:@logger_call_stack) - EventLoggerRails.log(event, level, data.merge(optional_data).merge(trace_data)) - end - - def trace_and_log_event(event, level: :warn, data: {}) - logger_trace.enable - yield - logger_trace.disable - log_event(event, level:, data:) + EventLoggerRails.log(event, level, data.merge(optional_data)) end private - def logger_trace - @logger_trace ||= TracePoint.new(:call) do |trace_point| - logger_call_stack << details_for_trace(trace_point) - end - end - - def details_for_trace(trace_point) - { - class: trace_point.defined_class, - method: trace_point.method_id, - parameters: trace_point.parameters.map do |type, name| - value = trace_point.binding.local_variable_get(name) - [type, name, value] - end, - path: trace_point.path.sub(%r{^#{Regexp.escape(Rails.root.to_s)}/}, '') - } - end - - def logger_call_stack - @logger_call_stack ||= [] - end - - def trace_data - { - trace: logger_call_stack[0..-3] - } - end - def optional_data {} end diff --git a/spec/lib/event_logger_rails/extensions/loggable_spec.rb b/spec/lib/event_logger_rails/extensions/loggable_spec.rb index 36c7262..2112cf7 100644 --- a/spec/lib/event_logger_rails/extensions/loggable_spec.rb +++ b/spec/lib/event_logger_rails/extensions/loggable_spec.rb @@ -8,37 +8,19 @@ class DummyClass include EventLoggerRails::Extensions::Loggable def test_one - trace_and_log_event 'event_logger_rails.event.testing' do - trace_test_one - end - end - - def test_two log_event 'event_logger_rails.event.testing' end - def test_three + def test_two log_event 'event_logger_rails.event.testing', data: { test: 'two' } end - def test_four + def test_three log_event 'event_logger_rails.event.testing', level: :info end private - def trace_test_one - trace_test_two(:foo) - end - - def trace_test_two(param) - trace_test_three(param, 'bar', { 'foo' => :bar }) - end - - def trace_test_three(foo, bar, baz) - [foo, bar, baz] - end - def optional_data { foo: 'bar' @@ -55,56 +37,17 @@ def optional_data allow(EventLoggerRails::EventLogger).to receive(:new).and_return(logger_spy) end - context 'with tracing' do - before { EventLoggerRails.reset } - - # rubocop:disable RSpec/ExampleLength - it 'calls the event logger' do - object.test_one - expect(logger_spy) - .to have_received(:log) - .with( - 'event_logger_rails.event.testing', - :warn, - { - foo: 'bar', - trace: [ - hash_including( - class: DummyClass, - method: :trace_test_one, - parameters: [], - path: anything - ), - hash_including( - class: DummyClass, - method: :trace_test_two, - parameters: [%i[req param foo]], - path: anything - ), - hash_including( - class: DummyClass, - method: :trace_test_three, - parameters: [%i[req foo foo], [:req, :bar, 'bar'], [:req, :baz, { 'foo' => :bar }]], - path: anything - ) - ] - } - ) - end - # rubocop:enable RSpec/ExampleLength - end - context 'without additional data provided' do before { EventLoggerRails.reset } it 'calls the event logger' do - object.test_two + object.test_one expect(logger_spy) .to have_received(:log) .with( 'event_logger_rails.event.testing', :warn, - { trace: [], foo: 'bar' } + { foo: 'bar' } ) end end @@ -113,13 +56,13 @@ def optional_data before { EventLoggerRails.reset } it 'calls the event logger' do - object.test_three + object.test_two expect(logger_spy) .to have_received(:log) .with( 'event_logger_rails.event.testing', :warn, - { trace: [], foo: 'bar', test: 'two' } + { foo: 'bar', test: 'two' } ) end end @@ -128,13 +71,13 @@ def optional_data before { EventLoggerRails.reset } it 'calls the event logger' do - object.test_four + object.test_three expect(logger_spy) .to have_received(:log) .with( 'event_logger_rails.event.testing', :info, - { trace: [], foo: 'bar' } + { foo: 'bar' } ) end end