DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Django/NewRelic Quickstart (gunicorn.d + Django 1.3 hack)

Django/NewRelic Quickstart (gunicorn.d + Django 1.3 hack)

Chase Seibert user avatar by
Chase Seibert
·
May. 29, 12 · Interview
Like (0)
Save
Tweet
Share
6.51K Views

Join the DZone community and get the full member experience.

Join For Free
New Relic is an excellent web application performance reporting tool. After a great experience using it on Heroku, I went to enable it for an existing self-hosted application. While Django 1.4 has an excellent WSGI configuration build-in, Django 1.3 makes it a little trickier. To complicate matters even more, I was using debian's gunicorn package, which not not make changing the Django executable easy.

New Relic has excellent documentation for downloading, installing and configuring their tools. Here are my crib notes:

view plainprint?

    pip install newrelic django-newrelic-extensions  
    # sign up on newrelic.com, get $LICENSE_KEY under "Account Settings"  
    mkdir /etc/newrelic  
    newrelic-admin generate-config $LICENSE_KEY /etc/newrelic/newrelic.ini  
    newrelic-admin validate-config /etc/newrelic/newrelic.ini 

You should now be able to log in to new relic and see an application called "Python Agent Test". This means that the test worked; new relic can communicate with the hosted servers. You can run gunicorn manually from the command line with new relic as follows:

view plainprint?

    cd /opt/my-django-project  
    export NEW_RELIC_CONFIG_FILE=/etc/newrelic/newrelic.ini  
    newrelic-admin run-program python manage.py run_gunicorn  

Notice that new relic replaces the python executable with its own command, "newrelic-admin run-program". I'm using Ubuntu, which uses the Debian gunicorn package, which stores its configuration in /etc/gunicorn.d/django. Here is my current config file:

view plainprint?

    #/etc/gunicorn.d/django  
    CONFIG = {  
        'mode': 'django',  
        'working_dir': '/opt/my-django-project',  
        'user': 'django',  
        'group': 'django',  
      
        # stupid hack to get new relic to load in Django 1.3  
        # once we move to 1.4 we should use straight WSGI  
        'python': '/opt/my-django-project/conf/newrelic.sh',  
        'environment': {  
            'NEW_RELIC_CONFIG_FILE': '/etc/newrelic/newrelic.ini',  
            },  
      
        'args': (  
            '--bind=0.0.0.0:80',  
            '--workers=4',   
            '--backlog=0',  
            '--worker-class=sync',  # gevent doesn't work smoothly with new relic yet  
            '--preload',  
        ),  
    }  

Notice the hack to inject the new relic runner script. I have over-ridden the python executable path to point to a third script, newrelic.sh.

view plainprint?

    #!/usr/bin/env bash  
    /usr/local/bin/newrelic-admin run-program python $*  

This script just takes whatever arguments are passed in ($*), and passes them as arguments to newrelic-admin. That's it, a service gunicorn restart should start data flowing into new relic.

Django (web framework) Hack (falconry)

Published at DZone with permission of Chase Seibert, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • What To Know Before Implementing IIoT
  • Top 10 Best Practices for Web Application Testing
  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: