Skip to content

Commit 3ddb808

Browse files
committed
Update based on PR discussions
1 parent 2a70787 commit 3ddb808

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ subsegments created on the import patching.
386386
### Django in Lambda
387387
X-Ray can't search on http annotations in subsegments. To enable searching the middleware adds the http values as annotations
388388
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+
389395
```
390396
annotation.url BEGINSWITH "https://your.url.com/here"
391397
```

aws_xray_sdk/ext/django/conf.py

Lines changed: 1 addition & 0 deletions
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

Lines changed: 15 additions & 8 deletions
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,12 +59,10 @@ def __call__(self, request):
5059
recorder=xray_recorder,
5160
sampling_req=sampling_req,
5261
)
53-
http_as_annotations = False
5462
if self.in_lambda_ctx:
5563
segment = xray_recorder.begin_subsegment(name, namespace="remote")
5664
# X-Ray can't search/filter subsegments on URL but it can search annotations
5765
# So for lambda to be able to filter by annotation we add these as annotations
58-
http_as_annotations = True
5966
else:
6067
segment = xray_recorder.begin_segment(
6168
name=name,
@@ -67,36 +74,36 @@ def __call__(self, request):
6774
segment.save_origin_trace_header(xray_header)
6875
segment.put_http_meta(http.URL, request.build_absolute_uri())
6976
segment.put_http_meta(http.METHOD, request.method)
70-
if http_as_annotations:
77+
if self._urls_as_annotation():
7178
segment.put_annotation(http.URL, request.build_absolute_uri())
7279
segment.put_annotation(http.METHOD, request.method)
7380

7481
if meta.get(USER_AGENT_KEY):
7582
segment.put_http_meta(http.USER_AGENT, meta.get(USER_AGENT_KEY))
76-
if http_as_annotations:
83+
if self._urls_as_annotation():
7784
segment.put_annotation(http.USER_AGENT, meta.get(USER_AGENT_KEY))
7885
if meta.get(X_FORWARDED_KEY):
7986
# X_FORWARDED_FOR may come from untrusted source so we
8087
# need to set the flag to true as additional information
8188
segment.put_http_meta(http.CLIENT_IP, meta.get(X_FORWARDED_KEY))
8289
segment.put_http_meta(http.X_FORWARDED_FOR, True)
83-
if http_as_annotations:
90+
if self._urls_as_annotation():
8491
segment.put_annotation(http.CLIENT_IP, meta.get(X_FORWARDED_KEY))
8592
segment.put_annotation(http.X_FORWARDED_FOR, True)
8693
elif meta.get(REMOTE_ADDR_KEY):
8794
segment.put_http_meta(http.CLIENT_IP, meta.get(REMOTE_ADDR_KEY))
88-
if http_as_annotations:
95+
if self._urls_as_annotation():
8996
segment.put_annotation(http.CLIENT_IP, meta.get(REMOTE_ADDR_KEY))
9097

9198
response = self.get_response(request)
9299
segment.put_http_meta(http.STATUS, response.status_code)
93-
if http_as_annotations:
100+
if self._urls_as_annotation():
94101
segment.put_annotation(http.STATUS, response.status_code)
95102

96103
if response.has_header(CONTENT_LENGTH_KEY):
97104
length = int(response[CONTENT_LENGTH_KEY])
98105
segment.put_http_meta(http.CONTENT_LENGTH, length)
99-
if http_as_annotations:
106+
if self._urls_as_annotation():
100107
segment.put_annotation(http.CONTENT_LENGTH, length)
101108
response[http.XRAY_HEADER] = prepare_response_header(xray_header, segment)
102109

0 commit comments

Comments
 (0)