From e68cbf7cc6a82f4a6e114e80a0fb5e99ce16f469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:00:12 +0200 Subject: [PATCH 01/14] Add instrumentation for repositories on #append_to_stream Issue: #244 --- .../lib/rails_event_store/all.rb | 1 + .../instrumented_repository.rb | 16 ++++++++ .../spec/instrumented_repository_spec.rb | 39 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 rails_event_store/lib/rails_event_store/instrumented_repository.rb create mode 100644 rails_event_store/spec/instrumented_repository_spec.rb diff --git a/rails_event_store/lib/rails_event_store/all.rb b/rails_event_store/lib/rails_event_store/all.rb index 43ba5dff80..4e2d7b6af1 100644 --- a/rails_event_store/lib/rails_event_store/all.rb +++ b/rails_event_store/lib/rails_event_store/all.rb @@ -7,6 +7,7 @@ require 'rails_event_store/version' require 'rails_event_store/railtie' require 'rails_event_store/deprecations' +require 'rails_event_store/instrumented_repository' module RailsEventStore Event = RubyEventStore::Event diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb new file mode 100644 index 0000000000..5c56a54aa0 --- /dev/null +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -0,0 +1,16 @@ +module RailsEventStore + class InstrumentedRepository + def initialize(repository) + @repository = repository + end + + def append_to_stream(events, stream, expected_version) + ActiveSupport::Notifications.instrument("append_to_stream.repository.rails_event_store", events: events, stream: stream) do + repository.append_to_stream(events, stream, expected_version) + end + end + + private + attr_reader :repository + end +end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb new file mode 100644 index 0000000000..87ebf36ef9 --- /dev/null +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +module RailsEventStore + RSpec.describe InstrumentedRepository do + describe "#append_to_stream" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + event1 = Object.new + + instrumented_repository.append_to_stream([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + + expect(some_repository).to have_received(:append_to_stream).with([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:append_to_stream) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("append_to_stream.repository.rails_event_store") + event1 = Object.new + + instrumented_repository.append_to_stream([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + + expect(notification_calls).to eq([ + { events: [event1], stream: "SomeStream" } + ]) + end + end + + def subscribe_to(name) + received_payloads = [] + ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| + received_payloads << payload + end + received_payloads + end + end +end From c467e9ae7d8d3fa07da4d9102c9c3d870bc638a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:05:33 +0200 Subject: [PATCH 02/14] Add instrumentation to #link_to_stream in repositories Issue: #244 --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index 5c56a54aa0..ace7b1c884 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -10,6 +10,12 @@ def append_to_stream(events, stream, expected_version) end end + def link_to_stream(event_ids, stream, expected_version) + ActiveSupport::Notifications.instrument("link_to_stream.repository.rails_event_store", event_ids: event_ids, stream: stream) do + repository.link_to_stream(event_ids, stream, expected_version) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 87ebf36ef9..f10d9458ac 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -28,6 +28,30 @@ module RailsEventStore end end + describe "#link_to_stream" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + + instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + + expect(some_repository).to have_received(:link_to_stream).with([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:link_to_stream) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("link_to_stream.repository.rails_event_store") + + instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + + expect(notification_calls).to eq([ + { event_ids: [42], stream: "SomeStream" } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From 60555c3356f1426b87ef27fe3083c0bbc6cbf534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:08:28 +0200 Subject: [PATCH 03/14] Add instrumentation to #delete_stream in repositories --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index ace7b1c884..a63e42e99d 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -16,6 +16,12 @@ def link_to_stream(event_ids, stream, expected_version) end end + def delete_stream(stream) + ActiveSupport::Notifications.instrument("delete_stream.repository.rails_event_store", stream: stream) do + repository.delete_stream(stream) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index f10d9458ac..9d3f9d416c 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -52,6 +52,30 @@ module RailsEventStore end end + describe "#delete_stream" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + + instrumented_repository.delete_stream("SomeStream") + + expect(some_repository).to have_received(:delete_stream).with("SomeStream") + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:delete_stream) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("delete_stream.repository.rails_event_store") + + instrumented_repository.delete_stream("SomeStream") + + expect(notification_calls).to eq([ + { stream: "SomeStream" } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From 62e741aae15d37066eb9e512c93759ebee81dba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:15:48 +0200 Subject: [PATCH 04/14] Add instrumentation for #has_event? in repositories Issue: #244 --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index a63e42e99d..9388a5b77a 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -22,6 +22,12 @@ def delete_stream(stream) end end + def has_event?(event_id) + ActiveSupport::Notifications.instrument("has_event?.repository.rails_event_store", event_id: event_id) do + repository.has_event?(event_id) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 9d3f9d416c..1866275601 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -76,6 +76,30 @@ module RailsEventStore end end + describe "#has_event?" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + + instrumented_repository.has_event?(42) + + expect(some_repository).to have_received(:has_event?).with(42) + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:has_event?) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("has_event?.repository.rails_event_store") + + instrumented_repository.has_event?(42) + + expect(notification_calls).to eq([ + { event_id: 42 } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From 3f190c35fc42b8088979889bbd254c6d4b5e5ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:18:42 +0200 Subject: [PATCH 05/14] Add instrumentation to #last_stream_event in repositories Issue: #244 --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index 9388a5b77a..094a21449a 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -28,6 +28,12 @@ def has_event?(event_id) end end + def last_stream_event(stream) + ActiveSupport::Notifications.instrument("last_stream_event.repository.rails_event_store", stream: stream) do + repository.last_stream_event(stream) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 1866275601..db57f861d1 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -100,6 +100,30 @@ module RailsEventStore end end + describe "#last_stream_event" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + + instrumented_repository.last_stream_event("SomeStream") + + expect(some_repository).to have_received(:last_stream_event).with("SomeStream") + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:last_stream_event) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("last_stream_event.repository.rails_event_store") + + instrumented_repository.last_stream_event("SomeStream") + + expect(notification_calls).to eq([ + { stream: "SomeStream" } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From 77b803c1627a11dddacc2ff38a2140f40fd8be57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:24:37 +0200 Subject: [PATCH 06/14] Add instrumentation for #read_event in repositories Issue: #244 --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index 094a21449a..d3e65f7181 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -34,6 +34,12 @@ def last_stream_event(stream) end end + def read_event(event_id) + ActiveSupport::Notifications.instrument("read_event.repository.rails_event_store", event_id: event_id) do + repository.read_event(event_id) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index db57f861d1..33baa65938 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -124,6 +124,30 @@ module RailsEventStore end end + describe "#read_event" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + + instrumented_repository.read_event(42) + + expect(some_repository).to have_received(:read_event).with(42) + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:read_event) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("read_event.repository.rails_event_store") + + instrumented_repository.read_event(42) + + expect(notification_calls).to eq([ + { event_id: 42 } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From d26323ed9b8113fddb538fd7b9db24767bb13cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:30:30 +0200 Subject: [PATCH 07/14] Add instrumentation to #read in repositories Issue: #244 --- .../instrumented_repository.rb | 6 +++++ .../spec/instrumented_repository_spec.rb | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index d3e65f7181..68e2c705dc 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -40,6 +40,12 @@ def read_event(event_id) end end + def read(specification) + ActiveSupport::Notifications.instrument("read.repository.rails_event_store", specification: specification) do + repository.read(specification) + end + end + private attr_reader :repository end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 33baa65938..c1bf7972ba 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -148,6 +148,32 @@ module RailsEventStore end end + describe "#read" do + specify "wraps around original implementation" do + some_repository = spy + instrumented_repository = InstrumentedRepository.new(some_repository) + specification = double + + instrumented_repository.read(specification) + + expect(some_repository).to have_received(:read).with(specification) + end + + specify "instruments" do + some_repository = double + allow(some_repository).to receive(:read) + instrumented_repository = InstrumentedRepository.new(some_repository) + notification_calls = subscribe_to("read.repository.rails_event_store") + specification = double + + instrumented_repository.read(specification) + + expect(notification_calls).to eq([ + { specification: specification } + ]) + end + end + def subscribe_to(name) received_payloads = [] ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| From 6f54bea18d2a300e1c5cd05d27f54fd340818650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:43:29 +0200 Subject: [PATCH 08/14] Ensure that instrumented repository still behaves like repository Issue: #244 --- .../spec/instrumented_repository_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index c1bf7972ba..ebb94ccc5f 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'ruby_event_store/spec/event_repository_lint' module RailsEventStore RSpec.describe InstrumentedRepository do @@ -183,3 +184,19 @@ def subscribe_to(name) end end end + +module RailsEventStore + RSpec.describe InstrumentedRepository do + subject do + InstrumentedRepository.new(RubyEventStore::InMemoryRepository.new) + end + + let(:test_race_conditions_any) { false } + let(:test_race_conditions_auto) { false } + let(:test_expected_version_auto) { false } + let(:test_link_events_to_stream) { false } + let(:test_binary) { false } + + it_behaves_like :event_repository, InstrumentedRepository + end +end From 51d9dda1723ca54401ec9ae2a3ad667cc88caee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 21:59:39 +0200 Subject: [PATCH 09/14] Instrument a repository by default Issue: #244 --- rails_event_store/lib/rails_event_store/client.rb | 2 +- rails_event_store/spec/client_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/rails_event_store/lib/rails_event_store/client.rb b/rails_event_store/lib/rails_event_store/client.rb index 280d8c9e12..162d6fe495 100644 --- a/rails_event_store/lib/rails_event_store/client.rb +++ b/rails_event_store/lib/rails_event_store/client.rb @@ -8,7 +8,7 @@ def initialize(repository: RailsEventStoreActiveRecord::EventRepository.new, dispatcher: ActiveJobDispatcher.new, request_metadata: default_request_metadata, page_size: PAGE_SIZE) - super(repository: repository, + super(repository: InstrumentedRepository.new(repository), mapper: mapper, subscriptions: subscriptions, dispatcher: dispatcher, diff --git a/rails_event_store/spec/client_spec.rb b/rails_event_store/spec/client_spec.rb index fcecc336c8..31bfdf0be0 100644 --- a/rails_event_store/spec/client_spec.rb +++ b/rails_event_store/spec/client_spec.rb @@ -43,5 +43,18 @@ module RailsEventStore expect(published.first.metadata[:request_id]).to eq('dummy_id') expect(published.first.metadata[:timestamp]).to be_a Time end + + specify 'wraps repository into instrumentation' do + client = Client.new(repository: InMemoryRepository.new) + + received_notifications = 0 + ActiveSupport::Notifications.subscribe("append_to_stream.repository.rails_event_store") do + received_notifications += 1 + end + + client.publish(TestEvent.new) + + expect(received_notifications).to eq(1) + end end end From 1f7fc920173babadacfbacf6da8e4b9191346d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 8 Aug 2018 22:24:40 +0200 Subject: [PATCH 10/14] Remove some instrumentations These considered not necessary right now, maybe they will be added later. Issue: #244 --- .../instrumented_repository.rb | 8 ++---- .../spec/instrumented_repository_spec.rb | 26 ------------------- 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index 68e2c705dc..47f8f8c5ac 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -23,15 +23,11 @@ def delete_stream(stream) end def has_event?(event_id) - ActiveSupport::Notifications.instrument("has_event?.repository.rails_event_store", event_id: event_id) do - repository.has_event?(event_id) - end + repository.has_event?(event_id) end def last_stream_event(stream) - ActiveSupport::Notifications.instrument("last_stream_event.repository.rails_event_store", stream: stream) do - repository.last_stream_event(stream) - end + repository.last_stream_event(stream) end def read_event(event_id) diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index ebb94ccc5f..5f0b54f653 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -86,19 +86,6 @@ module RailsEventStore expect(some_repository).to have_received(:has_event?).with(42) end - - specify "instruments" do - some_repository = double - allow(some_repository).to receive(:has_event?) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("has_event?.repository.rails_event_store") - - instrumented_repository.has_event?(42) - - expect(notification_calls).to eq([ - { event_id: 42 } - ]) - end end describe "#last_stream_event" do @@ -110,19 +97,6 @@ module RailsEventStore expect(some_repository).to have_received(:last_stream_event).with("SomeStream") end - - specify "instruments" do - some_repository = double - allow(some_repository).to receive(:last_stream_event) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("last_stream_event.repository.rails_event_store") - - instrumented_repository.last_stream_event("SomeStream") - - expect(notification_calls).to eq([ - { stream: "SomeStream" } - ]) - end end describe "#read_event" do From a870a38bf63f48c86f5bb2cf70237983898364e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Thu, 9 Aug 2018 09:13:47 +0200 Subject: [PATCH 11/14] Enable additional linter tests --- rails_event_store/spec/instrumented_repository_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 5f0b54f653..465155934d 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -167,8 +167,8 @@ module RailsEventStore let(:test_race_conditions_any) { false } let(:test_race_conditions_auto) { false } - let(:test_expected_version_auto) { false } - let(:test_link_events_to_stream) { false } + let(:test_expected_version_auto) { true } + let(:test_link_events_to_stream) { true } let(:test_binary) { false } it_behaves_like :event_repository, InstrumentedRepository From d5ccda83d339706e93c9a766e427722e7cd63456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Thu, 9 Aug 2018 10:17:21 +0200 Subject: [PATCH 12/14] Use temporary subscriptions to avoid memory leaks Issue: https://github.com/RailsEventStore/rails_event_store/pull/423#discussion_r208813529 --- .../spec/instrumented_repository_spec.rb | 85 +++++++++---------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 465155934d..558e71ea89 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -15,17 +15,16 @@ module RailsEventStore end specify "instruments" do - some_repository = double - allow(some_repository).to receive(:append_to_stream) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("append_to_stream.repository.rails_event_store") - event1 = Object.new + instrumented_repository = InstrumentedRepository.new(spy) + subscribe_to("append_to_stream.repository.rails_event_store") do |notification_calls| + event1 = Object.new - instrumented_repository.append_to_stream([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + instrumented_repository.append_to_stream([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") - expect(notification_calls).to eq([ - { events: [event1], stream: "SomeStream" } - ]) + expect(notification_calls).to eq([ + { events: [event1], stream: "SomeStream" } + ]) + end end end @@ -40,16 +39,15 @@ module RailsEventStore end specify "instruments" do - some_repository = double - allow(some_repository).to receive(:link_to_stream) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("link_to_stream.repository.rails_event_store") + instrumented_repository = InstrumentedRepository.new(spy) + subscribe_to("link_to_stream.repository.rails_event_store") do |notification_calls| - instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") + instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") - expect(notification_calls).to eq([ - { event_ids: [42], stream: "SomeStream" } - ]) + expect(notification_calls).to eq([ + { event_ids: [42], stream: "SomeStream" } + ]) + end end end @@ -64,16 +62,15 @@ module RailsEventStore end specify "instruments" do - some_repository = double - allow(some_repository).to receive(:delete_stream) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("delete_stream.repository.rails_event_store") + instrumented_repository = InstrumentedRepository.new(spy) + subscribe_to("delete_stream.repository.rails_event_store") do |notification_calls| - instrumented_repository.delete_stream("SomeStream") + instrumented_repository.delete_stream("SomeStream") - expect(notification_calls).to eq([ - { stream: "SomeStream" } - ]) + expect(notification_calls).to eq([ + { stream: "SomeStream" } + ]) + end end end @@ -110,16 +107,15 @@ module RailsEventStore end specify "instruments" do - some_repository = double - allow(some_repository).to receive(:read_event) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("read_event.repository.rails_event_store") + instrumented_repository = InstrumentedRepository.new(spy) + subscribe_to("read_event.repository.rails_event_store") do |notification_calls| - instrumented_repository.read_event(42) + instrumented_repository.read_event(42) - expect(notification_calls).to eq([ - { event_id: 42 } - ]) + expect(notification_calls).to eq([ + { event_id: 42 } + ]) + end end end @@ -135,26 +131,25 @@ module RailsEventStore end specify "instruments" do - some_repository = double - allow(some_repository).to receive(:read) - instrumented_repository = InstrumentedRepository.new(some_repository) - notification_calls = subscribe_to("read.repository.rails_event_store") - specification = double + instrumented_repository = InstrumentedRepository.new(spy) + subscribe_to("read.repository.rails_event_store") do |notification_calls| + specification = double - instrumented_repository.read(specification) + instrumented_repository.read(specification) - expect(notification_calls).to eq([ - { specification: specification } - ]) + expect(notification_calls).to eq([ + { specification: specification } + ]) + end end end def subscribe_to(name) received_payloads = [] - ActiveSupport::Notifications.subscribe(name) do |_name, _start, _finish, _id, payload| - received_payloads << payload + callback = ->(_name, _start, _finish, _id, payload) { received_payloads << payload } + ActiveSupport::Notifications.subscribed(callback, name) do + yield received_payloads end - received_payloads end end end From 111efbf4e009739bc8d51f8836e1f984a130767d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Thu, 9 Aug 2018 10:33:30 +0200 Subject: [PATCH 13/14] Make ActiveSupport::Notifications a dependency Issue: #244 --- .../lib/rails_event_store/client.rb | 2 +- .../instrumented_repository.rb | 15 ++++++----- .../spec/instrumented_repository_spec.rb | 26 +++++++++---------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/rails_event_store/lib/rails_event_store/client.rb b/rails_event_store/lib/rails_event_store/client.rb index 162d6fe495..76774011eb 100644 --- a/rails_event_store/lib/rails_event_store/client.rb +++ b/rails_event_store/lib/rails_event_store/client.rb @@ -8,7 +8,7 @@ def initialize(repository: RailsEventStoreActiveRecord::EventRepository.new, dispatcher: ActiveJobDispatcher.new, request_metadata: default_request_metadata, page_size: PAGE_SIZE) - super(repository: InstrumentedRepository.new(repository), + super(repository: InstrumentedRepository.new(repository, ActiveSupport::Notifications), mapper: mapper, subscriptions: subscriptions, dispatcher: dispatcher, diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/rails_event_store/lib/rails_event_store/instrumented_repository.rb index 47f8f8c5ac..457472f48b 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/rails_event_store/lib/rails_event_store/instrumented_repository.rb @@ -1,23 +1,24 @@ module RailsEventStore class InstrumentedRepository - def initialize(repository) + def initialize(repository, instrumentation) @repository = repository + @instrumentation = instrumentation end def append_to_stream(events, stream, expected_version) - ActiveSupport::Notifications.instrument("append_to_stream.repository.rails_event_store", events: events, stream: stream) do + instrumentation.instrument("append_to_stream.repository.rails_event_store", events: events, stream: stream) do repository.append_to_stream(events, stream, expected_version) end end def link_to_stream(event_ids, stream, expected_version) - ActiveSupport::Notifications.instrument("link_to_stream.repository.rails_event_store", event_ids: event_ids, stream: stream) do + instrumentation.instrument("link_to_stream.repository.rails_event_store", event_ids: event_ids, stream: stream) do repository.link_to_stream(event_ids, stream, expected_version) end end def delete_stream(stream) - ActiveSupport::Notifications.instrument("delete_stream.repository.rails_event_store", stream: stream) do + instrumentation.instrument("delete_stream.repository.rails_event_store", stream: stream) do repository.delete_stream(stream) end end @@ -31,18 +32,18 @@ def last_stream_event(stream) end def read_event(event_id) - ActiveSupport::Notifications.instrument("read_event.repository.rails_event_store", event_id: event_id) do + instrumentation.instrument("read_event.repository.rails_event_store", event_id: event_id) do repository.read_event(event_id) end end def read(specification) - ActiveSupport::Notifications.instrument("read.repository.rails_event_store", specification: specification) do + instrumentation.instrument("read.repository.rails_event_store", specification: specification) do repository.read(specification) end end private - attr_reader :repository + attr_reader :repository, :instrumentation end end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/rails_event_store/spec/instrumented_repository_spec.rb index 558e71ea89..c5a4a21ea0 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/rails_event_store/spec/instrumented_repository_spec.rb @@ -6,7 +6,7 @@ module RailsEventStore describe "#append_to_stream" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) event1 = Object.new instrumented_repository.append_to_stream([event1], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") @@ -15,7 +15,7 @@ module RailsEventStore end specify "instruments" do - instrumented_repository = InstrumentedRepository.new(spy) + instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) subscribe_to("append_to_stream.repository.rails_event_store") do |notification_calls| event1 = Object.new @@ -31,7 +31,7 @@ module RailsEventStore describe "#link_to_stream" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") @@ -39,7 +39,7 @@ module RailsEventStore end specify "instruments" do - instrumented_repository = InstrumentedRepository.new(spy) + instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) subscribe_to("link_to_stream.repository.rails_event_store") do |notification_calls| instrumented_repository.link_to_stream([42], "SomeStream", "c456845d-2b86-49c1-bdef-89e57b5d86b1") @@ -54,7 +54,7 @@ module RailsEventStore describe "#delete_stream" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) instrumented_repository.delete_stream("SomeStream") @@ -62,7 +62,7 @@ module RailsEventStore end specify "instruments" do - instrumented_repository = InstrumentedRepository.new(spy) + instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) subscribe_to("delete_stream.repository.rails_event_store") do |notification_calls| instrumented_repository.delete_stream("SomeStream") @@ -77,7 +77,7 @@ module RailsEventStore describe "#has_event?" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) instrumented_repository.has_event?(42) @@ -88,7 +88,7 @@ module RailsEventStore describe "#last_stream_event" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) instrumented_repository.last_stream_event("SomeStream") @@ -99,7 +99,7 @@ module RailsEventStore describe "#read_event" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) instrumented_repository.read_event(42) @@ -107,7 +107,7 @@ module RailsEventStore end specify "instruments" do - instrumented_repository = InstrumentedRepository.new(spy) + instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) subscribe_to("read_event.repository.rails_event_store") do |notification_calls| instrumented_repository.read_event(42) @@ -122,7 +122,7 @@ module RailsEventStore describe "#read" do specify "wraps around original implementation" do some_repository = spy - instrumented_repository = InstrumentedRepository.new(some_repository) + instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) specification = double instrumented_repository.read(specification) @@ -131,7 +131,7 @@ module RailsEventStore end specify "instruments" do - instrumented_repository = InstrumentedRepository.new(spy) + instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) subscribe_to("read.repository.rails_event_store") do |notification_calls| specification = double @@ -157,7 +157,7 @@ def subscribe_to(name) module RailsEventStore RSpec.describe InstrumentedRepository do subject do - InstrumentedRepository.new(RubyEventStore::InMemoryRepository.new) + InstrumentedRepository.new(RubyEventStore::InMemoryRepository.new, ActiveSupport::Notifications) end let(:test_race_conditions_any) { false } From 1b0ecbec15bd2d86bbf04aa89fe525dd0446cc46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Thu, 9 Aug 2018 10:46:36 +0200 Subject: [PATCH 14/14] Move instrumentation to RubyEventStore Now, when it doesn't directly on ActiveSupport::Notifications, it can be in RubyEventStore. ActiveSupport::Notifications still is present in the tests, because it serves us as API standard. Issue: #244 --- rails_event_store/lib/rails_event_store/all.rb | 1 - rails_event_store/lib/rails_event_store/client.rb | 2 +- ruby_event_store/lib/ruby_event_store.rb | 1 + .../lib/ruby_event_store}/instrumented_repository.rb | 2 +- ruby_event_store/ruby_event_store.gemspec | 1 + .../spec/instrumented_repository_spec.rb | 7 ++++--- 6 files changed, 8 insertions(+), 6 deletions(-) rename {rails_event_store/lib/rails_event_store => ruby_event_store/lib/ruby_event_store}/instrumented_repository.rb (98%) rename {rails_event_store => ruby_event_store}/spec/instrumented_repository_spec.rb (97%) diff --git a/rails_event_store/lib/rails_event_store/all.rb b/rails_event_store/lib/rails_event_store/all.rb index 4e2d7b6af1..43ba5dff80 100644 --- a/rails_event_store/lib/rails_event_store/all.rb +++ b/rails_event_store/lib/rails_event_store/all.rb @@ -7,7 +7,6 @@ require 'rails_event_store/version' require 'rails_event_store/railtie' require 'rails_event_store/deprecations' -require 'rails_event_store/instrumented_repository' module RailsEventStore Event = RubyEventStore::Event diff --git a/rails_event_store/lib/rails_event_store/client.rb b/rails_event_store/lib/rails_event_store/client.rb index 76774011eb..cfdcaa14aa 100644 --- a/rails_event_store/lib/rails_event_store/client.rb +++ b/rails_event_store/lib/rails_event_store/client.rb @@ -8,7 +8,7 @@ def initialize(repository: RailsEventStoreActiveRecord::EventRepository.new, dispatcher: ActiveJobDispatcher.new, request_metadata: default_request_metadata, page_size: PAGE_SIZE) - super(repository: InstrumentedRepository.new(repository, ActiveSupport::Notifications), + super(repository: RubyEventStore::InstrumentedRepository.new(repository, ActiveSupport::Notifications), mapper: mapper, subscriptions: subscriptions, dispatcher: dispatcher, diff --git a/ruby_event_store/lib/ruby_event_store.rb b/ruby_event_store/lib/ruby_event_store.rb index 7e8fe245f6..14a4d20b17 100644 --- a/ruby_event_store/lib/ruby_event_store.rb +++ b/ruby_event_store/lib/ruby_event_store.rb @@ -24,3 +24,4 @@ require 'ruby_event_store/async_dispatcher' require 'ruby_event_store/debouncer' require 'ruby_event_store/version' +require 'ruby_event_store/instrumented_repository' diff --git a/rails_event_store/lib/rails_event_store/instrumented_repository.rb b/ruby_event_store/lib/ruby_event_store/instrumented_repository.rb similarity index 98% rename from rails_event_store/lib/rails_event_store/instrumented_repository.rb rename to ruby_event_store/lib/ruby_event_store/instrumented_repository.rb index 457472f48b..9e141dffc9 100644 --- a/rails_event_store/lib/rails_event_store/instrumented_repository.rb +++ b/ruby_event_store/lib/ruby_event_store/instrumented_repository.rb @@ -1,4 +1,4 @@ -module RailsEventStore +module RubyEventStore class InstrumentedRepository def initialize(repository, instrumentation) @repository = repository diff --git a/ruby_event_store/ruby_event_store.gemspec b/ruby_event_store/ruby_event_store.gemspec index 26489606d3..99f8e28ac3 100644 --- a/ruby_event_store/ruby_event_store.gemspec +++ b/ruby_event_store/ruby_event_store.gemspec @@ -35,4 +35,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'unparser' spec.add_development_dependency 'astrolabe' spec.add_development_dependency 'google-protobuf', '~> 3.5.1.2' + spec.add_development_dependency 'activesupport', '~> 5.0' end diff --git a/rails_event_store/spec/instrumented_repository_spec.rb b/ruby_event_store/spec/instrumented_repository_spec.rb similarity index 97% rename from rails_event_store/spec/instrumented_repository_spec.rb rename to ruby_event_store/spec/instrumented_repository_spec.rb index c5a4a21ea0..9a25ca84b4 100644 --- a/rails_event_store/spec/instrumented_repository_spec.rb +++ b/ruby_event_store/spec/instrumented_repository_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' require 'ruby_event_store/spec/event_repository_lint' +require 'active_support/notifications' -module RailsEventStore +module RubyEventStore RSpec.describe InstrumentedRepository do describe "#append_to_stream" do specify "wraps around original implementation" do @@ -154,10 +155,10 @@ def subscribe_to(name) end end -module RailsEventStore +module RubyEventStore RSpec.describe InstrumentedRepository do subject do - InstrumentedRepository.new(RubyEventStore::InMemoryRepository.new, ActiveSupport::Notifications) + InstrumentedRepository.new(InMemoryRepository.new, ActiveSupport::Notifications) end let(:test_race_conditions_any) { false }