Django-Xray Tracing Setup

Install aws-xray-sdk

pip install aws_xray_sdk

Add Middleware

in settings.py, add below line at the top of MIDDLEWARE's list

"aws_xray_sdk.ext.django.middleware.XRayMiddleware"

Configure Xray

Add the below config in settings.py

XRAY_RECORDER = {
    'AWS_XRAY_DAEMON_ADDRESS': '127.0.0.1:2000',
    'AUTO_INSTRUMENT': True,  # If turned on built-in database queries and template rendering will be recorded as subsegments
    'AWS_XRAY_TRACING_NAME': 'my-application',  # the segment name for segments generated from incoming requests
    'PATCH_MODULES': [ #optional
        "my_module_1",
        "my_module_2",
    ],
    "AUTO_PATCH_PARENT_SEGMENT_NAME": True,
    "SAMPLING": True,
    'SAMPLING_RULES': {
        "version": 1,
        "rules": [
            {
                "description": "GraphQL APIs",
                "service_name": "*",
                "http_method": "*",
                "url_path": "/graphql",
                "fixed_target": 1, # no of calls to trace per sec addition to rate
                "rate": 0.01 # the rate at which traces should be handled here.
            }
        ],
        "default": {
            "fixed_target": 0,
            "rate": 0.01
        }
    }
}

Tracing For specific methods:

from aws_xray_sdk.core import xray_recorder

@xray_recorder.capture('some_unique_name_here')
def my_function():
    pass

Enable Xray Tracing at Lambda Function Level

To enable xray tracing in lambda, you can update zappa_settings.json, set xray_tracing with valuetrue

{
  "alpha": {
      "project_name": "my-project",
      ...
      ...
      "xray_tracing": true
    }
}

Or You can enable it manually in Lambda Function -> Configuration Tab -> Monitoring and operations tools -> AWS X-Ray (Active tracing)

Pricing

$5 per 1 Million Traces.

Last updated