Class: Vagrant::Timer::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/timer.rb

Constant Summary collapse

@@previous_action_name =
nil

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Middleware

Returns a new instance of Middleware.



23
24
25
26
27
# File 'lib/vagrant/timer.rb', line 23

def initialize(app, env)
    @init_time = Time.now.utc

    @app = app
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant/timer.rb', line 29

def call(env)
    @start_call = Time.now.utc
    @app.call(env)
    @end_call = Time.now.utc

    env_obj = env[:env]
    vagrantfile = env_obj && env_obj.vagrantfile
    config = vagrantfile && vagrantfile.config
    vm = config && config.vm
    box = vm && vm.box

    event = {
        :ruby_version    => RUBY_VERSION,
        :platform        => RUBY_PLATFORM,
        :vagrant_version => Vagrant::VERSION,
        :box             => box,
        :action_name     => env[:action_name],
        :initialized_at  => @init_time,
        :started_at      => @start_call,
        :ended_at        => @end_call,
        :init_duration   => @start_call - @init_time,
        :call_duration   => @end_call - @start_call,
    }

    if !@@previous_action_name.nil?
        event[:previous_action_name] = @@previous_action_name
        event[:since_previous_action] = @init_time - @@previous_action_end
    end

    if !ENV['VAGRANT_TIMER_LOG'].nil?
        begin
            File.open(ENV['VAGRANT_TIMER_LOG'], 'a') do |file|
                file.write(event.to_json + "\n")
            end
        rescue SystemCallError => err
            puts "Unable to log event to #{ENV['VAGRANT_TIMER_LOG']}: #{err}"
        end
    end
    @@previous_action_name = env[:action_name]
    @@previous_action_end = @end_call
end