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.



27
28
29
30
31
# File 'lib/vagrant/timer.rb', line 27

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

    @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
# File 'lib/vagrant/timer.rb', line 33

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.to_s,
        :started_at      => @start_call.to_s,
        :ended_at        => @end_call.to_s,
        :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?
        log_path = @init_time.strftime(ENV['VAGRANT_TIMER_LOG'])

        if !File::exist?(File::dirname(log_path))
            FileUtils.mkdir_p(File::dirname(log_path))
        end

        begin
            File.open(log_path, '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