Skip to content

Commit

Permalink
Log GET and POST params
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Jun 19, 2010
1 parent 8268162 commit 3c6ceb1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/mongo_metrics.rb
Expand Up @@ -4,16 +4,19 @@ class MongoMetrics
DB_NAME = "mongo_metrics"

def initialize(app)
require "mongo"

@app = app
@db = Mongo::Connection.new.db(DB_NAME)
@collection = @db["requests"]
end

def call(env)
request = Rack::Request.new(env)
status, headers, body = @app.call(env)

body = BodyWrapper.new(body) do
@collection.insert "foo" => "bar"
@collection.insert "params" => request.params
end

[status, headers, body]
Expand Down
13 changes: 11 additions & 2 deletions spec/bootstrap_spec.rb
@@ -1,9 +1,18 @@
require "spec_helper"
require "fixture/config/environment"

describe MongoMetrics do
it "records one document per request" do
get "/", :id => 42
get "/"
requests_collection.count.should == 1
end

it "records GET params" do
get "/", "id" => "42"
request_document["params"]["id"].should == "42"
end

it "records POST params" do
post "/", "id" => "42"
request_document["params"]["id"].should == "42"
end
end
1 change: 0 additions & 1 deletion spec/fixture/app/controllers/application_controller.rb
@@ -1,5 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery

def hello
render :text => "hello"
Expand Down
4 changes: 4 additions & 0 deletions spec/fixture/config/boot.rb
@@ -1,4 +1,8 @@
require 'rubygems'
require 'pathname'

mongo_metrics_path = Pathname.new(__FILE__).join("..", "..", "..", "..", "lib").expand_path
$LOAD_PATH.unshift mongo_metrics_path

# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
Expand Down
1 change: 1 addition & 0 deletions spec/fixture/config/initializers/mongo_metrics.rb
@@ -1 +1,2 @@
require "mongo_metrics"
Fixture::Application.config.middleware.use MongoMetrics
2 changes: 1 addition & 1 deletion spec/fixture/config/routes.rb
Expand Up @@ -48,8 +48,8 @@

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.

root :to => "application#hello"
# root :to => "welcome#index"

# See how all your routes lay out with "rake routes"

Expand Down
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -8,11 +8,18 @@
require "rspec"
require "rack/test"

ENV["RAILS_ENV"] = "test"
require "fixture/config/environment"

RSpec.configure do |c|
c.include Rack::Test::Methods

c.before do
mongo.drop_database(MongoMetrics::DB_NAME)
db.collections.each(&:drop)
end

def request_document
requests_collection.find_one
end

def requests_collection
Expand Down

0 comments on commit 3c6ceb1

Please sign in to comment.