如何在 django rest 框架和 docker 中设置 prometheus
how to setup prometheus in django rest framework and docker
我想使用 prometheus、django rest 框架和 docker、
监控我的数据库
都是我的本地机器,错误如下:
嗯,错误是 url http://127.0.0.1:9000/metrics, the http://127.0.0.1:9000 是乞求我的 API,我不知道是什么问题,我的配置如下
我的requirements.txt
- django-普罗米修斯
我的文件docker:docker-compose-monitoring.yml
version: '2'
services:
prometheus:
image: prom/prometheus:v2.14.0
volumes:
- ./prometheus/:/etc/prometheus/
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- 9090:9090
grafana:
image: grafana/grafana:6.5.2
ports:
- 3060:3060
我的文件夹和文件prometheus/prometheus.yml
global:
scrape_interval: 15s
rule_files:
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 127.0.0.1:9090
- job_name: monitoring_api
static_configs:
- targets:
- 127.0.0.1:9000
我的档案settings.py
INSTALLED_APPS=[
...........
'django_prometheus',]
MIDDLEWARE:[
'django_prometheus.middleware.PrometheusBeforeMiddleware',
......
'django_prometheus.middleware.PrometheusAfterMiddleware']
我的model.py
from django_promethues.models import ExportMOdelOperationMixin
class MyModel(ExportMOdelOperationMixin('mymodel'), models.Model):
"""all my fields in here"""
我的urls.py
url('', include('django_prometheus.urls')),
好吧,应用程序是 运行 好吧,在 127.0.0.1:9090/metrics 中,但只是监控相同的 url,而我需要监控不同的 url,我认为问题不在于文件 prometheus.yml 中的配置,因为我不知道如何调用我的 table 或我的 api,请帮助我。
再见。
您需要更改 prometheus 的配置并在 docker-compose 中添加 python 图像,如下所示:
- prometheus 的配置(prometheus.yaml):
global:
scrape_interval: 15s # when Prometheus is pulling data from exporters etc
evaluation_interval: 30s # time between each evaluation of Prometheus' alerting rules
scrape_configs:
- job_name: django_project # your project name
metrics_path: /metrics
static_configs:
- targets:
- web:8000
- docker-compose prometheus 和 django 的文件,你也可以包括 grafana 图像,我已经在本地安装了 grafana:
version: '3.7'
services:
web:
build:
context: . # context represent path of your dockerfile(dockerfile present in the root dir)
command: sh -c "python3 manage.py migrate &&
gunicorn webapp.route.wsgi:application --pythonpath webapp --bind 0.0.0.0:8000"
volumes:
- .:/app
ports:
- "8000:8000"
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml #prometheus.yaml present in the root dir
- Docker 文件:
FROM python:3.8
COPY ./webapp/django /app
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt*strong text*
- 对于 django 中的 prometheus 设置:
https://pypi.org/project/django-prometheus/
- 点击 api 的 django 应用。
- 命中 localhost:8000/指标 api。
- 点击 localhost:9090/ 并从下拉列表中搜索所需的指标并单击执行它将在控制台中生成结果并创建图表
- 要在 grafana 中显示图表,请点击 localhost:3000 并创建新的仪表板。
我想使用 prometheus、django rest 框架和 docker、
监控我的数据库都是我的本地机器,错误如下:
嗯,错误是 url http://127.0.0.1:9000/metrics, the http://127.0.0.1:9000 是乞求我的 API,我不知道是什么问题,我的配置如下
我的requirements.txt
- django-普罗米修斯
我的文件docker:docker-compose-monitoring.yml
version: '2'
services:
prometheus:
image: prom/prometheus:v2.14.0
volumes:
- ./prometheus/:/etc/prometheus/
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- 9090:9090
grafana:
image: grafana/grafana:6.5.2
ports:
- 3060:3060
我的文件夹和文件prometheus/prometheus.yml
global:
scrape_interval: 15s
rule_files:
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 127.0.0.1:9090
- job_name: monitoring_api
static_configs:
- targets:
- 127.0.0.1:9000
我的档案settings.py
INSTALLED_APPS=[
...........
'django_prometheus',]
MIDDLEWARE:[
'django_prometheus.middleware.PrometheusBeforeMiddleware',
......
'django_prometheus.middleware.PrometheusAfterMiddleware']
我的model.py
from django_promethues.models import ExportMOdelOperationMixin
class MyModel(ExportMOdelOperationMixin('mymodel'), models.Model):
"""all my fields in here"""
我的urls.py
url('', include('django_prometheus.urls')),
好吧,应用程序是 运行 好吧,在 127.0.0.1:9090/metrics 中,但只是监控相同的 url,而我需要监控不同的 url,我认为问题不在于文件 prometheus.yml 中的配置,因为我不知道如何调用我的 table 或我的 api,请帮助我。
再见。
您需要更改 prometheus 的配置并在 docker-compose 中添加 python 图像,如下所示:
- prometheus 的配置(prometheus.yaml):
global:
scrape_interval: 15s # when Prometheus is pulling data from exporters etc
evaluation_interval: 30s # time between each evaluation of Prometheus' alerting rules
scrape_configs:
- job_name: django_project # your project name
metrics_path: /metrics
static_configs:
- targets:
- web:8000
- docker-compose prometheus 和 django 的文件,你也可以包括 grafana 图像,我已经在本地安装了 grafana:
version: '3.7'
services:
web:
build:
context: . # context represent path of your dockerfile(dockerfile present in the root dir)
command: sh -c "python3 manage.py migrate &&
gunicorn webapp.route.wsgi:application --pythonpath webapp --bind 0.0.0.0:8000"
volumes:
- .:/app
ports:
- "8000:8000"
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml #prometheus.yaml present in the root dir
- Docker 文件:
FROM python:3.8
COPY ./webapp/django /app
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt*strong text*
- 对于 django 中的 prometheus 设置: https://pypi.org/project/django-prometheus/
- 点击 api 的 django 应用。
- 命中 localhost:8000/指标 api。
- 点击 localhost:9090/ 并从下拉列表中搜索所需的指标并单击执行它将在控制台中生成结果并创建图表
- 要在 grafana 中显示图表,请点击 localhost:3000 并创建新的仪表板。