其他分享
首页 > 其他分享> > 如何使用 Prometheus 监测 Django

如何使用 Prometheus 监测 Django

作者:互联网

目录

Introduction

Prometheus是一个开源系统监视和警报工具包,可用于轻松、廉价地监视基础结构和应用程序。在教程中,我们将展示使用Prometheus监视Django应用程序。

Prerequisites

A machine with the following installed:

Step 0 - 设置基本的Django应用程序

(Please skip this step if you already have a Django application.)

安装 Django

注:在我们的虚机上,Python3.6是独立于Python3.5安装的。

$ which python3.5
/usr/bin/python3.5

$ which python3.6
/usr/local/bin/python3.6

Django最低支持Python3.6,所以安装时用以下命令指定pip3.6安装Django:

sudo pip3.6 install Django

Create a template project

进入要选择安装的目录,然后运行:

django-admin startproject mysite

这将在当前目录中创建一个mysite目录,该目录如下所示:

mysite/
manage.py
mysite/
  __init__.py
  settings.py
  urls.py
  asgi.py
  wsgi.py

验证Django是否正常工作

进入安装目录下的 mysite 目录, 运行以下命令:

python3.6 manage.py runserver

执行提示的命令:

python3.6 manage.py migrate

执行上述命令后再次启动Django应用:

python3.6 manage.py runserver

浏览器中打开 http://127.0.0.1:8000/

我们可以看到“Congratulations!”的Django已成功安装的页面。

Step 1 - 从Django中导出Prometheus监视指标

我们将使用django-prometheus软件包从Django应用程序中导出prometheus样式的监视指标。

安装django-prometheus

在Django安装目录下用pip3 install命令安装一定要加上sudo,按照官方文档的命令行不通:

sudo pip3.6 install django-prometheus

修改 settings.pyurls.py配置文件

settings.py配置文件中添加如下内容:

INSTALLED_APPS = [
   ...
   'django_prometheus',
   ...
]

MIDDLEWARE = [
    'django_prometheus.middleware.PrometheusBeforeMiddleware',
    # All your other middlewares go here, including the default
    # middlewares like SessionMiddleware, CommonMiddleware,
    # CsrfViewmiddleware, SecurityMiddleware, etc.
    'django_prometheus.middleware.PrometheusAfterMiddleware',
]

urls.py配置文件中,添加头文件:

from django.conf.urls import include
from django.conf.urls import url

然后在urlpatterns下添加:

urlpatterns = [
    ...
    url('', include('django_prometheus.urls')),
]

验证是否正在导出指标

在安装目录/mysite下重新启动Django并验证metrics:

python manage.py runserver
curl localhost:8000/metrics

(或者浏览器直接访问 http://localhost:8000/metrics )

然后应该可以看到以下内容:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 11716.0
python_gc_objects_collected_total{generation="1"} 1699.0
python_gc_objects_collected_total{generation="2"} 616.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 7020.0
python_gc_collections_total{generation="1"} 638.0
python_gc_collections_total{generation="2"} 34.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="8",patchlevel="0",version="3.8.0"} 1.0
...

Step 2 - 将Prometheus指向Django应用程序指标

(Note: This section assumes that you have a locally running Prometheus instance.)

验证Prometheus是否正在从Django应用程序中抓取指标:

浏览器打开本地主机的Prometheus: http://localhost:9090/graph.

  1. 在Expression中输入: django_http_requests_total_by_method_total,在Graph界面点击 Execute 。
  2. Prometheus以图形的方式显示了我们的Django应用程序在一段时间内收到的HTTP请求总数。

Prometheus graph of Total HTTP Requests in the last hour

Step 3 - 分析应用程序的其他方面(可选)

Django-prometheus非常强大,可轻松地检测应用程序的其他方面,包括:

More information on how to do all of these is here.

标签:python,py,Django,Prometheus,gc,监测,total
来源: https://www.cnblogs.com/cscshi/p/16582831.html