Skip to content

Commit 9cae549

Browse files
committed
Add Prometheus and Grafana
1 parent 8f30e4c commit 9cae549

7 files changed

Lines changed: 144 additions & 3 deletions

File tree

app/forum/forum/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.dispatch import receiver
66
from django.utils import timezone
77

8+
from django_prometheus.models import ExportModelOperationsMixin
89
from uuslug import uuslug
910

1011
from utils.renderer import render
@@ -54,7 +55,7 @@ def __str__(self):
5455
return self.slug
5556

5657

57-
class Thread(CachedAuthorModel):
58+
class Thread(ExportModelOperationsMixin('thread'), CachedAuthorModel):
5859
"""Contains posts."""
5960
slug = models.SlugField(max_length=SLUG_LENGTH)
6061
title = models.CharField(max_length=140, verbose_name='Titre')
@@ -145,7 +146,7 @@ def __str__(self):
145146
return "{}/{}".format(self.category.slug, self.slug)
146147

147148

148-
class Post(CachedAuthorModel):
149+
class Post(ExportModelOperationsMixin('post'), CachedAuthorModel):
149150
"""A post."""
150151
created = models.DateTimeField(default=timezone.now,
151152
editable=False)

app/forum/naxos/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"django.contrib.humanize",
6868

6969
# Third-party Apps
70+
"django_prometheus",
7071
"storages",
7172
"crispy_forms",
7273

@@ -79,12 +80,14 @@
7980
)
8081

8182
MIDDLEWARE = [
83+
'django_prometheus.middleware.PrometheusBeforeMiddleware',
8284
"django.middleware.security.SecurityMiddleware",
8385
"django.contrib.sessions.middleware.SessionMiddleware",
8486
"django.middleware.common.CommonMiddleware",
8587
"django.middleware.csrf.CsrfViewMiddleware",
8688
"django.contrib.auth.middleware.AuthenticationMiddleware",
8789
"django.contrib.messages.middleware.MessageMiddleware",
90+
'django_prometheus.middleware.PrometheusAfterMiddleware',
8891
]
8992

9093
ROOT_URLCONF = "naxos.urls"
@@ -130,7 +133,7 @@
130133
# Database
131134
DATABASES = {
132135
"default": {
133-
"ENGINE": "django.db.backends.postgresql_psycopg2",
136+
"ENGINE": "django_prometheus.db.backends.postgresql",
134137
"NAME": os.environ.get("DB_NAME", "postgres"),
135138
"USER": os.environ.get("DB_USER", "postgres"),
136139
"PASSWORD": os.environ.get("DB_PASSWORD", "crimson"),

app/forum/naxos/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
path('messages/', include('pm.urls')),
1616
path('blog/', include('blog.urls')),
1717
path('admin/', admin.site.urls),
18+
path('', include('django_prometheus.urls')),
1819
]
1920

2021
if settings.DEBUG:

app/forum/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ django-storages<2.0
66
django-uuslug<1.2
77
markdown<2.7
88
postmarkup<1.3
9+
django-prometheus
910
psycopg2<2.8
1011
pylibmc<1.6
1112
setproctitle

conf/django.rules.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
groups:
3+
- name: django.rules
4+
rules:
5+
- record: job:django_http_requests_before_middlewares_total:sum_rate30s
6+
expr: sum by(job) (rate(django_http_requests_before_middlewares_total[30s]))
7+
- record: job:django_http_requests_unknown_latency_total:sum_rate30s
8+
expr: sum by(job) (rate(django_http_requests_unknown_latency_total[30s]))
9+
- record: job:django_http_ajax_requests_total:sum_rate30s
10+
expr: sum by(job) (rate(django_http_ajax_requests_total[30s]))
11+
- record: job:django_http_responses_before_middlewares_total:sum_rate30s
12+
expr: sum by(job) (rate(django_http_responses_before_middlewares_total[30s]))
13+
- record: job:django_http_requests_unknown_latency_including_middlewares_total:sum_rate30s
14+
expr: sum by(job) (rate(django_http_requests_unknown_latency_including_middlewares_total[30s]))
15+
- record: job:django_http_requests_body_total_bytes:sum_rate30s
16+
expr: sum by(job) (rate(django_http_requests_body_total_bytes[30s]))
17+
- record: job:django_http_responses_streaming_total:sum_rate30s
18+
expr: sum by(job) (rate(django_http_responses_streaming_total[30s]))
19+
- record: job:django_http_responses_body_total_bytes:sum_rate30s
20+
expr: sum by(job) (rate(django_http_responses_body_total_bytes[30s]))
21+
- record: job:django_http_requests_total:sum_rate30s
22+
expr: sum by(job) (rate(django_http_requests_total_by_method[30s]))
23+
- record: job:django_http_requests_total_by_method:sum_rate30s
24+
expr: sum by(job, method) (rate(django_http_requests_total_by_method[30s]))
25+
- record: job:django_http_requests_total_by_transport:sum_rate30s
26+
expr: sum by(job, transport) (rate(django_http_requests_total_by_transport[30s]))
27+
- record: job:django_http_requests_total_by_view:sum_rate30s
28+
expr: sum by(job, view) (rate(django_http_requests_total_by_view_transport_method[30s]))
29+
- record: job:django_http_requests_total_by_view_transport_method:sum_rate30s
30+
expr: sum by(job, view, transport, method) (rate(django_http_requests_total_by_view_transport_method[30s]))
31+
- record: job:django_http_responses_total_by_templatename:sum_rate30s
32+
expr: sum by(job, templatename) (rate(django_http_responses_total_by_templatename[30s]))
33+
- record: job:django_http_responses_total_by_status:sum_rate30s
34+
expr: sum by(job, status) (rate(django_http_responses_total_by_status[30s]))
35+
- record: job:django_http_responses_total_by_charset:sum_rate30s
36+
expr: sum by(job, charset) (rate(django_http_responses_total_by_charset[30s]))
37+
- record: job:django_http_exceptions_total_by_type:sum_rate30s
38+
expr: sum by(job, type) (rate(django_http_exceptions_total_by_type[30s]))
39+
- record: job:django_http_exceptions_total_by_view:sum_rate30s
40+
expr: sum by(job, view) (rate(django_http_exceptions_total_by_view[30s]))
41+
- record: job:django_http_requests_latency_including_middlewares_seconds:quantile_rate30s
42+
expr: histogram_quantile(0.5, sum by(job, le) (rate(django_http_requests_latency_including_middlewares_seconds_bucket[30s])))
43+
labels:
44+
quantile: "50"
45+
- record: job:django_http_requests_latency_including_middlewares_seconds:quantile_rate30s
46+
expr: histogram_quantile(0.95, sum by(job, le) (rate(django_http_requests_latency_including_middlewares_seconds_bucket[30s])))
47+
labels:
48+
quantile: "95"
49+
- record: job:django_http_requests_latency_including_middlewares_seconds:quantile_rate30s
50+
expr: histogram_quantile(0.99, sum by(job, le) (rate(django_http_requests_latency_including_middlewares_seconds_bucket[30s])))
51+
labels:
52+
quantile: "99"
53+
- record: job:django_http_requests_latency_including_middlewares_seconds:quantile_rate30s
54+
expr: histogram_quantile(0.999, sum by(job, le) (rate(django_http_requests_latency_including_middlewares_seconds_bucket[30s])))
55+
labels:
56+
quantile: "99.9"
57+
- record: job:django_http_requests_latency_seconds:quantile_rate30s
58+
expr: histogram_quantile(0.5, sum by(job, le) (rate(django_http_requests_latency_seconds_bucket[30s])))
59+
labels:
60+
quantile: "50"
61+
- record: job:django_http_requests_latency_seconds:quantile_rate30s
62+
expr: histogram_quantile(0.95, sum by(job, le) (rate(django_http_requests_latency_seconds_bucket[30s])))
63+
labels:
64+
quantile: "95"
65+
- record: job:django_http_requests_latency_seconds:quantile_rate30s
66+
expr: histogram_quantile(0.99, sum by(job, le) (rate(django_http_requests_latency_seconds_bucket[30s])))
67+
labels:
68+
quantile: "99"
69+
- record: job:django_http_requests_latency_seconds:quantile_rate30s
70+
expr: histogram_quantile(0.999, sum by(job, le) (rate(django_http_requests_latency_seconds_bucket[30s])))
71+
labels:
72+
quantile: "99.9"
73+
- record: job:django_model_inserts_total:sum_rate1m
74+
expr: sum by(job, model) (rate(django_model_inserts_total[1m]))
75+
- record: job:django_model_updates_total:sum_rate1m
76+
expr: sum by(job, model) (rate(django_model_updates_total[1m]))
77+
- record: job:django_model_deletes_total:sum_rate1m
78+
expr: sum by(job, model) (rate(django_model_deletes_total[1m]))
79+
- record: job:django_db_new_connections_total:sum_rate30s
80+
expr: sum by(alias, vendor) (rate(django_db_new_connections_total[30s]))
81+
- record: job:django_db_new_connection_errors_total:sum_rate30s
82+
expr: sum by(alias, vendor) (rate(django_db_new_connection_errors_total[30s]))
83+
- record: job:django_db_execute_total:sum_rate30s
84+
expr: sum by(alias, vendor) (rate(django_db_execute_total[30s]))
85+
- record: job:django_db_execute_many_total:sum_rate30s
86+
expr: sum by(alias, vendor) (rate(django_db_execute_many_total[30s]))
87+
- record: job:django_db_errors_total:sum_rate30s
88+
expr: sum by(alias, vendor, type) (rate(django_db_errors_total[30s]))
89+
- record: job:django_migrations_applied_total:max
90+
expr: max by(job, connection) (django_migrations_applied_total)
91+
- record: job:django_migrations_unapplied_total:max
92+
expr: max by(job, connection) (django_migrations_unapplied_total)
93+
- alert: UnappliedMigrations
94+
expr: job:django_migrations_unapplied_total:max > 0
95+
for: 1m
96+
labels:
97+
severity: testing
98+
annotations:
99+
description: Django detected {{$value}} unapplied migrations on database {{$labels.connection}}
100+
summary: Unapplied django migrations on {{$labels.connection}}
101+
...

conf/prometheus.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
global:
3+
scrape_interval: 10s
4+
evaluation_interval: 10s
5+
6+
external_labels:
7+
monitor: django-monitor
8+
9+
rule_files:
10+
- django.rules.yml
11+
12+
scrape_configs:
13+
- job_name: django
14+
static_configs:
15+
- targets: ["forum:5000"]
16+
...

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ services:
4747
- websocket
4848
restart: always
4949

50+
prometheus:
51+
image: prom/prometheus
52+
volumes:
53+
- ./conf/prometheus.yml:/etc/prometheus/prometheus.yml:ro
54+
- ./conf/django.rules.yml:/etc/prometheus/django.rules.yml:ro
55+
- prometheus:/prometheus
56+
ports:
57+
- "9090:9090"
58+
59+
grafana:
60+
image: grafana/grafana
61+
volumes:
62+
- grafana-storage:/var/lib/grafana
63+
ports:
64+
- "9091:3000"
65+
5066
volumes:
5167
forum-db: {}
5268
forum-media: {}
69+
prometheus: {}
70+
grafana-storage: {}
5371
...

0 commit comments

Comments
 (0)