Debian Codegraph @

aws_xray_sdk/core/models/subsegment.py

raw
1 import copy
2
3 from .entity import Entity
4 from ..exceptions.exceptions import SegmentNotFoundException
5
6
7 class Subsegment(Entity):
8 """
9 The work done in a single segment can be broke down into subsegments.
10 Subsegments provide more granular timing information and details about
11 downstream calls that your application made to fulfill the original request.
12 A subsegment can contain additional details about a call to an AWS service,
13 an external HTTP API, or an SQL database.
14 """
15 def __init__(self, name, namespace, segment):
16 """
17 Create a new subsegment.
18
19 :param str name: Subsegment name is required.
20 :param str namespace: The namespace of the subsegment. Currently
21 support `aws`, `remote` and `local`.
22 :param Segment segment: The parent segment
23 """
24 super(Subsegment, self).__init__(name)
25
26 if not segment:
27 raise SegmentNotFoundException("A parent segment is required for creating subsegments.")
28
29 self.parent_segment = segment
30 self.trace_id = segment.trace_id
31
32 self.type = 'subsegment'
33 self.namespace = namespace
34
35 self.sql = {}
36
37 def add_subsegment(self, subsegment):
38 """
39 Add input subsegment as a child subsegment and increment
40 reference counter and total subsegments counter of the
41 parent segment.
42 """
43 super(Subsegment, self).add_subsegment(subsegment)
44 self.parent_segment.increment()
45
46 def remove_subsegment(self, subsegment):
47 """
48 Remove input subsegment from child subsegemnts and
49 decrement parent segment total subsegments count.
50
51 :param Subsegment: subsegment to remove.
52 """
53 super(Subsegment, self).remove_subsegment(subsegment)
54 self.parent_segment.decrement_subsegments_size()
55
56 def close(self, end_time=None):
57 """
58 Close the trace entity by setting `end_time`
59 and flip the in progress flag to False. Also decrement
60 parent segment's ref counter by 1.
61
62 :param int end_time: Epoch in seconds. If not specified
63 current time will be used.
64 """
65 super(Subsegment, self).close(end_time)
66 self.parent_segment.decrement_ref_counter()
67
68 def set_sql(self, sql):
69 """
70 Set sql related metadata. This function is used by patchers
71 for database connectors and is not recommended to
72 invoke manually.
73
74 :param dict sql: sql related metadata
75 """
76 self.sql = sql
77
78 def __getstate__(self):
79
80 properties = copy.copy(self.__dict__)
81 super(Subsegment, self)._delete_empty_properties(properties)
82
83 del properties['parent_segment']
84 if not self.sql:
85 del properties['sql']
86 return properties