Skip to content

Commit

Permalink
move ddtrace to settings.py (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghengwang authored Dec 12, 2024
1 parent 89374ff commit cd896a2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 30 deletions.
94 changes: 72 additions & 22 deletions jandog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,8 +48,6 @@
ALLOWED_HOSTS = []




# Application definition

INSTALLED_APPS = [
Expand All @@ -44,14 +62,14 @@
]

MIDDLEWARE = [
'moesifdjango.middleware.moesif_middleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'moesifdjango.middleware.moesif_middleware',
]

ROOT_URLCONF = 'jandog.urls'
Expand Down Expand Up @@ -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',
},
}
8 changes: 0 additions & 8 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cd896a2

Please sign in to comment.