Sentry is an open source application exception tracking system, which is supported by Django+DRF+Celery+ celery-beat. If you are Pythoner and are familiar with these technical stack, Sentry uses Django+DRF+Celery+ celery-beat. You can read the relevant source code, and there are a lot of things to learn. It is worth mentioning that Sentry only supports Python2, not Python3, when deployed.
In this article we will deploy Sentry as Docker, which is the official recommended deployment, and write a simple Django application that uses email and spikes to notify the developer of any exceptions that occur.
The environment
I ran a cloud server on Vultr for this test, using CentOS7.
Initialization operation
Install the epel source
$ yum install epel-release -y
Copy the code
Update the system
$ yum update -y
Copy the code
Install some toolkits
$ yum install python-pip vim git -y
Copy the code
You are advised to restart the system
$ reboot
Copy the code
The basic configuration
-
memory
$ free -h
total used free shared buff/cache available
Mem: 3.7G 95M 3.4G 8.4M 168M 3.4G
Swap: 0B 0B 0B
Copy the code
-
CPU
$ cat /proc/cpuinfo | grep processor | wc -l
2
Copy the code
The configuration is also 2H4G, if you are a server with 1G memory, it seems that IT is difficult for me to run on Tencent cloud, I feel at least 2G.
Install the Docker
The installation of CentOS series of documents on: https://docs.docker.com/install/linux/docker-ce/centos/, interested can go to reading, I’ll simplify some operations here.
-
Install some software packages
$ yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
-
Add the Docker’s repo source
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code
-
Install the docker
$ yum install docker-ce -y
Copy the code
-
Start the docker
$ systemctl start docker
Copy the code
-
Self-start upon startup
$ systemctl enable docker
Copy the code
-
View the Docker version
$ docker --version
Docker version 18.03.1-ce, build 9ee9f40
Copy the code
-
Run a hello-world
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
Copy the code
If you get the same results as ME, then, OK, Docker is installed.
If you use a domestic server, it may be unusually slow in the pull image, so the official provides domestic Docker image acceleration, point me point me, after the configuration must remember to restart the Docker service, otherwise the configuration cannot be loaded, restart the command is as follows:
-
$ systemctl restart docker
Copy the code
Install the docker – compose
https://github.com/docker/compose/releases/download/1.21.2/docker-compose- $curl - L ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Copy the code
I installed is 1.21.2 version, usually you install the docker and docker – compose is the latest version will not have what problem, check the docker – compose version
-
$ docker -compose -- version
-
Docker-compose version 1.21.2, Build A133471
Copy the code
Install the Sentry
Finally, the key step, Sentry, officially provides a complete set of Docker deployment mode to run Sentry.
-
Download the project
$ cd /opt/
$ git clone https://github.com/getsentry/onpremise.git sentry
$ cd sentry
Copy the code
-
Create the database and Sentry configuration directory
$ mkdir -p data/{sentry,postgres}
Copy the code
-
Add some dependency libraries
$ vim requirements.txt
# Add plugins here
Sentry-dingding ~=0.0.1 # pin notification plugin
Django-smtp-ssl ~=1.0 # Supports SSL for sending emails
Copy the code
-
Build the Docker image
$ docker-compose build
Copy the code
-
Generate the key
$ docker-compose run --rm web config generate-secret-key
.
The last line will output a secret key similar to the following
kbjodp(id&b0^kbnxijn11&2e6xu&vy1(oini! -zl)pl610n&v
Copy the code
Add the above key to the SENTRY_SECRET_KEY environment variable in the docker-comemage. yml file
-
$ vim docker -compose. yml
-
.
-
SENTRY_SECRET_KEY : 'kbjodp(id&b0^kbnxijn11&2e6xu&vy1(oini! -zl)pl610n&v'
Copy the code
-
Generate database tables and create administrator users
$ docker-compose run --rm web upgrade
.
Would you like to create a user account now? [Y/n]: Y # Create a user
Email: [email protected] #
Password: # Password
Repeat for confirmation: #
Should this user be a superuser? [y/N]: y # is the super administrator
Copy the code
-
Start the service
$ docker-compose up -d
Copy the code
You can use docker-compose PS to see which containers are started
-
$ docker -compose ps
-
Name Command State Ports
-
------------------------------------------------------------------------------------
-
sentry_cron_1 /entrypoint. sh run cron Up 9000 /tcp
-
sentry_memcached_1 docker -entrypoint. sh memcached Up 11211 /tcp
-
sentry_postgres_1 docker -entrypoint. sh postgres Up 5432 /tcp
-
sentry_redis_1 docker -entrypoint. sh redis ... Up 6379 /tcp
-
sentry_smtp_1 docker -entrypoint. sh tini ... Up 25 /tcp
-
Sentry_web_1 /entrypoint. sh run Web Up 0.0.0.0 :9000 ->9000 / TCP
-
sentry_worker_1 /entrypoint. sh run worker Up 9000 /tcp
Copy the code
The above services are described as follows:
The name of the | describe |
---|---|
sentry_cron |
Scheduled task, usingcelery-beat |
sentry_memcached |
memcached |
sentry_postgres |
PGSQL database |
sentry_redis |
Run celery required services |
sentry_smtp |
|
sentry_web |
usedjango+drf Write a setSentry Web interface |
sentry_worker |
Celery worker service, used to run asynchronous tasks |
When the service is started, it listens on port 9000 by default. If you want to change it, you can do so in docker-comemage.yml.
I added an A record to my domain name ansheng.me. The record value is sentry, which points to the cloud server of Sentry, so it can be accessed through sentry.ansheng. me.
Basic Sentry Settings
The browser open http://sentry.ansheng.me:9000/, enter email address and password to log in
Once logged in successfully, enter the corresponding RootURL and Admin Email, and click Continue
Clicking takes you to the main Sentry screen
Add a Python project
Click Addnew in the upper right corner and select Project
And then create the project
After the project has been created, a use screen appears
Testing Python programs
Following the above steps, let’s do it step by step. I’m operating on my server, and the Python version is as follows:
$ python -V
Python 2.7.5
Copy the code
-
Install the raven
$ pip install raven --upgrade
Copy the code
-
Adding test code
$ vim sentry_python_test.py
from raven import Client
client = Client('http://ce5502f746a4484f9b2c391a54d2d1c4:[email protected]:9000/2')
try:
1/0
except ZeroDivisionError:
client.captureException()
Copy the code
-
run
$ python sentry_python_test.py
Sentry is attempting to send 1 pending error messages
Waiting up to 10 seconds
Press Ctrl-C to quit
Copy the code
Check the abnormal
In the usage screen above, click Gotit~Takeme to theIssueStream. Enter the project’s Issue page, or error page
You can see that there is already an exception. This exception is the error that we caught when we tested. Click on the heading of ZeroDivisionError to enter the detailed page of the exception.
On the project page, you can see MESSAGE and EXCEPTION, which output the error details
Basically the steps are the same as above, the flow is similar
Add Django projects and monitor them
Follow the steps above to create the PythonSentry project and add a Django framework project named Cash and save the DSN record values
http://9ad8168873a94fb1927e14111b9bca1e:[email protected]:9000/3
Creating a Django project
Django projects I created on my computer.
-
Add a virtual environment named Cash
$pyEnv VirtualEnv 3.6.5 cash
Copy the code
-
Switching virtual Environments
$ pyenv activate cash
Copy the code
-
Install django
$ pip install django
Copy the code
-
Create a project
$ cd /tmp
$ django-admin startproject cash
$ cd cash
$ python manage.py migrate
Copy the code
-
Start the project
$ python manage.py runserver 0:9999
Performing system checks...
System check identified no issues (0 silenced).
July 17, 2018 - 03:35:05
Django version 2.0.7, using settings 'cash.settings'
Starting development server at http://0:9999/
Quit the server with CONTROL-C.
Copy the code
Port listening at 9999, you can use curl to test access
-
$curl -i HTTP :// 127.0.1:9999
-
HTTP / 1.1 200 OK
-
.
Copy the code
A status of 200 indicates no problem
-
Viewing the directory Structure
➜ cash tree ./
. /
├ ─ ─ cash
│ ├ ─ ─ just set py
│ ├ ─ ─ Settings. Py
│ ├ ─ ─ urls. Py
│ ├ ─ ─ views. Py
│ └ ─ ─ wsgi. Py
├ ─ ─ the sqlite3
└ ─ ─ the manage. Py
Copy the code
-
Write a simple view
$ vim cash/views.py
from django.http import HttpResponse
def success(request):
return HttpResponse("OK")
def error(request):
1/0
return HttpResponse("Not OK")
Copy the code
-
Add the url
$ vim cash/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('success', views.success, name='success'),
path('error', views.error, name='error'),
]
Copy the code
-
Access to the test
Access the API that returns success
$curl http://127.0.0.1:9999/success
OK
Copy the code
Accessing an ERROR reporting API
$curl -i http://127.0.0.1:9999/error
HTTP/1.1 500 Internal Server Error
.
Copy the code
An error of 500 was reported, and the log output is as follows
-
Internal Server Error : /error
-
Traceback ( most recent call last ):
-
The File "/ Users/shengan /. / lib/python3.6 pyenv/versions/cash/site - packages/django/core/handlers/exception. Py", line 35, in inner
-
response = get_response (request )
-
The File "/ Users/shengan /. / lib/python3.6 pyenv/versions/cash/site - packages/django/core/handlers/base. Py", line 128, in _get_response
-
response = self. process_exception_by_middleware( e, request)
-
The File "/ Users/shengan /. / lib/python3.6 pyenv/versions/cash/site - packages/django/core/handlers/base. Py", line 126, in _get_response
-
response = wrapped_callback (request , *callback_args , **callback_kwargs )
-
File "/private/tmp/cash/cash/views.py", line 8 , in error
-
1/0
-
ZeroDivisionError : division by zero
-
[17/ Jul/ 2018 03:50:55] "HTTP/1.1" 500 60675
Copy the code
Integrated Sentry
-
Install the raven
$ pip install raven --upgrade
Copy the code
-
Add raven.contrib.django.raven_compat to INSTALLED_APPS
INSTALLED_APPS = (
.
'raven.contrib.django.raven_compat',
)
Copy the code
-
Add the Sentry configuration in Setting
import os
import raven
RAVEN_CONFIG = {
'dsn': 'http://9ad8168873a94fb1927e14111b9bca1e:[email protected]:9000/3' # DSN enter DSN we just recorded
}
Copy the code
-
After that, I’ll let’s test it out
Wrong API access
$curl -i http://127.0.0.1:9999/error
Copy the code
-
Exception list
-
Exception details
Configuring Email Notification
I use netease email here, and the specific operations are as follows
-
Modify the config.yml file to add the Mail Server configuration
$ vim config.yml
mail.backend: 'django_smtp_ssl.SSLEmailBackend' # Use dummy if you want to disable email entirely
mail.host: 'smtp.163.com'
mail.port: 465
mail.username: '[email protected]'
Mail. password: '14RJg5vzGFWUNKiP'
mail.use-tls: false
# The email address to send on behalf of
mail.from: '[email protected]'
Copy the code
-
Rebuild the image
$ docker-compose build
$ docker-compose up -d
Copy the code
After the restart, the configuration of the mailbox can be found under Admin==> mail
Then click below to send a test email to [email protected], and then go to your email to see if you received the email
I can receive the test email sent successfully. Usually, I can receive the abnormal notification after the email configuration is completed.
-
Verify the user’s mailbox
Find the Settings = > Account = > Emails
Then click Resendverification and you receive a verification email
Click Confirm to Confirm email.
-
Exception notification test
And then, we’re accessing the wrong API
$curl -i http://127.0.0.1:9999/error
HTTP/1.1 500 Internal Server Error
Copy the code
-
Check your mailbox for alarm emails
Basically, you’ll get an error email similar to the one above. Somehow that avatar doesn’t show up. Anyway, if you want to see the error details, click on ViewonSentry to open the exception details.
Configure pin notification
As we all know, the timeliness of mailbox notification is too poor to be delivered without delay, so we focus on the group notification of Dingdingrobot.
-
Create a pin group to add a custom robot and obtain an access_token
I’m here for the access_token dadfa77dfc3fe3b34e91237665afbef165e745dd93ec191c48bf4843b1ad63c 4
-
configuration
Find all integrations for the Cash project
Then locate the DingDing Plugin, enable the Plugin, and click Configure Plugin to Configure the Plugin
Enter the access_token you just obtained and click Save Changes to Save your Changes
-
Test Exception Notification
Then we access the API for the following exception
$curl -i http://127.0.0.1:9999/error
HTTP/1.1 500 Internal Server Error
.
Copy the code
Your stitching robot should now send the following error
Click the href above to jump to the error details page, and this article ends.