Sha256: 400743a2f5d7abb7eb968f114195b076afc05fea2e7f51a372e6c6caf488deb6

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require 'new_relic/agent/event_listener'
require 'new_relic/rack/agent_middleware'
require 'new_relic/agent/instrumentation/middleware_proxy'

module NewRelic::Rack
  # This middleware is used by the agent internally, and is usually injected
  # automatically into the middleware chain. If automatic injection is not
  # working, you may manually use it in your middleware chain instead.
  #
  # @api public
  #
  class AgentHooks < AgentMiddleware
    # We use this key in the Rack env hash to note when we've already fired
    # events for a given request, in case this middleware gets installed
    # multiple times in the middleware chain because of a misconfiguration.
    ENV_KEY = "newrelic.agent_hooks_fired".freeze

    def traced_call(env)
      if env[ENV_KEY]
        # Already fired the hooks, just pass through
        @app.call(env)
      else
        env[ENV_KEY] = true

        events.notify(:before_call, env)
        result = @app.call(env)
        events.notify(:after_call, env, result)

        result
      end
    end

    def events
      NewRelic::Agent.instance.events
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_rpm-3.9.9.275 lib/new_relic/rack/agent_hooks.rb
newrelic_rpm-3.9.8.273 lib/new_relic/rack/agent_hooks.rb