Class: StackProf::RouteMiddleware
- Inherits:
-
Object
- Object
- StackProf::RouteMiddleware
- Defined in:
- lib/stackprof/route_middleware.rb,
lib/stackprof/route_middleware/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.enabled ⇒ Object
Returns the value of attribute enabled.
-
.interval ⇒ Object
Returns the value of attribute interval.
-
.metadata ⇒ Object
Returns the value of attribute metadata.
-
.mode ⇒ Object
Returns the value of attribute mode.
-
.path ⇒ Object
Returns the value of attribute path.
-
.raw ⇒ Object
Returns the value of attribute raw.
-
.route_calculator ⇒ Object
Returns the value of attribute route_calculator.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ RouteMiddleware
constructor
A new instance of RouteMiddleware.
Constructor Details
#initialize(app, options = {}) ⇒ RouteMiddleware
Returns a new instance of RouteMiddleware.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/stackprof/route_middleware.rb', line 8 def initialize(app, = {}) @app = app @options = self.class.mode = [:mode] || :cpu self.class.interval = [:interval] || 1000 self.class.raw = [:raw] || false self.class.enabled = [:enabled] [:path] = "tmp/" if [:path].to_s.empty? self.class.path = [:path] self.class. = [:metadata] || {} self.class.route_calculator = [:route_calculator] warn "StackProf::RouteMiddleware does not support save_every option. It regards save_every is 1." if [:save_every] && [:save_every] != 1 warn "StackProf::RouteMiddleware does not support save_at_exit option." if [:save_at_exit] end |
Class Attribute Details
.enabled ⇒ Object
Returns the value of attribute enabled.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def enabled @enabled end |
.interval ⇒ Object
Returns the value of attribute interval.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def interval @interval end |
.metadata ⇒ Object
Returns the value of attribute metadata.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def @metadata end |
.mode ⇒ Object
Returns the value of attribute mode.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def mode @mode end |
.path ⇒ Object
Returns the value of attribute path.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def path @path end |
.raw ⇒ Object
Returns the value of attribute raw.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def raw @raw end |
.route_calculator ⇒ Object
Returns the value of attribute route_calculator.
44 45 46 |
# File 'lib/stackprof/route_middleware.rb', line 44 def route_calculator @route_calculator end |
Class Method Details
.enabled?(env) ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/stackprof/route_middleware.rb', line 46 def enabled?(env) if enabled.respond_to?(:call) enabled.call(env) else enabled end end |
.save(env) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/stackprof/route_middleware.rb', line 54 def save(env) results = StackProf.results return unless results raise "Expected path to be directory" if path == path.chomp("/") route = route_calculator.call(env) return unless route filename = "stackprof-#{route.gsub(/\W/, "_")}-#{results[:mode]}-#{Process.pid}-#{Time.now.to_i}.dump" FileUtils.mkdir_p(path) File.binwrite(File.join(path, filename), Marshal.dump(results)) filename end |
Instance Method Details
#call(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/stackprof/route_middleware.rb', line 25 def call(env) enabled = self.class.enabled?(env) if enabled StackProf.start( mode: self.class.mode, interval: self.class.interval, raw: self.class.raw, metadata: self.class. ) end @app.call(env) ensure if enabled StackProf.stop self.class.save(env) end end |