Debian Codegraph @

docs/basic.rst

raw
1 .. _basic:
2
3 Basic Usage
4 ===========
5
6 The SDK provides a global recorder, ``xray_recorder``, to generate segments and subsegments.
7
8 Manually create segment/subsegment
9 ----------------------------------
10 If you're using a web framework or library that is not supported, or you want to define
11 your own structure on segments/subsegments, you can manually create
12 segments and subsegments by using code like the following::
13
14 from aws_xray_sdk.core import xray_recorder
15
16 xray_recorder.begin_segment('name')
17
18 # your code here
19
20 xray_recorder.begin_subsegment('name')
21 # some code block you want to record
22 xray_recorder.end_subsegment()
23
24 xray_recorder.end_segment()
25
26 The ``xray_recorder`` keeps one segment per thread.
27 Therefore, in manual mode, call ``xray_recorder.end_segment()`` before creating a new segment,
28 otherwise the new segment overwrites the existing one.
29 To trace a particular code block inside a segment, use a subsegment.
30 If you open a new subsegment while there is already an open subsegment,
31 the new subsegment becomes the child of the existing subsegment.
32
33 Decorator for function auto-capture
34 -----------------------------------
35 A decorator is provided to easily capture basic information as a subsegment on
36 user defined functions. You can use the decorator like the following::
37
38 @xray_recorder.capture('name')
39 def my_func():
40 #do something
41
42 ``xray_recorder`` generates a subsegment for the decorated function, where the name is optional.
43 If the name argument is not provided, the function name is used as the subsegment name.
44 If the function is called without an open segment in the context storage, the subsegment is discarded.
45 Currently the decorator only works with synchronous functions.
46
47 Set annotation or metadata
48 --------------------------
49 You can add annotations and metadata to an active segment/subsegment.
50
51 Annotations are simple key-value pairs that are indexed for use with
52 `filter expressions <http://docs.aws.amazon.com/xray/latest/devguide/xray-console-filters.html>`_.
53 Use annotations to record data that you want to use to group traces in the console,
54 or when calling the GetTraceSummaries API.
55
56 Metadata are key-value pairs with values of any type, including objects and lists, but that are not indexed.
57 Use metadata to record data you want to store in the trace but don't need to use for searching traces.
58
59 You can add annotations/metadata like the following::
60
61 from aws_xray_sdk.core import xray_recorder
62
63 segment = xray_recorder.current_segment()
64 # value can be string, number or bool
65 segment.put_annotation('key', value)
66 # namespace and key must be string and value is an object
67 # that can be serialized to json
68 segment.put_metadata('key', json, 'namespace')
69
70 The ``current_segment`` and ``current_subsegment`` functions get the current
71 open segment or subsegment, respectively, from context storage.
72 Put these calls between segment or subsegment begin and end statements.
73
74 AWS Lambda Integration
75 ----------------------
76
77 To integrate with Lambda you must
78 first enable active tracing on a Lambda function.
79 See http://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html#using-x-ray for details.
80
81 In your Lambda function, you can only begin and end a subsegment.
82 The Lambda service emits a segment as the root.
83 This segment cannot be mutated.
84 Instrument the SDK as you would in any Python script.
85 Subsegments generated outside of the Lambda handler are discarded.