Debian Codegraph @

docs/frameworks.rst

raw
1 .. _frameworks:
2
3 Django
4 ======
5
6 Configure X-Ray Recorder
7 ------------------------
8 Make sure you add ``XRayMiddleWare`` as the first entry in your
9 Django *settings.py* file, as shown in the following example::
10
11 MIDDLEWARE = [
12 'aws_xray_sdk.ext.django.middleware.XRayMiddleware',
13 'django.contrib.auth.middleware.AuthenticationMiddleware',
14 'django.contrib.messages.middleware.MessageMiddleware',
15 ]
16
17 The incoming requests to the Django app are then automatically recorded as
18 a segment.
19
20 To get the current segment and add annotations or metadata as needed,
21 use the following statement in your application code when processing request::
22
23 segment = xray_recorder.current_segement()
24
25 For more configurations in your Django ``settings.py`` file,
26 add the following line::
27
28 INSTALLED_APPS = [
29 'django.contrib.admin',
30 ...
31 'django.contrib.sessions',
32 'aws_xray_sdk.ext.django',
33 ]
34
35 You can configure the X-Ray recorder in a Django app under the
36 'XRAY_RECORDER' namespace.
37 The default values are as follows::
38
39 XRAY_RECORDER = {
40 'AWS_XRAY_DAEMON_ADDRESS': '127.0.0.1:2000',
41 'AUTO_INSTRUMENT': True, # If turned on built-in database queries and template rendering will be recorded as subsegments
42 'AWS_XRAY_CONTEXT_MISSING': 'RUNTIME_ERROR',
43 'PLUGINS': (),
44 'SAMPLING': True,
45 'SAMPLING_RULES': None,
46 'AWS_XRAY_TRACING_NAME': None, # the segment name for segments generated from incoming requests
47 'DYNAMIC_NAMING': None, # defines a pattern that host names should match
48 }
49
50 Environment variables have higher precedence over user settings.
51 If neither is set, the defaults values shown previously are used.
52 'AWS_XRAY_TRACING_NAME' is required unless specified as an environment variable.
53 All other keys are optional.
54 For further information on individual settings, see the :ref:`Configure Global Recorder <configurations>` section.
55
56 Local Development
57 -----------------
58 When doing Django app local development, if you configured Django built-in database with ``AUTO_INSTRUMENT`` turned-on,
59 the command ``manage.py runserver`` may fail if ``AWS_XRAY_CONTEXT_MISSING`` is set to ``RUNTIME_ERROR``. This is because
60 the command ``runserver`` performs migrations check which will generate a subsegment,
61 the ``xray_recorder`` will raise an error since there is no active segment.
62
63 One solution is to set ``AWS_XRAY_CONTEXT_MISSING`` to ``LOG_ERROR`` so it only emits a error message on server startup.
64 Alternatively if you have defined your own ``ready()`` function for code execution at startup you can manually create a segment
65 as a placeholder.
66
67 By Django official guide it's recommanded to deploy Django to other servers in production so this particular issue normally
68 doesn't exist in production.
69
70 Flask
71 =====
72
73 To generate segment based on incoming requests, you need to instantiate the X-Ray middleware for flask::
74
75 from aws_xray_sdk.core import xray_recorder
76 from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
77
78 app = Flask(__name__)
79
80 xray_recorder.configure(service='my_app_name')
81 XRayMiddleware(app, xray_recorder)
82
83 Flask built-in template rendering will be wrapped into subsegments.
84 You can configure the recorder, see :ref:`Configure Global Recorder <configurations>` for more details.
85
86 aiohttp Server
87 ==============
88
89 For X-Ray to create a segment based on an incoming request, you need register some middleware with aiohttp. As aiohttp
90 is an asyncronous framework, X-Ray will also need to be configured with an ``AsyncContext`` compared to the default threaded
91 version.::
92
93 import asyncio
94
95 from aiohttp import web
96
97 from aws_xray_sdk.ext.aiohttp.middleware import middleware
98 from aws_xray_sdk.core.async_context import AsyncContext
99 from aws_xray_sdk.core import xray_recorder
100 # Configure X-Ray to use AsyncContext
101 xray_recorder.configure(service='service_name', context=AsyncContext())
102
103
104 async def handler(request):
105 return web.Response(body='Hello World')
106
107 loop = asyncio.get_event_loop()
108 # Use X-Ray SDK middleware, its crucial the X-Ray middleware comes first
109 app = web.Application(middlewares=[middleware])
110 app.router.add_get("/", handler)
111
112 web.run_app(app)
113
114 There are two things to note from the example above. Firstly a middleware corountine from aws-xray-sdk is provided during the creation
115 of an aiohttp server app. Lastly the ``xray_recorder`` has also been configured with a name and an ``AsyncContext``. See
116 :ref:`Configure Global Recorder <configurations>` for more information about configuring the ``xray_recorder``.