Skip to content

Commit 73249f1

Browse files
authored
Add annotations (#348)
* Add annotations * Update based on PR discussions * Remove namespace='remote' as it's not necessary for indexing
1 parent 4b664e8 commit 73249f1

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,19 @@ If `AUTO_PATCH_PARENT_SEGMENT_NAME` is also specified, then a segment parent wil
383383
with the supplied name, wrapping the automatic patching so that it captures any dangling
384384
subsegments created on the import patching.
385385

386+
### Django in Lambda
387+
X-Ray can't search on http annotations in subsegments. To enable searching the middleware adds the http values as annotations
388+
This allows searching in the X-Ray console like so
389+
390+
This is configurable in settings with `URLS_AS_ANNOTATION` that has 3 valid values
391+
`LAMBDA` - the default, which uses URLs as annotations by default if running in a lambda context
392+
`ALL` - do this for every request (useful if running in a mixed lambda/other deployment)
393+
`NONE` - don't do this for any (avoiding hitting the 50 annotation limit)
394+
395+
```
396+
annotation.url BEGINSWITH "https://your.url.com/here"
397+
```
398+
386399
### Add Flask middleware
387400

388401
```python

aws_xray_sdk/ext/django/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'PATCH_MODULES': [],
2020
'AUTO_PATCH_PARENT_SEGMENT_NAME': None,
2121
'IGNORE_MODULE_PATTERNS': [],
22+
'URLS_AS_ANNOTATION': 'LAMBDA', # 3 valid values, NONE -> don't ever, LAMBDA -> only for AWS Lambdas, ALL -> every time
2223
}
2324

2425
XRAY_NAMESPACE = 'XRAY_RECORDER'

aws_xray_sdk/ext/django/middleware.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from .conf import settings
23

34
from aws_xray_sdk.core import xray_recorder
45
from aws_xray_sdk.core.models import http
@@ -30,6 +31,14 @@ def __init__(self, get_response):
3031
if check_in_lambda() and type(xray_recorder.context) == LambdaContext:
3132
self.in_lambda_ctx = True
3233

34+
def _urls_as_annotation(self):
35+
if settings.URLS_AS_ANNOTATION == "LAMBDA" and self.in_lambda_ctx:
36+
return True
37+
elif settings.URLS_AS_ANNOTATION == "ALL":
38+
return True
39+
return False
40+
41+
3342
# hooks for django version >= 1.10
3443
def __call__(self, request):
3544

@@ -50,9 +59,10 @@ def __call__(self, request):
5059
recorder=xray_recorder,
5160
sampling_req=sampling_req,
5261
)
53-
5462
if self.in_lambda_ctx:
5563
segment = xray_recorder.begin_subsegment(name)
64+
# X-Ray can't search/filter subsegments on URL but it can search annotations
65+
# So for lambda to be able to filter by annotation we add these as annotations
5666
else:
5767
segment = xray_recorder.begin_segment(
5868
name=name,
@@ -64,23 +74,37 @@ def __call__(self, request):
6474
segment.save_origin_trace_header(xray_header)
6575
segment.put_http_meta(http.URL, request.build_absolute_uri())
6676
segment.put_http_meta(http.METHOD, request.method)
77+
if self._urls_as_annotation():
78+
segment.put_annotation(http.URL, request.build_absolute_uri())
79+
segment.put_annotation(http.METHOD, request.method)
6780

6881
if meta.get(USER_AGENT_KEY):
6982
segment.put_http_meta(http.USER_AGENT, meta.get(USER_AGENT_KEY))
83+
if self._urls_as_annotation():
84+
segment.put_annotation(http.USER_AGENT, meta.get(USER_AGENT_KEY))
7085
if meta.get(X_FORWARDED_KEY):
7186
# X_FORWARDED_FOR may come from untrusted source so we
7287
# need to set the flag to true as additional information
7388
segment.put_http_meta(http.CLIENT_IP, meta.get(X_FORWARDED_KEY))
7489
segment.put_http_meta(http.X_FORWARDED_FOR, True)
90+
if self._urls_as_annotation():
91+
segment.put_annotation(http.CLIENT_IP, meta.get(X_FORWARDED_KEY))
92+
segment.put_annotation(http.X_FORWARDED_FOR, True)
7593
elif meta.get(REMOTE_ADDR_KEY):
7694
segment.put_http_meta(http.CLIENT_IP, meta.get(REMOTE_ADDR_KEY))
95+
if self._urls_as_annotation():
96+
segment.put_annotation(http.CLIENT_IP, meta.get(REMOTE_ADDR_KEY))
7797

7898
response = self.get_response(request)
7999
segment.put_http_meta(http.STATUS, response.status_code)
100+
if self._urls_as_annotation():
101+
segment.put_annotation(http.STATUS, response.status_code)
80102

81103
if response.has_header(CONTENT_LENGTH_KEY):
82104
length = int(response[CONTENT_LENGTH_KEY])
83105
segment.put_http_meta(http.CONTENT_LENGTH, length)
106+
if self._urls_as_annotation():
107+
segment.put_annotation(http.CONTENT_LENGTH, length)
84108
response[http.XRAY_HEADER] = prepare_response_header(xray_header, segment)
85109

86110
if self.in_lambda_ctx:

0 commit comments

Comments
 (0)