Skip to content

Instantly share code, notes, and snippets.

@cpetersen
Created October 12, 2009 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cpetersen/208080 to your computer and use it in GitHub Desktop.
Save cpetersen/208080 to your computer and use it in GitHub Desktop.
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