3

I'm using the Django framework (version 1.5.1) with almost 25 inner apps, each with an avarage of 7 to 15 diferent views, quite a lot.

So to monitorize the RPM, query time, etc. . . and optime the code response I'm using the New Relic (free) service and it's very usetfull, BUT to monitorize the response time of a template/request it makes a javascript injection to the page before loading it.

Normaly it's not bad thing , unless your send email on a daily basis with a html rendered page, them it's hell on earth because the js injected in the html literary eats/destroys the html.

If your sending these mail by hand then maybe you can verify the content before sending it, but in my case it's a crontab taks, so that isn't a solution for me .

In the oficial docs there the disable_browser_autorum function that's just what I need, BUT (again) you can use the newrelic.disable_browser_autorum variable in a WSGI server, but I'm runnign a gunicorn server with supervisord, so that's not good.

But there also the newrelic.agent.disable_browser_autorum(flag=True) variable that you have to insert in your framework view and works together with the html quotes.

{% load staticfiles newrelic_tags %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
    <meta charset="utf-8" />
    {% newrelic_browser_timing_header %}
<body>
  . 
  .
  .

   {% newrelic_browser_timing_footer %}
</body>
</html>

But there's no info on how to do it, and I've also did tests on my own to try and figure how to work it.

I'm not a big developer of python or django but out of experience it must be something like

class EmailView(DetailView):
    template_name = 'email/daily-newsletter.html'
    model = News

    def get_queryset(self):
        return News.objects.filter(date=datetime.date.today())

    def get_context_data(self, **kwargs):
        context = super(EmailView, self).get_context_data(**kwargs)
        context['related_news'] = NewsRelated.objects.get(related=self.id)

        >> HERE DO SOMETHING LIKE self.request.something =newrelic.agent.disable_browser_autorum(flag=True)<<
        return context

Can anybody give me a hand with this? Any suggestion is welcommed.

PS: I've already posted a question on the official New Relic comunity, but with no luck so far.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Madox
  • 665
  • 1
  • 6
  • 14
  • I'm confused about why you think gunicorn is not a wsgi server. – Daniel Roseman Jul 09 '15 at 09:34
  • Also, it seems *very* odd that this JS would be added into an email. That code is added to the *response*, via middleware. I can't work out how your email could ever be the response. – Daniel Roseman Jul 09 '15 at 09:41
  • And finally I can't understand why you don't follow the [very clear instructions](https://docs.newrelic.com/docs/agents/python-agent/supported-features/page-load-timing-python) to just put `newrelic.agent.disable_browser_autorum()` into your view. – Daniel Roseman Jul 09 '15 at 09:41
  • And why are you manually adding the newrelic tags into the template anyway? The New Relic agent would do that automatically for you when running Django as a proper WSGI application. So just try taking out the use of the tags in your template. – Graham Dumpleton Jul 09 '15 at 10:06
  • @GrahamDumpleton I'm using the template tag to limit the monitorization to just the most important page, that are also the most used. That's what I understood from the oficial docs. – Madox Jul 09 '15 at 11:24
  • @DanielRoseman I said that gunicorn is not WSGI, refering that they are 2 different packages because you call them with different commands. After searching on the web I saw that they work the same, sorry my bad -_-' . The js was added in my mail because it's a view, I send my emails using Mailchimp, so I sent mailchimp the link to the page and then he imports the html that generates the view. continue . . . . – Madox Jul 09 '15 at 11:24
  • . . . And least I was expecting to add the newrelic.agent.disable_browser_autorum() command to my view response or something like so . . . but then after you commented "to just put it" I went to my pre production server and started 'throwing it' in my view method. At least I saw that putting it in my get_context() function worked, so again my bad, sorry -_- . – Madox Jul 09 '15 at 11:25
  • If you are doing this from a crontab completely outside of the actual running web application there should be no reason why the agent would even be active and so nothing should be injected even if the tags were used. You must be somehow activating the agent for the cron job when it isn't necessary. Are you enabling the agent for Django management commands to be tracked as background tasks? – Graham Dumpleton Jul 09 '15 at 12:07
  • @GrahamDumpleton I'm calling my task like this: 0 8 * * * /home/toor/Envs/my_project/bin/python /home/toor/www/manage.py send_nl_mailchimp --send – Madox Jul 09 '15 at 16:02

1 Answers1

4

Well thanks to my colleague & @Daniel Roseman's help my problem was solved, it was a simple solution:

import newrelic.agent
. . .

class EmailView(DetailView):
    template_name = 'email/daily-newsletter.html'
    model = News

    def get_queryset(self):
        return News.objects.filter(date=datetime.date.today())

    def get_context_data(self, **kwargs):
        context = super(EmailView, self).get_context_data(**kwargs)
        context['related_news'] = NewsRelated.objects.get(related=self.id)

        newrelic.agent.disable_browser_autorum(flag=True)
        return context

But then my colleague commented that the newrelic it's an 'enviroment variable' so just bi putting it there it will work.

So that's that.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Madox
  • 665
  • 1
  • 6
  • 14