Created
October 12, 2009 02:44
-
-
Save cpetersen/208080 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'couchrest' | |
class Log < CouchRest::ExtendedDocument | |
database = CouchRest.new | |
database.default_database = "logger" | |
use_database database.default_database | |
property :response_time | |
property :created_at | |
view_by :created_at | |
# Example view, gives the average response time by path | |
view_by :path_info_times, | |
:map => "function(doc) { | |
if ((doc['couchrest-type'] == 'Log') && doc['PATH_INFO']) { | |
emit(doc['PATH_INFO'], doc['response_time']); | |
} | |
} | |
", | |
:reduce => "function(key, values, rereduce) { | |
return sum(values)/values.length; | |
}" | |
end | |
module Rack | |
class CouchLogger | |
def initialize(app, options = {}) | |
@app = app | |
end | |
def call(env) | |
start = Time.now | |
result = @app.call(env) | |
log = Log.new(env) | |
log.response_time = (Time.now - start) | |
log.created_at = start | |
# TODO, save asynchronously with log.save(true) | |
log.save | |
result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment