Class: StackProf::RouteMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/stackprof/route_middleware.rb,
lib/stackprof/route_middleware/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @app = app
  @options = options

  self.class.mode = options[:mode] || :cpu
  self.class.interval = options[:interval] || 1000
  self.class.raw = options[:raw] || false
  self.class.enabled = options[:enabled]
  options[:path] = "tmp/" if options[:path].to_s.empty?
  self.class.path = options[:path]
  self.class. = options[:metadata] || {}
  self.class.route_calculator = options[:route_calculator]

  warn "StackProf::RouteMiddleware does not support save_every option. It regards save_every is 1." if options[:save_every] && options[:save_every] != 1
  warn "StackProf::RouteMiddleware does not support save_at_exit option." if options[:save_at_exit]
end

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def enabled
  @enabled
end

.intervalObject

Returns the value of attribute interval.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def interval
  @interval
end

.metadataObject

Returns the value of attribute metadata.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def 
  @metadata
end

.modeObject

Returns the value of attribute mode.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def mode
  @mode
end

.pathObject

Returns the value of attribute path.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def path
  @path
end

.rawObject

Returns the value of attribute raw.



44
45
46
# File 'lib/stackprof/route_middleware.rb', line 44

def raw
  @raw
end

.route_calculatorObject

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

Returns:

  • (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