Debian Codegraph @

aws_xray_sdk/ext/django/apps.py

raw
1 import logging
2
3 from django.apps import AppConfig
4
5 from .conf import settings
6 from .db import patch_db
7 from .templates import patch_template
8 from aws_xray_sdk.core import xray_recorder
9 from aws_xray_sdk.core.exceptions.exceptions import SegmentNameMissingException
10
11
12 log = logging.getLogger(__name__)
13
14
15 class XRayConfig(AppConfig):
16 name = 'aws_xray_sdk.ext.django'
17
18 def ready(self):
19 """
20 Configure global XRay recorder based on django settings
21 under XRAY_RECORDER namespace.
22 This method could be called twice during server startup
23 because of base command and reload command.
24 So this function must be idempotent
25 """
26 if not settings.AWS_XRAY_TRACING_NAME:
27 raise SegmentNameMissingException('Segment name is required.')
28
29 xray_recorder.configure(
30 daemon_address=settings.AWS_XRAY_DAEMON_ADDRESS,
31 sampling=settings.SAMPLING,
32 sampling_rules=settings.SAMPLING_RULES,
33 context_missing=settings.AWS_XRAY_CONTEXT_MISSING,
34 plugins=settings.PLUGINS,
35 service=settings.AWS_XRAY_TRACING_NAME,
36 dynamic_naming=settings.DYNAMIC_NAMING,
37 )
38
39 # if turned on subsegment will be generated on
40 # built-in database and template rendering
41 if settings.AUTO_INSTRUMENT:
42 try:
43 patch_db()
44 except Exception:
45 log.debug('failed to patch Django built-in database')
46 try:
47 patch_template()
48 except Exception:
49 log.debug('failed to patch Django built-in template engine')