Instrumenting Django with Prometheus and StatsD
This repository is an example setup of Django application that exposes its metrics for Prometheus monitoring toolkit using StatsD and statsd_exporter. The application and Prometheus run on Kubernetes cluster.
Get the Olympus application running to emit StatsD metrics.
$ git clone https://github.com/marselester/django-prometheus-via-statsd.git
$ cd ./django-prometheus-via-statsd/olympus-app
$ virtualenv venv
$ source ./venv/bin/activate
$ pip install -r requirements.txt
$ python manage.py runserver
$ curl http://localhost:8000/hello
Hello, World!
Forward DogStatsD Metrics to Prometheus
Another example is based on Datadog's StatsD fork (supports metric tagging).
$ cd ./django-prometheus-via-statsd/weather-app
$ virtualenv venv
$ source ./venv/bin/activate
$ pip install -r requirements.txt
$ python manage.py runserver
$ curl http://localhost:8000/forecast
It is sunny!
If you run tcpdump, you'll see StatsD metrics with tags.
$ tcpdump -i lo0 udp port 8125 -A
E..Ld ..@................8.Kweather.weatherapi.responses.total:1|c|#code:400
E..zi...@................f.yweather.request.duration.seconds:0.00187611579895|h|#method:GET,path:/forecast,status_code:200
They will be converted by statsd_exporter into:
$ statsd_exporter \
-statsd.listen-address=:8125 \
-statsd.add-suffix=false
# HELP weather_request_duration_seconds Metric autogenerated by statsd_exporter.
# TYPE weather_request_duration_seconds summary
weather_request_duration_seconds{method="GET",path="/forecast",status_code="200",quantile="0.5"} 0.00154209136963
weather_request_duration_seconds{method="GET",path="/forecast",status_code="200",quantile="0.9"} 0.00154209136963
weather_request_duration_seconds{method="GET",path="/forecast",status_code="200",quantile="0.99"} 0.00154209136963
weather_request_duration_seconds_sum{method="GET",path="/forecast",status_code="200"} 0.00154209136963
weather_request_duration_seconds_count{method="GET",path="/forecast",status_code="200"} 1
# HELP weather_weatherapi_responses_total Metric autogenerated by statsd_exporter.
# TYPE weather_weatherapi_responses_total counter
weather_weatherapi_responses_total{code="500"} 1