Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules - Django Monitoring #1018

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions modules/django-monitoring/django_monitoring/monitoring/README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SentryConfig(AppConfig):
name = "modules.django_monitoring.monitoring"
verbose_name = "Monitoring"
Original file line number Diff line number Diff line change
@@ -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
)
],
)
3 changes: 3 additions & 0 deletions modules/django-monitoring/django_monitoring/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
41 changes: 41 additions & 0 deletions modules/django-monitoring/django_monitoring/setup.py
Original file line number Diff line number Diff line change
@@ -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},
)
5 changes: 5 additions & 0 deletions modules/django-monitoring/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Monitoring (Django)",
"description": "Backend for the Django Monitoring (Sentry) module.",
"root": "/backend/modules"
}
Binary file added modules/django-monitoring/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading