Skip to content
Snippets Groups Projects
Commit 3d8f685c authored by Jan David Mol's avatar Jan David Mol
Browse files

Add example django app with models tracked by metrics

parent 1cb12529
No related branches found
No related tags found
No related merge requests found
Pipeline #60496 skipped
......@@ -2,13 +2,6 @@
[![Demo](https://img.shields.io/badge/demo-dashboards-blue)](http://real-astron.nl/grafana)
TODO:
* Fix django app (main URL 404s)
* (fixed) Metrics not exported to prometheus
* Provision dashboard found at https://grafana.com/grafana/dashboards/9528-django-prometheus/
* Make all metrics work in dashboard
## Welcome
Welcome to the Grafana Deploy! This repository lets you set up your grafana
instance in a few seconds.
......
......@@ -31,6 +31,8 @@ ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
"polls.apps.PollsConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
......@@ -38,6 +40,8 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"django_prometheus",
]
......@@ -127,3 +131,11 @@ STATIC_URL = "static/"
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# DRF
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny'
]
}
......@@ -16,9 +16,14 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.models import User
urlpatterns = [
path("admin/", admin.site.urls),
path("polls/", include("polls.urls")),
path('api-auth/', include('rest_framework.urls')),
path('', include('django_prometheus.urls')),
]
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class PollsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "polls"
from django.db import models
from django_prometheus.models import ExportModelOperationsMixin
class Question(ExportModelOperationsMixin('question'), models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField("date published")
class Choice(ExportModelOperationsMixin('choice'), models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
from django.test import TestCase
# Create your tests here.
from django.urls import path, include
from . import views
from rest_framework import routers
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'question', views.QuestionViewSet)
router.register(r'choice', views.ChoiceViewSet)
urlpatterns = [
path('', include(router.urls)),
]
from django.http import HttpResponse
from rest_framework import serializers, viewsets
from .models import Question, Choice
# Serializers define the API representation.
class QuestionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Question
fields = '__all__'
class ChoiceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Choice
fields = '__all__'
# ViewSets define the view behavior.
class QuestionViewSet(viewsets.ModelViewSet):
queryset = Question.objects.all()
serializer_class = QuestionSerializer
class ChoiceViewSet(viewsets.ModelViewSet):
queryset = Choice.objects.all()
serializer_class = ChoiceSerializer
# Django
django
tzdata
# DRF
djangorestframework
markdown
django-filter
# Metrics
django-prometheus
......@@ -69,11 +69,11 @@ services:
container_name: django
ports:
- "8000:8000"
entrypoint:
- python3
- /src/mysite/manage.py
- runserver
- 0.0.0.0:8000
working_dir: /src/mysite
command: >
sh -c "python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000"
networks:
- monitoring
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment