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

如何使用 Prometheus 监测 Django

时间:2022-08-13 13:00:10浏览次数:94  
标签:python py Django Prometheus gc 监测 total

目录

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
  • 然后发现按照提示我们需要修改Model重新构建表结构:

    Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    
    June 20, 2020 - 14:17:19
    Django version 3.0.7, using settings 'mysite.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.
    

执行提示的命令:

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安装目录下,打开 prometheus.yml 文件,在scrape_configs:下添加Django任务:

    - job_name: django
      scrape_interval: 10s
      static_configs:
      - targets:
        - localhost:8000
    
  • 启动 Django应用,用 Prometheus 监视其工作情况:

    我的Django安装目录位于:~/P4/data_tutorials/mysite$

    python3.6 manage.py runserver
    
  • 启动 Prometheus

    ./prometheus --config.file=prometheus.yml
    

验证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非常强大,可轻松地检测应用程序的其他方面,包括:

  • 数据库
  • 模型(例如,监视模型的创建/删除/更新速率)
  • cache缓存
  • 代码中的自定义指标

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

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

相关文章