-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add instrumentation for repositories #423
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e68cbf7
Add instrumentation for repositories on #append_to_stream
swistak35 c467e9a
Add instrumentation to #link_to_stream in repositories
swistak35 60555c3
Add instrumentation to #delete_stream in repositories
swistak35 62e741a
Add instrumentation for #has_event? in repositories
swistak35 3f190c3
Add instrumentation to #last_stream_event in repositories
swistak35 77b803c
Add instrumentation for #read_event in repositories
swistak35 d26323e
Add instrumentation to #read in repositories
swistak35 6f54bea
Ensure that instrumented repository still behaves like repository
swistak35 51d9dda
Instrument a repository by default
swistak35 1f7fc92
Remove some instrumentations
swistak35 a870a38
Enable additional linter tests
swistak35 d5ccda8
Use temporary subscriptions to avoid memory leaks
swistak35 111efbf
Make ActiveSupport::Notifications a dependency
swistak35 1b0ecbe
Move instrumentation to RubyEventStore
swistak35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
ruby_event_store/lib/ruby_event_store/instrumented_repository.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module RubyEventStore | ||
class InstrumentedRepository | ||
def initialize(repository, instrumentation) | ||
@repository = repository | ||
@instrumentation = instrumentation | ||
end | ||
|
||
def append_to_stream(events, stream, expected_version) | ||
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) | ||
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) | ||
instrumentation.instrument("delete_stream.repository.rails_event_store", stream: stream) do | ||
repository.delete_stream(stream) | ||
end | ||
end | ||
|
||
def has_event?(event_id) | ||
repository.has_event?(event_id) | ||
end | ||
|
||
def last_stream_event(stream) | ||
repository.last_stream_event(stream) | ||
end | ||
|
||
def read_event(event_id) | ||
instrumentation.instrument("read_event.repository.rails_event_store", event_id: event_id) do | ||
repository.read_event(event_id) | ||
end | ||
end | ||
|
||
def read(specification) | ||
instrumentation.instrument("read.repository.rails_event_store", specification: specification) do | ||
repository.read(specification) | ||
end | ||
end | ||
|
||
private | ||
attr_reader :repository, :instrumentation | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
require 'spec_helper' | ||
require 'ruby_event_store/spec/event_repository_lint' | ||
require 'active_support/notifications' | ||
|
||
module RubyEventStore | ||
RSpec.describe InstrumentedRepository do | ||
describe "#append_to_stream" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
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 | ||
instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) | ||
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") | ||
|
||
expect(notification_calls).to eq([ | ||
{ events: [event1], stream: "SomeStream" } | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#link_to_stream" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
|
||
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 | ||
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") | ||
|
||
expect(notification_calls).to eq([ | ||
{ event_ids: [42], stream: "SomeStream" } | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#delete_stream" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
|
||
instrumented_repository.delete_stream("SomeStream") | ||
|
||
expect(some_repository).to have_received(:delete_stream).with("SomeStream") | ||
end | ||
|
||
specify "instruments" do | ||
instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) | ||
subscribe_to("delete_stream.repository.rails_event_store") do |notification_calls| | ||
|
||
instrumented_repository.delete_stream("SomeStream") | ||
|
||
expect(notification_calls).to eq([ | ||
{ stream: "SomeStream" } | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#has_event?" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
|
||
instrumented_repository.has_event?(42) | ||
|
||
expect(some_repository).to have_received(:has_event?).with(42) | ||
end | ||
end | ||
|
||
describe "#last_stream_event" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
|
||
instrumented_repository.last_stream_event("SomeStream") | ||
|
||
expect(some_repository).to have_received(:last_stream_event).with("SomeStream") | ||
end | ||
end | ||
|
||
describe "#read_event" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
|
||
instrumented_repository.read_event(42) | ||
|
||
expect(some_repository).to have_received(:read_event).with(42) | ||
end | ||
|
||
specify "instruments" do | ||
instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) | ||
subscribe_to("read_event.repository.rails_event_store") do |notification_calls| | ||
|
||
instrumented_repository.read_event(42) | ||
|
||
expect(notification_calls).to eq([ | ||
{ event_id: 42 } | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#read" do | ||
specify "wraps around original implementation" do | ||
some_repository = spy | ||
instrumented_repository = InstrumentedRepository.new(some_repository, ActiveSupport::Notifications) | ||
specification = double | ||
|
||
instrumented_repository.read(specification) | ||
|
||
expect(some_repository).to have_received(:read).with(specification) | ||
end | ||
|
||
specify "instruments" do | ||
instrumented_repository = InstrumentedRepository.new(spy, ActiveSupport::Notifications) | ||
subscribe_to("read.repository.rails_event_store") do |notification_calls| | ||
specification = double | ||
|
||
instrumented_repository.read(specification) | ||
|
||
expect(notification_calls).to eq([ | ||
{ specification: specification } | ||
]) | ||
end | ||
end | ||
end | ||
|
||
def subscribe_to(name) | ||
received_payloads = [] | ||
callback = ->(_name, _start, _finish, _id, payload) { received_payloads << payload } | ||
ActiveSupport::Notifications.subscribed(callback, name) do | ||
yield received_payloads | ||
end | ||
end | ||
end | ||
end | ||
|
||
module RubyEventStore | ||
RSpec.describe InstrumentedRepository do | ||
subject do | ||
InstrumentedRepository.new(InMemoryRepository.new, ActiveSupport::Notifications) | ||
end | ||
|
||
let(:test_race_conditions_any) { false } | ||
let(:test_race_conditions_auto) { false } | ||
let(:test_expected_version_auto) { true } | ||
let(:test_link_events_to_stream) { true } | ||
let(:test_binary) { false } | ||
|
||
it_behaves_like :event_repository, InstrumentedRepository | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍