-
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
Allow to dynamically enrich events metadata by using with_metadata
#327
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9efeb76
Add silence_warnings helper for RSpec
jakubkosinski 06a7785
Allow to dynamically set events' metadata with with_metadata
jakubkosinski b8cc2ed
Deprecate using metadata_proc
jakubkosinski e015c42
No need for aditional begin...ensure block here
jakubkosinski 5a894c4
Move metadata_proc warning to constructor and kill some mutants
jakubkosinski 682bd2a
Add some docs for with_metadata method
jakubkosinski 40bf44d
Add missing parenthesis
jakubkosinski 20371eb
Rewrite the middleware
jakubkosinski 55ef497
Update docs
jakubkosinski b1eea40
No need to pass metadata_proc anymore
jakubkosinski ed5eb59
Use Rails.application global in middleware to retrieve the config
jakubkosinski 1d01dec
Ensure each instance of RubyEventStore::Client has its own metadata
jakubkosinski 92e6198
Extract TestApplication for specs
jakubkosinski f88922f
Move setting request_metadata proc to RailsEventStore::Client
jakubkosinski 7861be5
Update docs
jakubkosinski 30866ab
Composable with_metadata calls
jakubkosinski 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
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.
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.
Why have you decided to remove
Rack::Lint
that verifies Middleware in accordance toRack
specification?