-
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
Changes from 8 commits
9efeb76
06a7785
b8cc2ed
e015c42
5a894c4
682bd2a
40bf44d
20371eb
55ef497
b1eea40
ed5eb59
1d01dec
92e6198
f88922f
7861be5
30866ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,37 @@ | ||
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 @app.config.respond_to?(:event_store) | ||
@app.config.event_store.with_metadata(request_metadata(env)) do | ||
@app.call(env) | ||
end | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
def request_metadata(env) | ||
(metadata_proc || default_request_metadata).call(env) | ||
end | ||
|
||
private | ||
|
||
def metadata_proc | ||
@app.config.x.rails_event_store.request_metadata if @app.config.x.rails_event_store.request_metadata.respond_to?(:call) | ||
end | ||
|
||
def default_request_metadata | ||
->(env) do | ||
request = ActionDispatch::Request.new(env) | ||
{ | ||
remote_ip: request.remote_ip, | ||
request_id: request.uuid | ||
} | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,77 @@ | ||
require 'spec_helper' | ||
require 'action_controller/railtie' | ||
require 'rails_event_store/railtie' | ||
require 'securerandom' | ||
require 'rails_event_store/middleware' | ||
require 'rack/lint' | ||
|
||
module RailsEventStore | ||
RSpec.describe Middleware do | ||
specify 'lint' do | ||
request = ::Rack::MockRequest.new(::Rack::Lint.new(Middleware.new( | ||
->(env) { [200, {}, ['Hello World']] }, | ||
->(env) { { kaka: 'dudu' } } ))) | ||
specify 'calls app within with_metadata block when app has configured the event store instance' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that a bit too much of testing implementation details, something which one would call klasotesty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvmd, changed later |
||
app.config.event_store = event_store = Client.new | ||
expect(app).to receive(:call).with(dummy_env) | ||
middleware = Middleware.new(app) | ||
expect(event_store).to receive(:with_metadata).with(request_id: 'dummy_id', remote_ip: 'dummy_ip').and_call_original | ||
middleware.call(dummy_env) | ||
end | ||
|
||
specify 'just calls the app when app has not configured the event store instance' do | ||
expect(app).to receive(:call).with(dummy_env) | ||
middleware = Middleware.new(app) | ||
middleware.call(dummy_env) | ||
end | ||
|
||
specify 'use config.rails_event_store.request_metadata' do | ||
app.config.x.rails_event_store.request_metadata = kaka_dudu | ||
middleware = Middleware.new(app) | ||
|
||
expect { request.get('/') }.to_not raise_error | ||
expect(middleware.request_metadata(dummy_env)).to eq({ | ||
kaka: 'dudu' | ||
}) | ||
end | ||
|
||
specify do | ||
request = ::Rack::MockRequest.new(Middleware.new( | ||
->(env) { [200, {}, ['Hello World']] }, | ||
->(env) { { kaka: 'dudu' } } )) | ||
request.get('/') | ||
specify 'use config.rails_event_store.request_metadata is not callable' do | ||
app.config.x.rails_event_store.request_metadata = {} | ||
middleware = Middleware.new(app) | ||
|
||
expect(Thread.current[:rails_event_store]).to be_nil | ||
expect(middleware.request_metadata(dummy_env)).to eq({ | ||
request_id: 'dummy_id', | ||
remote_ip: 'dummy_ip' | ||
}) | ||
end | ||
|
||
specify do | ||
request = ::Rack::MockRequest.new(Middleware.new( | ||
->(env) { raise }, | ||
->(env) { { kaka: 'dudu' } } )) | ||
specify 'use config.rails_event_store.request_metadata is not set' do | ||
middleware = Middleware.new(app) | ||
|
||
expect { request.get('/') }.to raise_error(RuntimeError) | ||
expect(Thread.current[:rails_event_store]).to be_nil | ||
expect(middleware.request_metadata(dummy_env)).to eq({ | ||
request_id: 'dummy_id', | ||
remote_ip: 'dummy_ip' | ||
}) | ||
end | ||
|
||
specify do | ||
request = ::Rack::MockRequest.new(Middleware.new( | ||
->(env) { [200, {}, ['Hello World']] }, | ||
->(env) { raise } )) | ||
def kaka_dudu | ||
->(env) { { kaka: 'dudu' } } | ||
end | ||
|
||
def dummy_env | ||
{ | ||
'action_dispatch.request_id' => 'dummy_id', | ||
'action_dispatch.remote_ip' => 'dummy_ip' | ||
} | ||
end | ||
|
||
expect { request.get('/') }.to raise_error(RuntimeError) | ||
expect(Thread.current[:rails_event_store]).to be_nil | ||
def app | ||
@app ||= Class.new(::Rails::Application) do | ||
def self.name | ||
"TestRails::Application" | ||
end | ||
end.tap do |app| | ||
app.config.eager_load = false | ||
app.config.secret_key_base = SecureRandom.hex | ||
app.initialize! | ||
app.routes.draw { root(to: ->(env) {[200, {}, ['']]}) } | ||
app.default_url_options = { host: 'example.com' } | ||
end | ||
end | ||
end | ||
end | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
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?