-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from RailsEventStore/with-metadata
Allow to dynamically enrich events metadata by using `with_metadata`
- Loading branch information
Showing
16 changed files
with
359 additions
and
184 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
module RailsEventStore | ||
class Client < RubyEventStore::Client | ||
attr_reader :request_metadata | ||
|
||
def initialize(repository: RailsEventStoreActiveRecord::EventRepository.new, | ||
mapper: RubyEventStore::Mappers::Default.new, | ||
event_broker: EventBroker.new(dispatcher: ActiveJobDispatcher.new), | ||
request_metadata: default_request_metadata, | ||
page_size: PAGE_SIZE) | ||
capture_metadata = ->{ Thread.current[:rails_event_store] } | ||
super(repository: repository, | ||
mapper: mapper, | ||
event_broker: event_broker, | ||
page_size: page_size, | ||
metadata_proc: capture_metadata) | ||
page_size: page_size) | ||
@request_metadata = request_metadata | ||
end | ||
|
||
def with_request_metadata(env, &block) | ||
with_metadata(request_metadata.call(env)) do | ||
block.call | ||
end | ||
end | ||
|
||
private | ||
def default_request_metadata | ||
->(env) do | ||
request = ActionDispatch::Request.new(env) | ||
{ | ||
remote_ip: request.remote_ip, | ||
request_id: request.uuid | ||
} | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
module RailsEventStore | ||
class Middleware | ||
def initialize(app, request_metadata_proc) | ||
def initialize(app) | ||
@app = app | ||
@request_metadata_proc = request_metadata_proc | ||
end | ||
|
||
def call(env) | ||
Thread.current[:rails_event_store] = @request_metadata_proc.(env) | ||
@app.call(env) | ||
ensure | ||
Thread.current[:rails_event_store] = nil | ||
if config.respond_to?(:event_store) | ||
config.event_store.with_request_metadata(env) do | ||
@app.call(env) | ||
end | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
def config | ||
Rails.application.config | ||
end | ||
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,47 @@ | ||
require 'spec_helper' | ||
|
||
module RailsEventStore | ||
RSpec.describe Client do | ||
TestEvent = Class.new(RailsEventStore::Event) | ||
|
||
specify 'has default request metadata proc if no custom one provided' do | ||
client = Client.new | ||
expect(client.request_metadata.call({ | ||
'action_dispatch.request_id' => 'dummy_id', | ||
'action_dispatch.remote_ip' => 'dummy_ip' | ||
})).to eq({ | ||
remote_ip: 'dummy_ip', | ||
request_id: 'dummy_id' | ||
}) | ||
end | ||
|
||
specify 'allows to set custom request metadata proc' do | ||
client = Client.new( | ||
request_metadata: -> env { {server_name: env['SERVER_NAME']} } | ||
) | ||
expect(client.request_metadata.call({ | ||
'SERVER_NAME' => 'example.org' | ||
})).to eq({ | ||
server_name: 'example.org' | ||
}) | ||
end | ||
|
||
specify 'published event metadata will be enriched by metadata provided in request metadata when executed inside a with_request_metadata block' do | ||
client = Client.new( | ||
repository: InMemoryRepository.new, | ||
) | ||
event = TestEvent.new | ||
client.with_request_metadata( | ||
'action_dispatch.request_id' => 'dummy_id', | ||
'action_dispatch.remote_ip' => 'dummy_ip' | ||
) do | ||
client.publish_event(event) | ||
end | ||
published = client.read_all_streams_forward | ||
expect(published.size).to eq(1) | ||
expect(published.first.metadata[:remote_ip]).to eq('dummy_ip') | ||
expect(published.first.metadata[:request_id]).to eq('dummy_id') | ||
expect(published.first.metadata[:timestamp]).to be_a Time | ||
end | ||
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
require 'action_controller/railtie' | ||
require 'rails_event_store/railtie' | ||
require 'securerandom' | ||
|
||
class TestApplication < Rails::Application | ||
config.eager_load = false | ||
config.secret_key_base = SecureRandom.hex(16) | ||
initialize! | ||
routes.default_url_options = { host: 'example.org' } | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.