from django.core.urlresolvers import resolve

from snippets.base.views import fetch_snippets


class FetchSnippetsMiddleware(object):
    """
    If the incoming request is for the fetch_snippets view, execute the view
    and return it before other middleware can run.

    fetch_snippets is a very very basic view that doesn't need any of the
    middleware that the rest of the site needs, such as the session or csrf
    middlewares. To avoid unintended issues (such as headers we don't want
    being added to the response) this middleware detects requests to that view
    and executes the view early, bypassing the rest of the middleware.
    """
    def process_request(self, request):
        result = resolve(request.path)
        if result.func == fetch_snippets:
            return fetch_snippets(request, *result.args, **result.kwargs)

from django.core.urlresolvers import resolve

from snippets.base.views import fetch_snippets


class FetchSnippetsMiddleware(object):
    """
    If the incoming request is for the fetch_snippets view, execute the view
    and return it before other middleware can run.

    fetch_snippets is a very very basic view that doesn't need any of the
    middleware that the rest of the site needs, such as the session or csrf
    middlewares. To avoid unintended issues (such as headers we don't want
    being added to the response) this middleware detects requests to that view
    and executes the view early, bypassing the rest of the middleware.

    Also disables New Relic's apdex for views that aren't the
    fetch_snippets, as we only really care about the apdex for
    fetch_snippets.
    """
    def process_request(self, request):
        result = resolve(request.path)
        if result.func == fetch_snippets:
            return fetch_snippets(request, *result.args, **result.kwargs)
        else:
            # Not fetch_snippets? Then no New Relic for you!
            try:
                import newrelic.agent
            except ImportError:
                pass
            else:
                newrelic.agent.suppress_apdex_metric()

Disable New Relic's apdex metric on non-fetch_snippets views.
Disable New Relic's apdex metric on non-fetch_snippets views.

New Relic doesn't allow us to set different thresholds for different
pages across the site, so in order to get valuable metrics on the main
view for snippets, fetch_snippets, we need to disable apdex for the
admin interface and public snippets views, which don't need as close
monitoring.
  from django.core.urlresolvers import resolve
  
  from snippets.base.views import fetch_snippets
  
  
  class FetchSnippetsMiddleware(object):
      """
      If the incoming request is for the fetch_snippets view, execute the view
      and return it before other middleware can run.
  
      fetch_snippets is a very very basic view that doesn't need any of the
      middleware that the rest of the site needs, such as the session or csrf
      middlewares. To avoid unintended issues (such as headers we don't want
      being added to the response) this middleware detects requests to that view
      and executes the view early, bypassing the rest of the middleware.
+ 
+     Also disables New Relic's apdex for views that aren't the
+     fetch_snippets, as we only really care about the apdex for
+     fetch_snippets.
      """
      def process_request(self, request):
          result = resolve(request.path)
          if result.func == fetch_snippets:
              return fetch_snippets(request, *result.args, **result.kwargs)
+         else:
+             # Not fetch_snippets? Then no New Relic for you!
+             try:
+                 import newrelic.agent
+             except ImportError:
+                 pass
+             else:
+                 newrelic.agent.suppress_apdex_metric()
  
Disable New Relic's apdex metric on non-fetch_snippets views.
## Code Before:
from django.core.urlresolvers import resolve

from snippets.base.views import fetch_snippets


class FetchSnippetsMiddleware(object):
    """
    If the incoming request is for the fetch_snippets view, execute the view
    and return it before other middleware can run.

    fetch_snippets is a very very basic view that doesn't need any of the
    middleware that the rest of the site needs, such as the session or csrf
    middlewares. To avoid unintended issues (such as headers we don't want
    being added to the response) this middleware detects requests to that view
    and executes the view early, bypassing the rest of the middleware.
    """
    def process_request(self, request):
        result = resolve(request.path)
        if result.func == fetch_snippets:
            return fetch_snippets(request, *result.args, **result.kwargs)

## Instruction:
Disable New Relic's apdex metric on non-fetch_snippets views.
## Code After:
from django.core.urlresolvers import resolve

from snippets.base.views import fetch_snippets


class FetchSnippetsMiddleware(object):
    """
    If the incoming request is for the fetch_snippets view, execute the view
    and return it before other middleware can run.

    fetch_snippets is a very very basic view that doesn't need any of the
    middleware that the rest of the site needs, such as the session or csrf
    middlewares. To avoid unintended issues (such as headers we don't want
    being added to the response) this middleware detects requests to that view
    and executes the view early, bypassing the rest of the middleware.

    Also disables New Relic's apdex for views that aren't the
    fetch_snippets, as we only really care about the apdex for
    fetch_snippets.
    """
    def process_request(self, request):
        result = resolve(request.path)
        if result.func == fetch_snippets:
            return fetch_snippets(request, *result.args, **result.kwargs)
        else:
            # Not fetch_snippets? Then no New Relic for you!
            try:
                import newrelic.agent
            except ImportError:
                pass
            else:
                newrelic.agent.suppress_apdex_metric()
