File: apps.py

package info (click to toggle)
python-aws-xray-sdk 0.95-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: python: 3,006; makefile: 20
file content (49 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging

from django.apps import AppConfig

from .conf import settings
from .db import patch_db
from .templates import patch_template
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.exceptions.exceptions import SegmentNameMissingException


log = logging.getLogger(__name__)


class XRayConfig(AppConfig):
    name = 'aws_xray_sdk.ext.django'

    def ready(self):
        """
        Configure global XRay recorder based on django settings
        under XRAY_RECORDER namespace.
        This method could be called twice during server startup
        because of base command and reload command.
        So this function must be idempotent
        """
        if not settings.AWS_XRAY_TRACING_NAME:
            raise SegmentNameMissingException('Segment name is required.')

        xray_recorder.configure(
            daemon_address=settings.AWS_XRAY_DAEMON_ADDRESS,
            sampling=settings.SAMPLING,
            sampling_rules=settings.SAMPLING_RULES,
            context_missing=settings.AWS_XRAY_CONTEXT_MISSING,
            plugins=settings.PLUGINS,
            service=settings.AWS_XRAY_TRACING_NAME,
            dynamic_naming=settings.DYNAMIC_NAMING,
        )

        # if turned on subsegment will be generated on
        # built-in database and template rendering
        if settings.AUTO_INSTRUMENT:
            try:
                patch_db()
            except Exception:
                log.debug('failed to patch Django built-in database')
            try:
                patch_template()
            except Exception:
                log.debug('failed to patch Django built-in template engine')