Class: Stackprofiler::DataCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/stackprofiler/data_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ DataCollector

Returns a new instance of DataCollector.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/stackprofiler/data_collector.rb', line 3

def initialize(app, options)
  @app = app

  pred = options[:predicate] || /profile=true/
  if pred.respond_to? :call
    @predicate = pred
  else
    regex = Regexp.new pred

    @predicate = proc do |env|
      req = Rack::Request.new env
      req.fullpath =~ regex
    end
  end

  @stackprof_opts = {mode: :wall, interval: 1000, raw: true}.merge(options[:stackprof] || {})
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stackprofiler/data_collector.rb', line 21

def call(env)
  if @predicate.call(env)
    out = nil
    profile = StackProf.run(@stackprof_opts) { out = @app.call env }

    req = Rack::Request.new env
    run = Run.new req.fullpath, profile, Time.now
    RunDataSource.runs << run

    out
  else
    @app.call env
  end
end