Sha256: c9871924467fbad1db428d125749bef4245d4d97c8ef15dd5b2ac5eb421e31d1

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'vizsla/timer'
require 'vizsla/puppet_master'

module Vizsla
  class Middleware
    attr_reader :config

    def initialize(app)
      @app = app
      @config = Vizsla::Agent.config

      Vizsla::Agent.start! unless Vizsla::Agent.started?
    end

    def call(env)
      dup.__call(env)
    end

    def __call(env)
      path = env['REQUEST_PATH']
      ignored_paths = config.ignored_paths.map { |root| %r{^#{root}} }

      if ignored_paths.any? { |root| !!root.match(path) }
        @app.call env
      else
        timer = Timer.new
        timer.start!

        status, headers, response = @app.call(env)

        timer.transaction_name = fetch_endpoint_name env

        timer.stop!

        PuppetMaster.new(timer).process

        [status, headers, response]
      end
    end

    private

    def fetch_endpoint_name(env)
      if controller = env['action_controller.instance']
        "#{controller.class}##{controller.params['action']}"
      else
        'RackTransaction'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tracebin-0.0.7 lib/vizsla/middleware.rb