|
15 | 15 | from pathlib import Path
|
16 | 16 |
|
17 | 17 | import dj_database_url
|
| 18 | +import requests |
18 | 19 | import sentry_sdk
|
| 20 | +from dbt_copilot_python.database import database_url_from_env |
| 21 | +from dbt_copilot_python.network import setup_allowed_hosts |
| 22 | +from dbt_copilot_python.utility import is_copilot |
| 23 | +from django_log_formatter_asim import ASIMFormatter |
19 | 24 | from django_log_formatter_ecs import ECSFormatter
|
20 | 25 | from environ import Env
|
21 | 26 | from sentry_sdk.integrations.django import DjangoIntegration
|
|
45 | 50 | DEBUG = env("DEBUG")
|
46 | 51 |
|
47 | 52 | ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])
|
48 |
| -# Application definition |
| 53 | +ALLOWED_HOSTS = setup_allowed_hosts(ALLOWED_HOSTS) |
49 | 54 |
|
50 |
| -ELASTIC_APM_ENABLED = env("ELASTIC_APM_ENABLED", default=not DEBUG) |
| 55 | +# Application definition |
| 56 | +ELASTIC_APM_ENABLED = env.bool("ELASTIC_APM_ENABLED", default=not DEBUG) |
51 | 57 |
|
52 | 58 | PRIORITISATION_STRATEGIC_ASSESSMENTS = env.bool(
|
53 | 59 | "PRIORITISATION_STRATEGIC_ASSESSMENTS", default=False
|
|
91 | 97 | "healthcheck",
|
92 | 98 | "reports",
|
93 | 99 | "users",
|
| 100 | + "pingdom", |
94 | 101 | ]
|
95 | 102 |
|
96 | 103 | INSTALLED_APPS = BASE_APPS + DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
|
|
148 | 155 | # Database
|
149 | 156 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases
|
150 | 157 |
|
151 |
| -DATABASES = { |
152 |
| - "default": dj_database_url.config( |
153 |
| - env="DATABASE_URL", conn_max_age=0, conn_health_checks=True, default="" |
154 |
| - ) |
155 |
| -} |
| 158 | +if is_copilot(): |
| 159 | + DATABASES = { |
| 160 | + "default": dj_database_url.config( |
| 161 | + default=database_url_from_env("DATABASE_ENV_VAR_KEY"), |
| 162 | + conn_max_age=0, |
| 163 | + conn_health_checks=True, |
| 164 | + ) |
| 165 | + } |
| 166 | +else: |
| 167 | + DATABASES = {"default": env.db("DATABASE_URL")} |
156 | 168 |
|
157 | 169 |
|
158 | 170 | # Password validation
|
|
196 | 208 | STATICFILES_DIRS = [
|
197 | 209 | str(ROOT_DIR / "core/frontend/dist/"),
|
198 | 210 | ]
|
199 |
| -STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage" |
| 211 | + |
200 | 212 | STATICFILES_FINDERS = (
|
201 | 213 | "django.contrib.staticfiles.finders.FileSystemFinder",
|
202 | 214 | "django.contrib.staticfiles.finders.AppDirectoriesFinder",
|
203 | 215 | )
|
204 | 216 |
|
| 217 | +STORAGES = { |
| 218 | + "staticfiles": { |
| 219 | + "BACKEND": env.str( |
| 220 | + "STATICFILES_STORAGE", "whitenoise.storage.CompressedStaticFilesStorage" |
| 221 | + ), |
| 222 | + }, |
| 223 | +} |
205 | 224 |
|
206 | 225 | USER_DATA_CACHE_TIME = 3600
|
207 | 226 | METADATA_CACHE_TIME = "10600"
|
|
271 | 290 | "version": 1,
|
272 | 291 | "disable_existing_loggers": False,
|
273 | 292 | "formatters": {
|
274 |
| - "ecs_formatter": { |
275 |
| - "()": ECSFormatter, |
276 |
| - }, |
| 293 | + "asim_formatter": {"()": ASIMFormatter}, |
| 294 | + "ecs_formatter": {"()": ECSFormatter}, |
277 | 295 | "simple": {
|
278 | 296 | "format": "{asctime} {levelname} {message}",
|
279 | 297 | "style": "{",
|
280 | 298 | },
|
281 | 299 | },
|
282 | 300 | "handlers": {
|
| 301 | + "asim": { |
| 302 | + "class": "logging.StreamHandler", |
| 303 | + "stream": sys.stdout, # noqa F405 |
| 304 | + "formatter": "asim_formatter", |
| 305 | + }, |
283 | 306 | "ecs": {
|
284 | 307 | "class": "logging.StreamHandler",
|
285 | 308 | "stream": sys.stdout, # noqa F405
|
|
292 | 315 | },
|
293 | 316 | },
|
294 | 317 | "root": {
|
295 |
| - "handlers": [ |
296 |
| - "ecs", |
297 |
| - "stdout", |
298 |
| - ], |
| 318 | + "handlers": ["asim", "ecs", "stdout"], |
299 | 319 | "level": os.getenv("ROOT_LOG_LEVEL", "INFO"), # noqa F405
|
300 | 320 | },
|
301 | 321 | "loggers": {
|
302 |
| - "": { |
303 |
| - "handlers": [ |
304 |
| - "ecs", |
305 |
| - "stdout", |
306 |
| - ], |
307 |
| - "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"), # noqa F405 |
308 |
| - "propagate": False, |
309 |
| - }, |
310 | 322 | "django": {
|
311 |
| - "handlers": [ |
312 |
| - "ecs", |
313 |
| - "stdout", |
314 |
| - ], |
| 323 | + "handlers": ["asim", "ecs", "stdout"], |
315 | 324 | "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"), # noqa F405
|
316 | 325 | "propagate": False,
|
317 | 326 | },
|
318 | 327 | "django.server": {
|
319 |
| - "handlers": [ |
320 |
| - "ecs", |
321 |
| - "stdout", |
322 |
| - ], |
| 328 | + "handlers": ["asim", "ecs", "stdout"], |
323 | 329 | "level": os.getenv("DJANGO_SERVER_LOG_LEVEL", "ERROR"), # noqa F405
|
324 | 330 | "propagate": False,
|
325 | 331 | },
|
326 | 332 | "django.db.backends": {
|
327 |
| - "handlers": [ |
328 |
| - "ecs", |
329 |
| - "stdout", |
330 |
| - ], |
| 333 | + "handlers": ["asim", "ecs", "stdout"], |
331 | 334 | "level": os.getenv("DJANGO_DB_LOG_LEVEL", "ERROR"), # noqa F405
|
332 | 335 | "propagate": False,
|
333 | 336 | },
|
334 | 337 | },
|
335 | 338 | }
|
| 339 | + |
| 340 | + |
| 341 | +# Django Log Formatter ASIM settings |
| 342 | +if is_copilot(): |
| 343 | + DLFA_TRACE_HEADERS = ("X-B3-TraceId", "X-B3-SpanId") |
| 344 | + |
| 345 | + |
336 | 346 | DLFE_APP_NAME = True
|
337 | 347 | DLFE_LOG_SENSITIVE_USER_DATA = True
|
338 | 348 |
|
|
343 | 353 |
|
344 | 354 | # Sentry
|
345 | 355 | SENTRY_BROWSER_TRACES_SAMPLE_RATE = env.float("SENTRY_BROWSER_TRACES_SAMPLE_RATE", 0.0)
|
346 |
| -if not DEBUG: |
| 356 | +SENTRY_DNS = env("SENTRY_DSN", default=None) |
| 357 | + |
| 358 | +if SENTRY_DNS: |
347 | 359 | sentry_sdk.init(
|
348 |
| - dsn=env("SENTRY_DSN"), |
| 360 | + dsn=SENTRY_DNS, |
349 | 361 | environment=env("SENTRY_ENVIRONMENT"),
|
350 | 362 | integrations=[
|
351 | 363 | DjangoIntegration(),
|
|
0 commit comments