Sha256: 4008934c8f0cb2a7137c0376560315796f6e6d210d2ebddd6711f50f0375aa40

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'tracebin/timer'
require 'tracebin/puppet_master'

module Tracebin
  class Middleware
    attr_reader :config, :logger

    def initialize(app)
      @app = app
      @config = Tracebin::Agent.config
      @logger = Tracebin::Agent.logger

      start_agent
    end

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

    def __call(env)
      if agent_disabled?(env)
        return @app.call env
      else
        @tracebin_timer = Timer.new
        @tracebin_timer.start!

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

        @tracebin_timer.transaction_name = fetch_endpoint_name(env)

        @tracebin_timer.stop!

        PuppetMaster.new(@tracebin_timer).process

        return [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

    def start_agent
      Tracebin::Agent.start!
    rescue => e
      @logger.warn "TRACEBIN: Failed to start agent: #{e.message}"
    end

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

      !Tracebin::Agent.started? ||
        ignored_paths.any? { |root| !!root.match(path) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tracebin-0.0.10 lib/tracebin/middleware.rb