




version: ‘3.6’
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:3000
第2步:配置你的Prometheus
为了能够让Prometheus跑起来,你需要创建一份prometheus.yml文件。
我把这个文件放在了./prometheus/目录下,同时这个目录里也放了刚刚创建的docker-compose.monitoring.yml文件。
prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# — “first.rules”
# — “second.rules”
scrape_configs:
— job_name: monitoring
static_configs:
— targets:
— host.docker.internal
host.docker.internal用来获取目标监控对象的IP地址。你可以从这里找到更多的信息:https://docs.docker.com/docker-for-windows/networking。
这里的url是仅用作开发目的。如果你面对的是在生产环境中部署好的应用,那么在生产环境中,你也应该拥有一个Prometheus服务器。并且你需要修改相应配置文件prometheus.yml,将这个host.docker.internal改成生产环境中目标机器的域名地址,比如your-prod-application.com。
为了能够让Prometheus和Grafana跑起来,你需要在你的命令行终端,敲上这个命令:
$ docker-compose -f docker-compose.monitoring.yml up -d
那么你将在浏览器里输入localhost:9090后,看到下面的东西:

pip install django-prometheus
在你的配置项里,增加下面内容:
settings.py
BASE_INSTALLED_APPS = [
...
“django_prometheus”,
]
MIDDLEWARE = [
“django_prometheus.middleware.PrometheusBeforeMiddleware”,
...
“django_prometheus.middleware.PrometheusAfterMiddleware”,
]
那么暴露给Prometheus的endpoints就设置完成了,这个url地址会是localhost/metrics。
urls.py
url(“”, include(“django_prometheus.urls”)),
现在你可以到,你的prometheus targets对象会是"UP"状态:http://localhost:9090/targets。

-
点击graph title,然后点击“Edit”。
-
在“Metrics”tab页,选择你的Prometheus数据源(页面右下角)。
-
在“Query”一栏,输入任何的Prometheus查询表达式,并且使用“Metric”选项去完成补全。



你已经注意到,我们已经在本地把我们的Grafana和Prometheus跑起来了。一旦你做到这点,你也准备好用你自己的方式去部署你的Grafana和Prometheus Docker镜像[3]。 我希望这10分钟的教程可以帮到你,让你方便、快速地使用Prometheus和Grafana去监控一个Django应用。 相关链接:
-
https://hub.docker.com/r/grafana/grafana
-
https://github.com/korfuri/django-prometheus/
-
https://www.katacoda.com/courses/docker/deploying-first-container
