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,12 +59,10 @@ def __call__(self, request):
50
59
recorder = xray_recorder ,
51
60
sampling_req = sampling_req ,
52
61
)
53
- http_as_annotations = False
54
62
if self .in_lambda_ctx :
55
63
segment = xray_recorder .begin_subsegment (name , namespace = "remote" )
56
64
# X-Ray can't search/filter subsegments on URL but it can search annotations
57
65
# So for lambda to be able to filter by annotation we add these as annotations
58
- http_as_annotations = True
59
66
else :
60
67
segment = xray_recorder .begin_segment (
61
68
name = name ,
@@ -67,36 +74,36 @@ def __call__(self, request):
67
74
segment .save_origin_trace_header (xray_header )
68
75
segment .put_http_meta (http .URL , request .build_absolute_uri ())
69
76
segment .put_http_meta (http .METHOD , request .method )
70
- if http_as_annotations :
77
+ if self . _urls_as_annotation () :
71
78
segment .put_annotation (http .URL , request .build_absolute_uri ())
72
79
segment .put_annotation (http .METHOD , request .method )
73
80
74
81
if meta .get (USER_AGENT_KEY ):
75
82
segment .put_http_meta (http .USER_AGENT , meta .get (USER_AGENT_KEY ))
76
- if http_as_annotations :
83
+ if self . _urls_as_annotation () :
77
84
segment .put_annotation (http .USER_AGENT , meta .get (USER_AGENT_KEY ))
78
85
if meta .get (X_FORWARDED_KEY ):
79
86
# X_FORWARDED_FOR may come from untrusted source so we
80
87
# need to set the flag to true as additional information
81
88
segment .put_http_meta (http .CLIENT_IP , meta .get (X_FORWARDED_KEY ))
82
89
segment .put_http_meta (http .X_FORWARDED_FOR , True )
83
- if http_as_annotations :
90
+ if self . _urls_as_annotation () :
84
91
segment .put_annotation (http .CLIENT_IP , meta .get (X_FORWARDED_KEY ))
85
92
segment .put_annotation (http .X_FORWARDED_FOR , True )
86
93
elif meta .get (REMOTE_ADDR_KEY ):
87
94
segment .put_http_meta (http .CLIENT_IP , meta .get (REMOTE_ADDR_KEY ))
88
- if http_as_annotations :
95
+ if self . _urls_as_annotation () :
89
96
segment .put_annotation (http .CLIENT_IP , meta .get (REMOTE_ADDR_KEY ))
90
97
91
98
response = self .get_response (request )
92
99
segment .put_http_meta (http .STATUS , response .status_code )
93
- if http_as_annotations :
100
+ if self . _urls_as_annotation () :
94
101
segment .put_annotation (http .STATUS , response .status_code )
95
102
96
103
if response .has_header (CONTENT_LENGTH_KEY ):
97
104
length = int (response [CONTENT_LENGTH_KEY ])
98
105
segment .put_http_meta (http .CONTENT_LENGTH , length )
99
- if http_as_annotations :
106
+ if self . _urls_as_annotation () :
100
107
segment .put_annotation (http .CONTENT_LENGTH , length )
101
108
response [http .XRAY_HEADER ] = prepare_response_header (xray_header , segment )
102
109
0 commit comments