diff --git a/jandog/settings.py b/jandog/settings.py index cecbf5a..e91f2d6 100644 --- a/jandog/settings.py +++ b/jandog/settings.py @@ -9,8 +9,28 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ - +import os from pathlib import Path +from dotenv import load_dotenv +from ddtrace import patch_all, Pin, patch, tracer + +load_dotenv() + + +# Below to custom writer to log traces to the console for easier debugging +# but you can use any writer you want after verify writing to console works. +# tracer.configure(writer=ConsoleWriter()) + +# if you are not using ddtrace-run you should use patch_all below. +# patch_all() + +tracer.configure( + settings={ + 'FILTERS': [], + 'DD_TAGS': 'team:debugging' + } +) + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -28,8 +48,6 @@ ALLOWED_HOSTS = [] - - # Application definition INSTALLED_APPS = [ @@ -44,7 +62,6 @@ ] MIDDLEWARE = [ - 'moesifdjango.middleware.moesif_middleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', @@ -52,6 +69,7 @@ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'moesifdjango.middleware.moesif_middleware', ] ROOT_URLCONF = 'jandog.urls' @@ -128,24 +146,56 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' MOESIF_MIDDLEWARE = { - # Your Moesif Application ID - 'APPLICATION_ID': 'Your Application ID', - - # Optional: Enable debug mode to log requests locally - 'DEBUG': True, - - # Optional: Function to skip logging certain requests - 'SKIP': lambda request: request.path.startswith('/admin'), - - # Optional: Function to mask sensitive data in requests - 'MASK_EVENT_MODEL': lambda event_model: event_model, - - # Optional: Function to identify users - 'IDENTIFY_USER': lambda request, response: request.user.id if request.user.is_authenticated else None, + "APPLICATION_ID": os.environ.get("MOESIF_APPLICATION_ID"), + "SKIP": lambda request, response: not request.path.startswith("/api/"), + "SKIP_OUTGOING": lambda request, response: True, + "IDENTIFY_USER": lambda request, response: str(request.user.id), + "IDENTIFY_COMPANY": lambda request, response: str(request.user.primary_account), + "LOG_BODY": True, + # # Ignore requests made by card-service. + "LOG_BODY_OUTGOING": False, + "GET_METADATA": lambda request, response: { + "company_name": 'abc', + }, + "REQUEST_HEADER_MASKS": ( + # Contains auth token + "authorization", + # Contains the users jwt and refresh token + "cookie", + # Reduce noise + "x-akamai-config-log-detail", + "x-datadog-parent-id", + "x-datadog-sampling-priority", + "x-datadog-trace-id", + "x-edgeconnect-session-id", + "x-envoy-attempt-count", + "x-envoy-decorator-operation", + "x-envoy-external-address", + "x-envoy-peer-metadata", + "x-envoy-peer-metadata-id", + "x-forwarded-for", + "x-forwarded-proto", + "x-request-id", + ), + "LOCAL_DEBUG": True, + "GET_SESSION_TOKEN": lambda request, response: "xxxxxxxxxx", +} - # Optional: Function to identify companies - 'IDENTIFY_COMPANY': lambda request, response: None, - # Optional: Function to add metadata to events - 'ADD_EVENT_METADATA': lambda request, response: {'foo': 'bar'}, +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + 'file': { + 'class': 'logging.FileHandler', + 'filename': 'debug.log', + }, + }, + 'root': { + 'handlers': ['console', 'file'], + 'level': 'INFO', + }, } diff --git a/manage.py b/manage.py index 5367740..7769fa9 100755 --- a/manage.py +++ b/manage.py @@ -2,14 +2,6 @@ """Django's command-line utility for administrative tasks.""" import os import sys -from ddtrace import patch_all, tracer -from custom_writer import ConsoleWriter - -patch_all() -# Set the custom writer to log traces to the console for easier debugging -# but you can use any writer you want after verify writing to console works. -tracer.configure(writer=ConsoleWriter()) - def main(): """Run administrative tasks.""" diff --git a/requirements.txt b/requirements.txt index 430ac2c..2325d1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ ddtrace==2.8.0 Django==4.2.11 djangorestframework==3.15.2 moesifdjango==2.3.11 +python-dotenv==1.0.1