Prometheus can't scrape Django metrics (400 Bad Request) when running in Docker container

I'm trying to collect prometheus-django metrics from a Django application running inside a Docker container. The metrics are exposed at http://127.0.0.1:8888/prometheus/metrics, and I can access them from my local machine. However, when Prometheus (running in another container) tries to scrape them, I get a 400 Bad Request response.

400 Bad Request

docker-compose:

  backend:
    build: ./store/
    container_name: store_backend
    env_file: .env
    volumes:
      - static:/backend_static/
      - media:/app/media/
  
  nginx:
    build: ./nginx/
    container_name: store_nginx
    env_file: .env
    volumes:
      - static:/staticfiles/
      - media:/app/media/
    ports:
      - ${PORT}:80
    depends_on:
      - backend

  prometheus:
    build: ./monitoring
    container_name: store_prometheus
    volumes:
      - prometheus_data:/prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
    ports:
      - "9090:9090"

prometheus.yml:

global:
  scrape_interval: 5s

scrape_configs:
  - job_name: 'django'
    static_configs:
      - targets: ['store_backend:8888']
    metrics_path: '/prometheus/metrics'

nginx.conf:

    location /prometheus/metrics/ {
        proxy_set_header Host $http_host;
        proxy_pass http://backend:8888/prometheus/metrics/;
    }

While troubleshooting this issue, I set ALLOWED_HOSTS = ['*'], but it doesn't work.

So Prometheus can connect to the Django container, but the Django app rejects the request. Can you help me to understand what might be causing this issue?

Вернуться на верх