Permalink
Browse files

middleware and around filter for tracing web requests

  • Loading branch information...
1 parent b901940 commit 59e8fc1b0d3af04e7021f50fc63ed473cf0528db @tmm1 tmm1 committed Apr 27, 2010
Showing with 34 additions and 0 deletions.
  1. +34 −0 lib/memprof/tracer.rb
View
@@ -0,0 +1,34 @@
+begin
+ require File.expand_path('../../memprof', __FILE__)
+rescue LoadError
+ require File.expand_path('../../../ext/memprof', __FILE__)
+end
+
+module Memprof
+ # Middleware for tracing requests
+ #
+ # require 'memprof/tracer'
+ # config.middleware.use(Memprof::Tracer)
+ class Tracer
+ def initialize(app)
+ @app=app
+ end
+ def call(env)
+ Memprof.trace_filename ||= "/tmp/memprof_tracer-#{Process.pid}.json"
+ Memprof.trace_request(env){ @app.call(env) }
+ end
+ end
+
+ # Legacy filter for tracing requests on Rails 2.2
+ #
+ # require 'memprof/tracer'
+ # around_filter(Memprof::Filter)
+ module Filter
+ def self.filter(controller)
+ env = controller.request.env
+ info = controller.request.path_parameters
+ Memprof.trace_filename ||= "/tmp/memprof_tracer-#{Process.pid}.json"
+ Memprof.trace_request(env.merge('action_controller.request.path_parameters' => info)){ yield }
+ end
+ end
+end

0 comments on commit 59e8fc1

Please sign in to comment.