Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ DEBUG=1
APPLY_MIGRATIONS=1
CREATE_SUPERUSER=1
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0
DATABASE_URL=postgis://geo-search:geo-search@localhost:5434/geo-search
DATABASE_URL=postgis://geo-search:geo-search@geo-search-db/geo-search
SENTRY_DSN=https://abcdefg@your.sentry.here/999
SENTRY_ENVIRONMENT=local
SENTRY_TRACES_SAMPLE_RATE=0.1
SENTRY_TRACES_IGNORE_PATHS=/healthz,/readiness
10 changes: 4 additions & 6 deletions compose.yml → compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
services:

postgis:
image: postgis/postgis:17-3.5
platform: linux/x86_64
platform: linux/amd64
restart: on-failure
environment:
POSTGRES_USER: geo-search
Expand All @@ -12,22 +13,19 @@ services:
volumes:
- pgdata:/var/lib/postgresql/data
container_name: geo-search-db

django:
restart: unless-stopped
build:
context: .
dockerfile: ./Dockerfile
target: development
env_file:
- .env
environment:
DEV_SERVER: 1
DATABASE_URL: postgis://geo-search:geo-search@geo-search-db/geo-search
DATABASE_HOST: geo-search-db.helsinki
volumes:
- .:/app
ports:
- "8080:8080"
restart: unless-stopped
depends_on:
- postgis
container_name: geo-search-django
Expand Down
56 changes: 40 additions & 16 deletions geo_search/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import subprocess
from pathlib import Path

import sentry_sdk
from corsheaders.defaults import default_headers
from django.utils.log import DEFAULT_LOGGING
from django.utils.translation import gettext_lazy as _
from environ import Env
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.types import SamplingContext

GDAL_LIBRARY_PATH = os.environ.get("GDAL_LIBRARY_PATH")
GEOS_LIBRARY_PATH = os.environ.get("GEOS_LIBRARY_PATH")
Expand All @@ -32,7 +33,11 @@
),
DATABASE_PASSWORD=(str, ""),
SENTRY_DSN=(str, ""),
SENTRY_ENVIRONMENT=(str, ""),
SENTRY_ENVIRONMENT=(str, "local"),
SENTRY_PROFILE_SESSION_SAMPLE_RATE=(float, None),
SENTRY_RELEASE=(str, None),
SENTRY_TRACES_SAMPLE_RATE=(float, None),
SENTRY_TRACES_IGNORE_PATHS=(list, ["/healthz", "/readiness"]),
REQUIRE_AUTHORIZATION=(bool, True),
DJANGO_LOG_LEVEL=(str, "INFO"),
)
Expand All @@ -47,21 +52,34 @@
SECRET_KEY = "secret-for-debugging-only"
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS")

try:
version = str(
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip()
SENTRY_TRACES_SAMPLE_RATE = env("SENTRY_TRACES_SAMPLE_RATE")
SENTRY_TRACES_IGNORE_PATHS = env.list("SENTRY_TRACES_IGNORE_PATHS")


def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
# Respect parent sampling decision if one exists. Recommended by Sentry.
if (parent_sampled := sampling_context.get("parent_sampled")) is not None:
return float(parent_sampled)

# Exclude health check endpoints from tracing
path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO", "")
if path.rstrip("/") in SENTRY_TRACES_IGNORE_PATHS:
return 0

# Use configured sample rate for all other requests
return SENTRY_TRACES_SAMPLE_RATE or 0


if env("SENTRY_DSN"):
sentry_sdk.init(
dsn=env("SENTRY_DSN"),
environment=env("SENTRY_ENVIRONMENT"),
release=env("SENTRY_RELEASE"),
integrations=[DjangoIntegration()],
traces_sampler=sentry_traces_sampler,
profile_session_sample_rate=env("SENTRY_PROFILE_SESSION_SAMPLE_RATE"),
profile_lifecycle="trace",
)
except OSError:
version = "n/a"

sentry_sdk.init(
dsn=env.str("SENTRY_DSN"),
release=version,
environment=env("SENTRY_ENVIRONMENT"),
traces_sample_rate=1.0,
send_default_pii=True,
integrations=[DjangoIntegration()],
)

# Application definition
INSTALLED_APPS = [
Expand Down Expand Up @@ -197,3 +215,9 @@
USE_X_FORWARDED_HOST = True

CORS_ALLOW_ALL_ORIGINS = True

CORS_ALLOW_HEADERS = (
*default_headers,
"baggage",
"sentry-trace",
)
Loading