2
votes

I want to monitoring my database using prometheus, django rest framework and docker,

all is my local machine, the error is below:

enter image description here

well the error is the url http://127.0.0.1:9000/metrics, the http://127.0.0.1:9000 is the begging the my API, and I don't know what's the problem, my configuration is below

my requirements.txt

  • django-prometheus

my file 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

my folder and file 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

my file settings.py

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

MIDDLEWARE:[
    'django_prometheus.middleware.PrometheusBeforeMiddleware',
    ......
    'django_prometheus.middleware.PrometheusAfterMiddleware']

my model.py

from django_promethues.models import ExportMOdelOperationMixin

class MyModel(ExportMOdelOperationMixin('mymodel'), models.Model):
     """all my fields in here"""

my urls.py

url('', include('django_prometheus.urls')),

well the application is running well, when in the 127.0.0.1:9090/metrics, but just monitoring the same url, and I need monitoring different url, I think the problem is not the configuration except in the file prometheus.yml, because I don't know how to call my table or my api, please help me.

bye.

1

1 Answers

5
votes

you need to change your config of prometheus and add python image in docker-compose like this:

  1. config of 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

  1. docker-compose file for prometheus and django , you can also include grafana image, I have installed grafana locally:

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


  1. Dockerfile:
FROM python:3.8

COPY ./webapp/django /app
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt*strong text*
  1. For prometheus settings in django: https://pypi.org/project/django-prometheus/

  1. Hit django app api's.
  2. Hit localhost:8000/metrics api.
  3. Hit localhost:9090/ and search for the required metrics from the dropdown and click on execute it will generate result in console and create graph
  4. To show graph in the grafana hit localhost:3000 and create new dashboard.