Skip to content

Commit

Permalink
Feat/ Support for telemetry, loguru, refactored settings (#296)
Browse files Browse the repository at this point in the history
* support for telemetry and logging

* fixed incorrect middleware name

* added docstrings and setup otel for loguru

* sending logs to otel

* init

* django settings module variable set in post_fork

* trace export error fixed

* refactor: base settings

* add: OTEL_RESOURCE_ATTRIBUTES env var

* zango release workflow added (#301)

* zango release workflow added

* branch updated, workflow name corrected

* version and changelog checks added

* image link updated

* feat: added frontend build generation workflow (#316)

* autoincrement filter added

* filter renamed

* filter renamed

* feat: added frontend build generation workflow

* fix: api token added, LICENSE file removed

---------

Co-authored-by: deepakdinesh1123 <[email protected]>
Co-authored-by: Harsh Shah <[email protected]>

* Deploy frontend build

* add: contribution doc and fix use_latest template tag (#293)

* add: contribution doc and fix use_latest template tag fix

* chore: add doc_string for use_latest filter

---------

Co-authored-by: Harsh Shah <[email protected]>

* added version for dependencies (#320)

* added version for dependencies

* Update base.txt

django-rest-knox version fixed

* auditlogs now gets default timezone of host if timezone is not specified (#322)

* auditlogs now gets default timezone of host if timezone is not specified

* tzlocal removed

* remove unused import

---------

Co-authored-by: Harsh Shah <[email protected]>

* support for telemetry and logging

* otel resource attributes variable removed

* python-dotenv removed

* fix: shift handler and logger inside setup_setting
for avoiding error on existing project

---------

Co-authored-by: kc-diabeat <[email protected]>
Co-authored-by: deepakdinesh1123 <[email protected]>
Co-authored-by: Harsh Shah <[email protected]>
Co-authored-by: deepakdinesh1123 <[email protected]>
Co-authored-by: Ravi Patel <[email protected]>
Co-authored-by: Harsh Shah <[email protected]>
  • Loading branch information
7 people authored Aug 2, 2024
1 parent 046b5b9 commit ad3428a
Show file tree
Hide file tree
Showing 16 changed files with 715 additions and 137 deletions.
30 changes: 30 additions & 0 deletions backend/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,33 @@ django-ipware==7.0.1
django-decorator-include==3.0
setuptools==70.1.1
django-axes==6.4.0
opentelemetry-api==1.22.0
opentelemetry-distro==0.43b0
opentelemetry-exporter-otlp==1.22.0
opentelemetry-exporter-otlp-proto-common==1.22.0
opentelemetry-exporter-otlp-proto-grpc==1.22.0
opentelemetry-exporter-otlp-proto-http==1.22.0
opentelemetry-instrumentation==0.43b0
opentelemetry-instrumentation-asgi==0.43b0
opentelemetry-instrumentation-aws-lambda==0.43b0
opentelemetry-instrumentation-boto3sqs==0.43b0
opentelemetry-instrumentation-botocore==0.43b0
opentelemetry-instrumentation-celery==0.43b0
opentelemetry-instrumentation-dbapi==0.43b0
opentelemetry-instrumentation-django==0.43b0
opentelemetry-instrumentation-grpc==0.43b0
opentelemetry-instrumentation-jinja2==0.43b0
opentelemetry-instrumentation-logging==0.43b0
opentelemetry-instrumentation-psycopg2==0.43b0
opentelemetry-instrumentation-redis==0.43b0
opentelemetry-instrumentation-requests==0.43b0
opentelemetry-instrumentation-sqlite3==0.43b0
opentelemetry-instrumentation-urllib==0.43b0
opentelemetry-instrumentation-urllib3==0.43b0
opentelemetry-instrumentation-wsgi==0.43b0
opentelemetry-propagator-aws-xray==1.0.1
opentelemetry-proto==1.22.0
opentelemetry-sdk==1.22.0
opentelemetry-semantic-conventions==0.43b0
opentelemetry-util-http==0.43b0
loguru==0.7.2
11 changes: 11 additions & 0 deletions backend/src/zango/cli/project_template/project_name/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

from django.core.asgi import get_asgi_application

from zango.core.monitoring import setup_telemetry

# The telemetry instrumentation library setup needs to run prior to django's setup.
setup_telemetry(add_django_instrumentation=True)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{project_name}}.settings")

application = get_asgi_application()

# Our custom loguru logging to should be setup after django has been setup as Django
# will try to override with its own logging setup.
from zango.core.monitoring import setup_logging

setup_logging()
144 changes: 19 additions & 125 deletions backend/src/zango/cli/project_template/project_name/settings.py
Original file line number Diff line number Diff line change
@@ -1,151 +1,45 @@
import os
import environ
from pathlib import Path
from datetime import timedelta

from zango.config.settings.base import *

BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env(
DEBUG=(bool, True),
REDIS_HOST=(str, "127.0.0.1"),
REDIS_PORT=(str, "6379"),
SESSION_SECURITY_WARN_AFTER=(int, 1700),
SESSION_SECURITY_EXPIRE_AFTER=(int, 1800),
INTERNAL_IPS=(list, []),
ALLOWED_HOSTS=(list, ["*"]),
CORS_ORIGIN_WHITELIST=(list, ["http://localhost:1443", "http://localhost:8000"]),
CSRF_TRUSTED_ORIGINS=(list, ["http://localhost:1443", "http://localhost:8000"]),
AXES_BEHIND_REVERSE_PROXY = (bool, False),
AXES_FAILURE_LIMIT = (int, 6),
AXES_LOCK_OUT_AT_FAILURE = (bool, True),
AXES_COOLOFF_TIME = (int, 900),
)
environ.Env.read_env(os.path.join(BASE_DIR.parent, ".env"))

SECRET_KEY = "{{secret_key}}"
class AttrDict(dict):
"""
A dictionary subclass for managing global settings with attribute-style access.
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
This class allows getting and setting items in the global namespace
using both attribute and item notation.
"""

ALLOWED_HOSTS = env("ALLOWED_HOSTS")
def __getattr__(self, item):
return globals()[item]

PROJECT_NAME = env("PROJECT_NAME")
def __setattr__(self, item, value):
globals()[item] = value

WSGI_APPLICATION = f"{PROJECT_NAME}.wsgi.application"
def __setitem__(self, key, value):
globals()[key] = value


DATABASES = {
"default": {
"ENGINE": "django_tenants.postgresql_backend",
"NAME": env("POSTGRES_DB"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
"HOST": env("POSTGRES_HOST"),
"PORT": env("POSTGRES_PORT"),
"ATOMIC_REQUESTS": True,
}
}
# Call setup_settings to initialize the settings
settings_result = setup_settings(AttrDict(vars()), BASE_DIR)

REDIS_HOST = env("REDIS_HOST")
REDIS_PORT = env("REDIS_PORT")
REDIS_PROTOCOL = "redis"
# Setting Overrides
# Any settings that need to be overridden or added should be done below this line
# to ensure they take effect after the initial setup

REDIS_URL = f"{REDIS_PROTOCOL}://{REDIS_HOST}:{REDIS_PORT}/1"
CELERY_BROKER_URL = REDIS_URL
SECRET_KEY = "{{secret_key}}" # Shift this to .env

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL, # Using DB 1 for cache
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
"TIMEOUT": 300, # Default timeout is 5 minutes, but adjust as needed
}
}

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = env(
"CORS_ORIGIN_WHITELIST"
) # Change according to domain configured
CSRF_TRUSTED_ORIGINS = env(
"CSRF_TRUSTED_ORIGINS"
) # Change according to domain configured

# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# ROOT_URLCONF = '{{project_name}}.urls'

import os

TEMPLATES[0]["DIRS"] = [os.path.join(BASE_DIR, "templates")]

SHOW_PUBLIC_IF_NO_TENANT_FOUND = False
PUBLIC_SCHEMA_URLCONF = "zango.config.urls_public"
ROOT_URLCONF = f"{PROJECT_NAME}.urls_tenants"

ENV = "dev"

PHONENUMBER_DEFAULT_REGION = "IN"

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"
AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"
AWS_S3_REGION_NAME = "AWS_S3_REGION_NAME"

STORAGES = {
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
}

# To change the media storage to S3 you can use the BACKEND class provided by the default storage
# To change the static storage to S3 you can use the BACKEND class provided by the staticfiles storage
# STORAGES = {
# "default": {"BACKEND": "zango.core.storage_utils.S3MediaStorage"},
# "staticfiles": {"BACKEND": "zango.core.storage_utils.S3StaticStorage"},
# }
#
AWS_MEDIA_STORAGE_BUCKET_NAME = "media" # S3 Bucket Name
AWS_MEDIA_STORAGE_LOCATION = "media" # Prefix added to all the files uploaded
AWS_STATIC_STORAGE_BUCKET_NAME = "static" # S3 Bucket Name
AWS_STATIC_STORAGE_LOCATION = "static" # Prefix added to all the files uploaded

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "static/"
STATICFILES_DIRS += [os.path.join(BASE_DIR, "assets")]

# Session Security
SESSION_SECURITY_WARN_AFTER = env("SESSION_SECURITY_WARN_AFTER")
SESSION_SECURITY_EXPIRE_AFTER = env("SESSION_SECURITY_EXPIRE_AFTER")

if DEBUG or ENV == "dev":
# Disable secure cookies in development or debugging environments
# to simplify troubleshooting and testing.
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False


# INTERNAL_IPS can contain a list of IP addresses or CIDR blocks that are considered internal.
# Both individual IP addresses and CIDR notation (e.g., '192.168.1.1' or '192.168.1.0/24') can be provided.
INTERNAL_IPS = env("INTERNAL_IPS")


AXES_BEHIND_REVERSE_PROXY = env("AXES_BEHIND_REVERSE_PROXY")
AXES_FAILURE_LIMIT = env("AXES_FAILURE_LIMIT")
AXES_LOCK_OUT_AT_FAILURE = env("AXES_LOCK_OUT_AT_FAILURE")
AXES_COOLOFF_TIME = timedelta(seconds=env("AXES_COOLOFF_TIME"))
13 changes: 12 additions & 1 deletion backend/src/zango/cli/project_template/project_name/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{project_name}}.settings")


from django.core.wsgi import get_wsgi_application
from zango.core.monitoring import setup_telemetry

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{project_name}}.settings")
# The telemetry instrumentation library setup needs to run prior to django's setup.
setup_telemetry(add_django_instrumentation=True)

application = get_wsgi_application()

# Our custom loguru logging to should be setup after django has been setup as Django
# will try to override with its own logging setup.
from zango.core.monitoring import setup_logging

setup_logging()
Loading

0 comments on commit ad3428a

Please sign in to comment.