-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to set request data in global state via middleware (#33)
* Refactored to set request data in global state via middleware * Fix sanitization in event logger
- Loading branch information
Showing
10 changed files
with
143 additions
and
20 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
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventLoggerRails | ||
## | ||
# Provides global state with request details | ||
class CurrentRequest < ActiveSupport::CurrentAttributes | ||
attribute :id, :format, :method, :parameters, :path, :remote_ip | ||
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 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
36 changes: 36 additions & 0 deletions
36
lib/event_logger_rails/middleware/capture_request_details.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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../current_request' | ||
|
||
module EventLoggerRails | ||
module Middleware | ||
## | ||
# Middleware to capture request details and store in global state | ||
class CaptureRequestDetails | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength | ||
def call(env) | ||
begin | ||
request = ActionDispatch::Request.new(env) | ||
|
||
EventLoggerRails::CurrentRequest.id = env['action_dispatch.request_id'] | ||
EventLoggerRails::CurrentRequest.format = request.headers['Content-Type'] | ||
EventLoggerRails::CurrentRequest.method = request.method | ||
EventLoggerRails::CurrentRequest.parameters = request.parameters.except(:controller, :action, :format) | ||
EventLoggerRails::CurrentRequest.path = request.path | ||
EventLoggerRails::CurrentRequest.remote_ip = request.remote_ip | ||
|
||
status, headers, body = @app.call(env) | ||
ensure | ||
EventLoggerRails::CurrentRequest.reset | ||
end | ||
|
||
[status, headers, body] | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength | ||
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 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