Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active July 10, 2019 08:05
Show Gist options
  • Save voluntas/7278351 to your computer and use it in GitHub Desktop.
Save voluntas/7278351 to your computer and use it in GitHub Desktop.
Django + New Relic コトハジメ

Django + New Relic コトハジメ

更新:2013-11-02
バージョン:0.0.3
作者:@voluntas
URL:http://voluntas.github.io/

概要

url:http://newrelic.com/

New Relic というパフォーマンス監視サービスがかなりよさそう。

サーバやサーバアプリ、モバイルアプリのパフォーマンスを見ることが出来る。

導入もかなり簡単で、取得できる情報はかなり細かいところまで取れるようです。

プラグインも色々あるみたいですが、今回は調べません。

セットアップ

New Relic のサイトでアカウント取得してください

newrelic の Python ライブラリをインストールする

$ pip install newrelic

Reveal your license key をクリックしてライセンスを取得する。

LICENSE_KEY は New Relic のサイトから取得した文字列を指定する

https://dl.dropboxusercontent.com/u/89936/gist/newrelic-django/new_relic_license_key.png

$ newrelic-admin generate-config <LICENSE_KEY> newrelic.ini

設定ファイルを弄ったら validate-config でチェックすること

validate-config:

newrelic-admin validate-config newrelic.ini

Django

Django に設定する方法はいくつかありますが wsgi に設定し普通に起動する方法と newrelic-admin から起動する方法が二つあります。

wsgi

wsgi.py に設定する方法、シンプルなのでとてもオススメです。

import os

import newrelic.agent

newrelic.agent.initialize('/path/to/newrelic.ini')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
application = newrelic.agent.wsgi_application()(application)

gunicorn

newrelic-admin run-python から mange.py run_gunicorn を実行する方法。

$ NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-python manage.py run_gunicorn -w 3
2013-11-02 03:47:51 [4841] [INFO] Starting gunicorn 18.0
2013-11-02 03:47:51 [4841] [INFO] Listening at: http://127.0.0.1:8000 (4841)
2013-11-02 03:47:51 [4841] [INFO] Using worker: sync
2013-11-02 03:47:51 [4844] [INFO] Booting worker with pid: 4844
2013-11-02 03:47:51 [4845] [INFO] Booting worker with pid: 4845
2013-11-02 03:47:51 [4846] [INFO] Booting worker with pid: 4846

uWSGI

未検証

newrelic-admin run-program から uwsgi を起動する方法。

$ newrelic-admin run-program uwsgi --socket /tmp/uwsgi.sock --single-interpreter --enable-threads wsgi.py

New Relic

ウェブアプリの組み込みが完了したらアプリサーバを起動し New Relic のサイトから Connect to my application をクリックする

https://dl.dropboxusercontent.com/u/89936/gist/newrelic-django/new_relic_connect_application.png

アクセスして少し立つと以下のように Activate application(s) detected, head to the dashbord をクリックしてセットアップは終了です。

https://dl.dropboxusercontent.com/u/89936/gist/newrelic-django/new_relic_activate_application.png

あとは好きなだけウェブサイトにアクセスしてパフォーマンスを計測しましょう

https://dl.dropboxusercontent.com/u/89936/gist/newrelic-django/new_relic_dashboard.png

参考

Set Up New Relic for a Django project | djangosteps
http://djangosteps.wordpress.com/2013/07/03/set-up-new-relic-for-django-project-in-development/
Take a deep dive tour of New Relic monitoring for Python/Django, now officially available today! - New Relic blog
http://blog.newrelic.com/2011/11/08/new-relic-supports-python/
Real user monitoring in Python - New Relic Documentation
https://docs.newrelic.com/docs/python/real-user-monitoring-in-python
Python agent and uWSGI - New Relic Documentation
https://docs.newrelic.com/docs/python/python-agent-and-uwsgi
Python agent and gunicorn - New Relic Documentation
https://docs.newrelic.com/docs/python/python-agent-and-gunicorn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment