forked from grafana/pyroscope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add Django Example to Python Examples Folder (grafana#472)
* Add django example
- Loading branch information
1 parent
4cd06a2
commit 0dab599
Showing
39 changed files
with
654 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DEBUG=1 | ||
SECRET_KEY=foo | ||
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] web | ||
SQL_ENGINE=django.db.backends.postgresql | ||
SQL_DATABASE=hello_django_dev | ||
SQL_USER=hello_django | ||
SQL_PASSWORD=hello_django | ||
SQL_HOST=db | ||
SQL_PORT=5432 | ||
DATABASE=postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DEBUG=1 | ||
SECRET_KEY=foo | ||
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] web | ||
SQL_ENGINE=django.db.backends.postgresql | ||
SQL_DATABASE=hello_django_dev | ||
SQL_USER=hello_django | ||
SQL_PASSWORD=hello_django | ||
SQL_HOST=db | ||
SQL_PORT=5432 | ||
DATABASE=postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.pyc | ||
__pycache | ||
.DS_Store | ||
.env.prod | ||
.env.prod.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM python:3.9 | ||
|
||
RUN pip3 install requests | ||
|
||
COPY load-generator.py ./load-generator.py | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
CMD [ "python", "load-generator.py" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Michael Herman | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Dockerizing Django with Pyroscope, Postgres, Gunicorn, and Nginx | ||
This is a simple rideshare example that adds Pyroscope to a Django application and uses it to profile various routes | ||
|
||
### Development | ||
Uses the default Django development server. | ||
|
||
1. Rename *.env.dev-sample* to *.env.dev*. | ||
1. Update the environment variables in the *docker-compose.yml* and *.env.dev* files. | ||
1. Build the images and run the containers: | ||
|
||
```sh | ||
$ docker-compose up -d --build | ||
``` | ||
|
||
Test it out at [http://localhost:8000](http://localhost:8000). The "app" folder is mounted into the container and your code changes apply automatically. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# pull official base image | ||
FROM python:3.9 | ||
|
||
# set work directory | ||
WORKDIR /usr/src/app | ||
|
||
# set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
# install psycopg2 dependencies | ||
# RUN apk update \ | ||
# && apk add libffi-dev postgresql-dev gcc python3-dev musl-dev | ||
|
||
RUN apt-get update -y | ||
RUN apt-get install -y postgresql postgresql-client netcat gcc python3-dev musl-dev | ||
|
||
|
||
# install dependencies | ||
RUN pip install --upgrade pip | ||
COPY ./requirements.txt . | ||
RUN pip install -r requirements.txt | ||
|
||
# copy entrypoint.sh | ||
COPY ./entrypoint.sh . | ||
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh | ||
RUN chmod +x /usr/src/app/entrypoint.sh | ||
|
||
# copy project | ||
COPY . . | ||
|
||
# run entrypoint.sh | ||
ENTRYPOINT ["/usr/src/app/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
########### | ||
# BUILDER # | ||
########### | ||
|
||
# pull official base image | ||
FROM python:3.9.6-alpine as builder | ||
|
||
# set work directory | ||
WORKDIR /usr/src/app | ||
|
||
# set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
# install psycopg2 dependencies | ||
RUN apk update \ | ||
&& apk add postgresql-dev gcc python3-dev musl-dev | ||
|
||
# lint | ||
RUN pip install --upgrade pip | ||
RUN pip install flake8==3.9.2 | ||
COPY . . | ||
RUN flake8 --ignore=E501,F401 . | ||
|
||
# install dependencies | ||
COPY ./requirements.txt . | ||
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt | ||
|
||
|
||
######### | ||
# FINAL # | ||
######### | ||
|
||
# pull official base image | ||
FROM python:3.9.6-alpine | ||
|
||
# create directory for the app user | ||
RUN mkdir -p /home/app | ||
|
||
# create the app user | ||
RUN addgroup -S app && adduser -S app -G app | ||
|
||
# create the appropriate directories | ||
ENV HOME=/home/app | ||
ENV APP_HOME=/home/app/web | ||
RUN mkdir $APP_HOME | ||
RUN mkdir $APP_HOME/staticfiles | ||
RUN mkdir $APP_HOME/mediafiles | ||
WORKDIR $APP_HOME | ||
|
||
# install dependencies | ||
RUN apk update && apk add libpq | ||
COPY --from=builder /usr/src/app/wheels /wheels | ||
COPY --from=builder /usr/src/app/requirements.txt . | ||
RUN pip install --no-cache /wheels/* | ||
|
||
# copy entrypoint.prod.sh | ||
COPY ./entrypoint.prod.sh . | ||
RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh | ||
RUN chmod +x $APP_HOME/entrypoint.prod.sh | ||
|
||
# copy project | ||
COPY . $APP_HOME | ||
|
||
# chown all the files to the app user | ||
RUN chown -R app:app $APP_HOME | ||
|
||
# change to the app user | ||
USER app | ||
|
||
# run entrypoint.prod.sh | ||
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
|
||
if [ "$DATABASE" = "postgres" ] | ||
then | ||
echo "Waiting for postgres..." | ||
|
||
while ! nc -z $SQL_HOST $SQL_PORT; do | ||
sleep 0.1 | ||
done | ||
|
||
echo "PostgreSQL started" | ||
fi | ||
|
||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/sh | ||
|
||
if [ "$DATABASE" = "postgres" ] | ||
then | ||
echo "Waiting for postgres..." | ||
|
||
while ! nc -z $SQL_HOST $SQL_PORT; do | ||
sleep 0.1 | ||
done | ||
|
||
echo "PostgreSQL started" | ||
fi | ||
|
||
python manage.py flush --no-input | ||
python manage.py migrate | ||
|
||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
ASGI config for hello_django project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_django.settings') | ||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
""" | ||
Django settings for hello_django project. | ||
Generated by 'django-admin startproject' using Django 3.2.6. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/3.2/ref/settings/ | ||
""" | ||
|
||
import os | ||
import pyroscope | ||
from pathlib import Path | ||
|
||
|
||
# Build paths inside the project like this: BASE_DIR / 'subdir'. | ||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ | ||
|
||
SECRET_KEY = os.environ.get("SECRET_KEY") | ||
|
||
DEBUG = int(os.environ.get("DEBUG", default=0)) | ||
|
||
# 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each. | ||
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]' | ||
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ") | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
"django.contrib.admin", | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"django.contrib.sessions", | ||
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"rest_framework", | ||
"ride_share", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'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 = 'hello_django.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 = 'hello_django.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"), | ||
"NAME": os.environ.get("SQL_DATABASE", BASE_DIR / "db.sqlite3"), | ||
"USER": os.environ.get("SQL_USER", "user"), | ||
"PASSWORD": os.environ.get("SQL_PASSWORD", "password"), | ||
"HOST": os.environ.get("SQL_HOST", "localhost"), | ||
"PORT": os.environ.get("SQL_PORT", "5432"), | ||
} | ||
} | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/ | ||
|
||
STATIC_URL = "/static/" | ||
STATIC_ROOT = BASE_DIR / "staticfiles" | ||
|
||
MEDIA_URL = "/media/" | ||
MEDIA_ROOT = BASE_DIR / "mediafiles" | ||
|
||
|
||
# Default primary key field type | ||
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field | ||
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | ||
|
||
pyroscope.configure( | ||
app_name = "django-ride-sharing-app", | ||
server_address = "http://pyroscope:4040", | ||
# tags = { | ||
# "region": f'{os.getenv("REGION")}', | ||
# } | ||
) |
Oops, something went wrong.