Skip to content

Commit

Permalink
Remove misguided attempt at tracing (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
dickdavis authored Sep 30, 2023
1 parent 3bfa5eb commit 644acd8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 101 deletions.
37 changes: 1 addition & 36 deletions lib/event_logger_rails/extensions/loggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 8 additions & 65 deletions spec/lib/event_logger_rails/extensions/loggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 644acd8

Please sign in to comment.