| 1 |
.. _configurations: |
| 2 |
|
| 3 |
Configure Global Recorder |
| 4 |
========================= |
| 5 |
|
| 6 |
Sampling |
| 7 |
-------- |
| 8 |
Sampling is enabled by default. |
| 9 |
Whenever the global recorder creates a segment, |
| 10 |
it decides whether to sample this segment. |
| 11 |
If it does not sample this segment, it is discarded and not sent to the |
| 12 |
X-Ray daemon. |
| 13 |
|
| 14 |
To turn off sampling, use code like the following:: |
| 15 |
|
| 16 |
from aws_xray_sdk.core import xray_recorder |
| 17 |
xray_recorder.configure(sampling=False) |
| 18 |
|
| 19 |
You can also configure the sampling rules:: |
| 20 |
|
| 21 |
xray_recorder.configure(sampling_rules=your_rules) |
| 22 |
|
| 23 |
The input can either be an absolute path to your sampling rule |
| 24 |
*.json* file or a dictionary. |
| 25 |
|
| 26 |
The following code is an example of a rule configuration:: |
| 27 |
|
| 28 |
{ |
| 29 |
"version": 1, |
| 30 |
"rules": [ |
| 31 |
{ |
| 32 |
"description": "Player moves.", |
| 33 |
"service_name": "*", |
| 34 |
"http_method": "*", |
| 35 |
"url_path": "/api/move/*", |
| 36 |
"fixed_target": 0, |
| 37 |
"rate": 0.05 |
| 38 |
} |
| 39 |
], |
| 40 |
"default": { |
| 41 |
"fixed_target": 1, |
| 42 |
"rate": 0.1 |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
This example defines one custom rule and a default rule. |
| 47 |
The custom rule applies a five-percent sampling rate |
| 48 |
with no minimum number of requests to trace for paths under */api/move/*. |
| 49 |
The default rule traces the first request each second and 10 percent of |
| 50 |
additional requests. |
| 51 |
The SDK applies custom rules in the order in which they are defined. |
| 52 |
If a request matches multiple custom rules, the SDK applies only the first rule. |
| 53 |
You can use wildcard character "*" and "?" in service_name, http_method and |
| 54 |
url_path. |
| 55 |
"*" represents any combination of characters. "?" represents a single character. |
| 56 |
|
| 57 |
Note that sampling configurations have no effect if the application runs in AWS Lambda. |
| 58 |
|
| 59 |
Plugins |
| 60 |
------- |
| 61 |
The plugin adds extra metadata for each segment if the app is running on that environment. |
| 62 |
The SDK provides three plugins: |
| 63 |
|
| 64 |
* Amazon EC2 – EC2Plugin adds the instance ID and Availability Zone. |
| 65 |
* Elastic Beanstalk – ElasticBeanstalkPlugin adds the environment name, version label, and deployment ID. |
| 66 |
* Amazon ECS – ECSPlugin adds the container host name |
| 67 |
|
| 68 |
To use plugins, use code like the following:: |
| 69 |
|
| 70 |
# a tuple of strings |
| 71 |
plugins = ('elasticbeanstalk_plugin', 'ec2_plugin', 'ecs_plugin') |
| 72 |
# alternatively you can use |
| 73 |
plugins = ('ElasticBeanstalkPlugin', 'EC2Plugin', 'ECSPlugin') |
| 74 |
|
| 75 |
xray_recorder.configure(plugins=plugins) |
| 76 |
|
| 77 |
Order matters in the tuple and the origin of the segment is set from the last plugin. |
| 78 |
Therefore, in the previous example, if the program runs on ECS, the segment origin is |
| 79 |
'AWS::ECS::CONTAINER'. |
| 80 |
Plugins must be configured before patching any third party libraries to |
| 81 |
avoid unexpected behavior. |
| 82 |
Plugins are employed after they are specified. |
| 83 |
|
| 84 |
Context Missing Strategy |
| 85 |
------------------------ |
| 86 |
Defines the recorder behavior when your instrumented code attempts to record data when no segment is open. |
| 87 |
Configure like the following:: |
| 88 |
|
| 89 |
xray_recorder.configure(context_missing='Your Strategy Name Here') |
| 90 |
|
| 91 |
Supported strategies are: |
| 92 |
|
| 93 |
* RUNTIME_ERROR: throw an SegmentNotFoundException |
| 94 |
* LOG_ERROR: log an error and continue |
| 95 |
|
| 96 |
Segment Dynamic Naming |
| 97 |
---------------------- |
| 98 |
For a web application you might want to name the segment using host names. You can pass in a pattern |
| 99 |
with wildcard character "*" and "?". "*" represents any combination of characters. |
| 100 |
"?" represents a single character. If the host name from incoming request's header matches the pattern, |
| 101 |
the host name will be used as the segment name, otherwise it uses fallback name defined in ``AWS_XRAY_TRACING_NAME``. |
| 102 |
To configure dynamic naming, use code like the following:: |
| 103 |
|
| 104 |
xray_recorder.configure(dynamic_naming='*.example.com') |
| 105 |
|
| 106 |
Environment Variables |
| 107 |
--------------------- |
| 108 |
There are three supported environment variables to configure the global |
| 109 |
recorder: |
| 110 |
|
| 111 |
* AWS_XRAY_CONTEXT_MISSING: configure context missing strategy |
| 112 |
* AWS_XRAY_TRACING_NAME: default segment name |
| 113 |
* AWS_XRAY_DAEMON_ADDRESS: where the recorder sends data to over UDP |
| 114 |
|
| 115 |
Environment variables has higher precedence over ``xray_recorder.configure()`` |
| 116 |
|
| 117 |
Logging |
| 118 |
------- |
| 119 |
The SDK uses Python's built-in ``logging`` module to perform logging. |
| 120 |
You can configure the SDK logging just like how you configure other |
| 121 |
python libraries. An example of set the SDK log level is like the following:: |
| 122 |
|
| 123 |
logging.basicConfig(level='DEBUG') |
| 124 |
logging.getLogger('aws_xray_sdk').setLevel(logging.WARNING) |
| 125 |
|
| 126 |
Context Storage |
| 127 |
--------------- |
| 128 |
The global recorder uses threadlocal to store active segments/subsegments. |
| 129 |
You can override the default context class to implement your own context storage:: |
| 130 |
|
| 131 |
from aws_xray_sdk.core.context import Context |
| 132 |
|
| 133 |
class MyOwnContext(Context): |
| 134 |
|
| 135 |
def put_segment(self, segment): |
| 136 |
# store the segment created by ``xray_recorder`` to the context. |
| 137 |
pass |
| 138 |
|
| 139 |
def end_segment(self, end_time=None): |
| 140 |
# end the segment in the current context. |
| 141 |
pass |
| 142 |
|
| 143 |
def put_subsegment(self, subsegment): |
| 144 |
# store the subsegment created by ``xray_recorder`` to the context. |
| 145 |
pass |
| 146 |
|
| 147 |
def end_subsegment(self, end_time=None): |
| 148 |
# end the subsegment in the current context. |
| 149 |
pass |
| 150 |
|
| 151 |
def get_trace_entity(self): |
| 152 |
# get the current active trace entity(segment or subsegment). |
| 153 |
pass |
| 154 |
|
| 155 |
def set_trace_entity(self, trace_entity): |
| 156 |
# manually inject a trace entity to the context storage. |
| 157 |
pass |
| 158 |
|
| 159 |
def clear_trace_entities(self): |
| 160 |
# clean up context storage. |
| 161 |
pass |
| 162 |
|
| 163 |
def handle_context_missing(self): |
| 164 |
# behavior on no trace entity to access or mutate. |
| 165 |
pass |
| 166 |
|
| 167 |
The function ``current_segment`` and ``current_subsegment`` on recorder level uses |
| 168 |
``context.get_trace_entity()`` and dynamically return the expected type by using internal |
| 169 |
references inside segment/subsegment objects. |
| 170 |
|
| 171 |
Then you can pass your own context:: |
| 172 |
|
| 173 |
my_context=MyOwnContext() |
| 174 |
xray_recorder.configure(context=my_context) |
| 175 |
|
| 176 |
Emitter |
| 177 |
------- |
| 178 |
The default emitter uses non-blocking socket to send data to the X-Ray daemon. |
| 179 |
It doesn't retry on IOError. To override the default emitter:: |
| 180 |
|
| 181 |
from aws_xray_sdk.core.emitters.udp_emitter import UDPEmitter |
| 182 |
|
| 183 |
class MyOwnEmitter(UDPEmitter): |
| 184 |
|
| 185 |
def send_entity(self, entity): |
| 186 |
# send the input segment/subsegment to the X-Ray daemon. |
| 187 |
# Return True on success and False on failure. |
| 188 |
pass |
| 189 |
|
| 190 |
def set_daemon_address(self, address): |
| 191 |
# parse input full address like 127.0.0.1:8000 to ip and port and |
| 192 |
# store them to the local emitter properties. |
| 193 |
pass |
| 194 |
|
| 195 |
Then you can pass your own emitter:: |
| 196 |
|
| 197 |
my_emitter = MyOwnEmitter() |
| 198 |
xray_recorder.configure(emitter=my_emitter) |