Debian Codegraph @

aws_xray_sdk/core/models/segment.py

raw
1 import copy
2
3 from .entity import Entity
4 from .traceid import TraceId
5 from ..utils.atomic_counter import AtomicCounter
6 from ..exceptions.exceptions import SegmentNameMissingException
7
8
9 class Segment(Entity):
10 """
11 The compute resources running your application logic send data
12 about their work as segments. A segment provides the resource's name,
13 details about the request, and details about the work done.
14 """
15 def __init__(self, name, entityid=None, traceid=None,
16 parent_id=None, sampled=True):
17 """
18 Create a segment object.
19
20 :param str name: segment name. If not specified a
21 SegmentNameMissingException will be thrown.
22 :param str entityid: hexdigits segment id.
23 :param str traceid: The trace id of the segment.
24 :param str parent_id: The parent id of the segment. It comes
25 from id of an upstream segment or subsegment.
26 :param bool sampled: If False this segment will not be sent
27 to the X-Ray daemon.
28 """
29 if not name:
30 raise SegmentNameMissingException("Segment name is required.")
31
32 super(Segment, self).__init__(name)
33
34 if not traceid:
35 traceid = TraceId().to_id()
36 self.trace_id = traceid
37 if entityid:
38 self.id = entityid
39
40 self.in_progress = True
41 self.sampled = sampled
42 self.ref_counter = AtomicCounter()
43 self._subsegments_counter = AtomicCounter()
44
45 if parent_id:
46 self.parent_id = parent_id
47
48 def add_subsegment(self, subsegment):
49 """
50 Add input subsegment as a child subsegment and increment
51 reference counter and total subsegments counter.
52 """
53 super(Segment, self).add_subsegment(subsegment)
54 self.increment()
55
56 def increment(self):
57 """
58 Increment reference counter to track on open subsegments
59 and total subsegments counter to track total size of subsegments
60 it currently hold.
61 """
62 self.ref_counter.increment()
63 self._subsegments_counter.increment()
64
65 def decrement_ref_counter(self):
66 """
67 Decrement reference counter by 1 when a subsegment is closed.
68 """
69 self.ref_counter.decrement()
70
71 def ready_to_send(self):
72 """
73 Return True if the segment doesn't have any open subsegments
74 and itself is not in progress.
75 """
76 return self.ref_counter.get_current() <= 0 and not self.in_progress
77
78 def get_total_subsegments_size(self):
79 """
80 Return the number of total subsegments regardless of open or closed.
81 """
82 return self._subsegments_counter.get_current()
83
84 def decrement_subsegments_size(self):
85 """
86 Decrement total subsegments by 1. This usually happens when
87 a subsegment is streamed out.
88 """
89 return self._subsegments_counter.decrement()
90
91 def remove_subsegment(self, subsegment):
92 """
93 Remove the reference of input subsegment.
94 """
95 super(Segment, self).remove_subsegment(subsegment)
96 self.decrement_subsegments_size()
97
98 def __getstate__(self):
99 """
100 Used by jsonpikle to remove unwanted fields.
101 """
102 properties = copy.copy(self.__dict__)
103 super(Segment, self)._delete_empty_properties(properties)
104
105 del properties['ref_counter']
106 del properties['_subsegments_counter']
107 return properties