diff --git a/modules/django-monitoring/django_monitoring/monitoring/README.md b/modules/django-monitoring/django_monitoring/monitoring/README.md new file mode 100644 index 000000000..c96d493aa --- /dev/null +++ b/modules/django-monitoring/django_monitoring/monitoring/README.md @@ -0,0 +1,60 @@ +## Django Monitoring backend configuration and information + +## Module description + +This backend module enables automatic reporting of errors and exceptions as well as performance monitoring. The users can track and identify any errors or performance issues that occur while your application is in production. + +The following are the scope features of this module: + +- Ability to captures errors, uncaught exceptions, and unhandled rejections. +- Ability to monitors various error types based on the platform. +- Ability to provides real-time error log tracking on Sentry/Issues dashboard. + + +## Features + +- [ ] This module includes migrations. +- [x] This module includes environment variables. +- [x] This module requires manual configurations. +- [ ] This module can be configured with module options. + +## Environment variables + +```dotenv +SENTRY_DSN_URL="Your Sentry Project DSN Url" +TRACES_SAMPLE_RATE=1.0 +SEND_DEFAULT_PII="True or False" +SENTRY_DEBUG="True or False" +``` +```settings.py +from modules.django_monitoring.monitoring.sentry_configurations import * +``` + +## 3rd party setup + +For implementation of this module, the 3rd party setup is required: + +- Create a [developer account](https://sentry.io/signup/) on Sentry +- By adding your relevant information and clicking on the Create Your Account button, your account will be created. +- Choose your project framework and set up Sentry configuration to generate the DSN URL. This URL will be used in your project to receive reports of errors and exceptions, allowing you to effectively manage issues as they occur. +- Go to your project settings, then click on "Client Keys (DSN)" and copy your "SENTRY_DSN_URL" and add in your project. + +![sentry_client](https://github.com/cbshoaib/modules/assets/120275623/cecc310e-6134-450e-8d68-326de3146320) + +## Dependencies + +[Sentry](https://github.com/getsentry/sentry-python/blob/master/README.md) + +Dependencies used: + +- [sentry-sdk](https://pypi.org/project/sentry-sdk/) + +## API details + +No API details for this module. + +## Module Specifications + +Here is +the [Module Specification Document](https://docs.google.com/document/d/1BpX1jfGgt3FI0REn6vYOlyg0PUSy-YCnA3mhaoD2BQU/edit?usp=sharing), +which provides more information about the module's actual intentions. diff --git a/modules/django-monitoring/django_monitoring/monitoring/__init__.py b/modules/django-monitoring/django_monitoring/monitoring/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/django-monitoring/django_monitoring/monitoring/apps.py b/modules/django-monitoring/django_monitoring/monitoring/apps.py new file mode 100644 index 000000000..9eb19d38a --- /dev/null +++ b/modules/django-monitoring/django_monitoring/monitoring/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SentryConfig(AppConfig): + name = "modules.django_monitoring.monitoring" + verbose_name = "Monitoring" diff --git a/modules/django-monitoring/django_monitoring/monitoring/sentry_configurations.py b/modules/django-monitoring/django_monitoring/monitoring/sentry_configurations.py new file mode 100644 index 000000000..ec7da62a8 --- /dev/null +++ b/modules/django-monitoring/django_monitoring/monitoring/sentry_configurations.py @@ -0,0 +1,28 @@ +import logging +import os + +import sentry_sdk +from sentry_sdk.integrations.django import DjangoIntegration +from sentry_sdk.integrations.logging import LoggingIntegration + +sentry_sdk.init( + dsn=os.getenv("SENTRY_DSN_URL"), + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for performance monitoring. + # Recommend adjusting this value in production. + traces_sample_rate=os.getenv("TRACES_SAMPLE_RATE"), + # If you use django.contrib.auth and you've set "send_default_pii=True" so, + # user data (such as current user id, email address, username) will be attached to error events. + send_default_pii=os.getenv("SEND_DEFAULT_PII", False), + # if debug is (True) enabled SDK will attempt to print out useful debugging information, + # if something goes wrong while sending the event. + # Not recommended to turn it on in production. + debug=os.getenv("SENTRY_DEBUG", False), + integrations=[ + DjangoIntegration(), + LoggingIntegration( + level=logging.INFO, # Capture info and above as breadcrumbs + event_level=logging.WARNING # Send warnings as events + ) + ], +) diff --git a/modules/django-monitoring/django_monitoring/pyproject.toml b/modules/django-monitoring/django_monitoring/pyproject.toml new file mode 100644 index 000000000..fed528d4a --- /dev/null +++ b/modules/django-monitoring/django_monitoring/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/modules/django-monitoring/django_monitoring/setup.py b/modules/django-monitoring/django_monitoring/setup.py new file mode 100644 index 000000000..d918b2d83 --- /dev/null +++ b/modules/django-monitoring/django_monitoring/setup.py @@ -0,0 +1,41 @@ +import os +from pathlib import Path + +from setuptools import setup +from setuptools.command.build import build + +try: + base_dir = Path.cwd() + settings_file_path = None + for root, dirs, files in os.walk(base_dir.parent.parent): + if 'settings.py' in files: + settings_file_path = f"{root}/settings.py" + + import_statement = "from modules.django_monitoring.monitoring.sentry_configurations import *\n" + + if settings_file_path: + with open(settings_file_path, 'r') as file: + lines = file.readlines() + index_to_insert = lines.index("from modules.manifest import get_modules\n") + if import_statement not in lines: + lines.insert(index_to_insert, import_statement) + with open(settings_file_path, 'w') as file: + file.write(''.join(lines)) +except (Exception,): + pass + + +# Override build command +class BuildCommand(build): + def initialize_options(self): + build.initialize_options(self) + self.build_base = "/tmp" + + +setup( + name="cb_django_monitoring", + version="0.1", + packages=["monitoring"], + install_requires=["sentry-sdk[django]"], + cmdclass={"build": BuildCommand}, +) diff --git a/modules/django-monitoring/meta.json b/modules/django-monitoring/meta.json new file mode 100644 index 000000000..fa29e91d0 --- /dev/null +++ b/modules/django-monitoring/meta.json @@ -0,0 +1,5 @@ +{ + "title": "Monitoring (Django)", + "description": "Backend for the Django Monitoring (Sentry) module.", + "root": "/backend/modules" +} diff --git a/modules/django-monitoring/preview.png b/modules/django-monitoring/preview.png new file mode 100644 index 000000000..7a813441b Binary files /dev/null and b/modules/django-monitoring/preview.png differ