GitLab Commit is coming up on August 3-4. Learn how to innovate together using GitLab, the DevOps platform. Register for free: gitlabcommitvirtual2021.com

Verified Commit 7a283adb authored by Markus Holtermann's avatar Markus Holtermann 🐍
Browse files

Initial setup

parents
Copyright 2019, Markus Holtermann
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# django-structlog
This project demonstrates how one can use structured logging in Django.
## Installation
This project requires [docker-compose](https://docs.docker.com/compose/). When ``docker-compose`` is installed, one can spin up the entire project with this command:
```shell
$ docker-compose up --detach
```
This will set up these services:
- A NGINX reverse proxy to serve all web requests
- A PostgreSQL database as a database used by the Django application
- A Django application
- A Fluentd deamon to fetch structured logs from the Django application and push them into CrateDB (TODO)
- A Grafana dashboard to visualize the logs (TODO)
- A CrateDB cluster as long-term log storage (TODO)
To finish the project setup, one needs to create a Django user to play around with:
```shell
$ docker-compose run app python manage.py createsuperuser
```
FROM python:3.7-alpine
WORKDIR /app
EXPOSE 8000
CMD [ "/app/entrypoint.sh" ]
COPY requirements.txt /app/requirements.txt
RUN apk update && \
apk add postgresql-libs && \
apk add --virtual .build-deps gcc musl-dev postgresql-dev && \
pip install -r requirements.txt --no-cache-dir && \
apk --purge del .build-deps
COPY . /app
#!/bin/sh
python manage.py migrate
gunicorn --bind 0.0.0.0:8000 project.wsgi:application
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
"""
Django settings for project project.
Generated by 'django-admin startproject' using Django 2.1.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ["SECRET_KEY"]
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "project.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
]
},
}
]
WSGI_APPLICATION = "project.wsgi.application"
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {"default": dj_database_url.config()}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = "/static/"
"""project URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse
from django.urls import path
urlpatterns = [path("admin/", admin.site.urls)]
"""
WSGI config for project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
dj-database-url==0.5.0
Django==2.1.5
gunicorn==19.9.0
psycopg2==2.7.6.1
pytz==2018.9
whitenoise==4.1.2
version: "3"
services:
# fluentd:
nginx:
image: nginx:1.15-alpine
depends_on:
- app
- grafana
ports:
- "8000:8000"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
postgres:
image: postgres:11.1-alpine
volumes:
- ./postgres:/var/lib/postgres/data
environment:
- POSTGRES_PASSWORD=postgres
cratedb:
image: crate:3.1
volumes:
- ./cratedb:/data
app:
build:
context: ./app
image: markush/django-structlog
depends_on:
- postgres
environment:
- SECRET_KEY=demo-s!cret-k3y
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
grafana:
image: grafana/grafana
user: "0" # To allow Grafana to boot up
depends_on:
- cratedb
- postgres
volumes:
- ./grafana:/var/lib/grafana
environment:
- GF_SERVER_ROOT_URL=http://localhost:8000/grafana/
- GF_SERVER_STATIC_ROOT_PATH=/usr/share/grafana/public
- GF_DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 8000;
location /grafana/ {
proxy_pass http://grafana:3000/;
proxy_set_header Host $host:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_buffers 32 8k;
proxy_buffer_size 64k;
}
location / {
proxy_pass http://app:8000;
proxy_set_header Host $host:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_buffers 32 8k;
proxy_buffer_size 64k;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment