Repository is archived
No commit activity in last 3 years
No release in over 3 years
Rack middleware for logging selected request for further investigation / analyze.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 0.7.8
~> 2.11.4
= 1.5.2
~> 0.6.3
~> 10.0
~> 3.2.0
~> 3.1.0
~> 1.4.5
~> 0.7.1
 Project Readme

Rack::RequestPolice

Code Climate Build Status Coverage Status Gem Version Dependency Status

Rack middleware for logging selected request for further investigation / analyze.

Features:

  • filter requests by method (get/post/patch/delete) and/or regular expression
  • log requests into storage of your choice (at the moment redis supported)
  • choose what headers you want to store and transform them if you want to

Work in progress.

Installation

Add this line to your application's Gemfile:

gem 'rack-request_police'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-request_police

Usage

Rails

Add do your application.rb / environment config:

Application.configure do
  # ...
  config.middleware.use Rack::RequestPolice::Middleware
end

Configure middleware using initializer (eg. config/initializers/request_police.rb).

Rack::RequestPolice.configure do |config|
  # For the time being only redis storage if provided, but you can hook up
  # any storage of your choice as long it responds to log_request and page methods
  # see Storage::Base and Storage::Redis for more references
  config.storage = Rack::RequestPolice::Storage::Redis.new(host: 'localhost', port: 6379)

  # Regular expression that will be matched against request uri
  # Nil by default (logs all requests)
  config.regex = /some-url/

  # array of methods that should be logged (all four by default)
  # requests not included in this list will be ignored
  config.method = [:get, :post, :delete, :patch]

  # array of headers that you want to store for each request (empty by default)
  config.headers = [
    "HTTP_HEADER_NAME",
    "HTTP_ANOTHER_HEADER_NAME"
  ]

  # each item in headers array can be also defined using helper method config.header
  #
  # @param original_header_name is required and it is a name of original header
  #
  # options:
  # storage_name - name of under which http header will be stored, default - original_header_name
  # fallback_value - truthy value of header that will be logged if header is missing
  #                  (missing headers are not logged by default)
  #
  # @block - optional transformations of the header before it is stored
  #
  # Example:
  # config.headers = [
  #   config.header("HTTP_HEADER_NAME",
  #     storage_name: 'HEADER_NAME',
  #     fallback_value: 'HEADER_IS_MISSING'
  #   ) { |header| header.downcase },
  #   config.header("HTTP_ANOTHER_HEADER_NAME"),
  #   config.header("HTTP_ANOTHER_FANCY_HEADER", storage_name: 'fancy_header')
  # ]
end

Contributing

  1. Fork it ( https://github.com/emq/rack-request_police/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request