1
1
import logging
2
+ from .conf import settings
2
3
3
4
from aws_xray_sdk .core import xray_recorder
4
5
from aws_xray_sdk .core .models import http
@@ -30,6 +31,14 @@ def __init__(self, get_response):
30
31
if check_in_lambda () and type (xray_recorder .context ) == LambdaContext :
31
32
self .in_lambda_ctx = True
32
33
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
+
33
42
# hooks for django version >= 1.10
34
43
def __call__ (self , request ):
35
44
@@ -50,9 +59,10 @@ def __call__(self, request):
50
59
recorder = xray_recorder ,
51
60
sampling_req = sampling_req ,
52
61
)
53
-
54
62
if self .in_lambda_ctx :
55
63
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
56
66
else :
57
67
segment = xray_recorder .begin_segment (
58
68
name = name ,
@@ -64,23 +74,37 @@ def __call__(self, request):
64
74
segment .save_origin_trace_header (xray_header )
65
75
segment .put_http_meta (http .URL , request .build_absolute_uri ())
66
76
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 )
67
80
68
81
if meta .get (USER_AGENT_KEY ):
69
82
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 ))
70
85
if meta .get (X_FORWARDED_KEY ):
71
86
# X_FORWARDED_FOR may come from untrusted source so we
72
87
# need to set the flag to true as additional information
73
88
segment .put_http_meta (http .CLIENT_IP , meta .get (X_FORWARDED_KEY ))
74
89
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 )
75
93
elif meta .get (REMOTE_ADDR_KEY ):
76
94
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 ))
77
97
78
98
response = self .get_response (request )
79
99
segment .put_http_meta (http .STATUS , response .status_code )
100
+ if self ._urls_as_annotation ():
101
+ segment .put_annotation (http .STATUS , response .status_code )
80
102
81
103
if response .has_header (CONTENT_LENGTH_KEY ):
82
104
length = int (response [CONTENT_LENGTH_KEY ])
83
105
segment .put_http_meta (http .CONTENT_LENGTH , length )
106
+ if self ._urls_as_annotation ():
107
+ segment .put_annotation (http .CONTENT_LENGTH , length )
84
108
response [http .XRAY_HEADER ] = prepare_response_header (xray_header , segment )
85
109
86
110
if self .in_lambda_ctx :
0 commit comments