]> git.proxmox.com Git - ceph.git/blob - ceph/src/blkin/babeltrace-plugins/zipkin/src/zipkin_logic/trace.py
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / blkin / babeltrace-plugins / zipkin / src / zipkin_logic / trace.py
1 import math
2 import time
3 import random
4
5
6 class Trace(object):
7 """
8 An L{ITrace} provider which delegates to zero or more L{ITracers} and
9 allows setting a default L{IEndpoint} to associate with L{IAnnotation}s
10
11 @ivar _tracers: C{list} of one or more L{ITracer} providers.
12 @ivar _endpoint: An L{IEndpoint} provider.
13 """
14 def __init__(self, name, trace_id=None, span_id=None,
15 parent_span_id=None, tracers=None):
16 """
17 @param name: C{str} describing the current span.
18 @param trace_id: C{int} or C{None}
19 @param span_id: C{int} or C{None}
20 @param parent_span_id: C{int} or C{None}
21
22 @param tracers: C{list} of L{ITracer} providers, primarily useful
23 for unit testing.
24 """
25 self.name = name
26 # If no trace_id and span_id are given we want to generate new
27 # 64-bit integer ids.
28 self.trace_id = trace_id
29 self.span_id = span_id
30
31 # If no parent_span_id is given then we assume there is no parent span
32 # and leave it as None.
33 self.parent_span_id = parent_span_id
34
35 # If no tracers are given we get the global list of tracers.
36 self._tracers = tracers
37
38 # By default no endpoint will be associated with annotations recorded
39 # to this trace.
40 self._endpoint = None
41
42 def __ne__(self, other):
43 return not self == other
44
45 def __repr__(self):
46 return (
47 '{0.__class__.__name__}({0.name!r}, trace_id={0.trace_id!r}, '
48 'span_id={0.span_id!r}, parent_span_id={0.parent_span_id!r})'
49 ).format(self)
50
51 def set_endpoint(self, endpoint):
52 """
53 Set a default L{IEndpoint} provider for the current L{Trace}.
54 All annotations recorded after this endpoint is set will use it,
55 unless they provide their own endpoint.
56 """
57 self._endpoint = endpoint
58
59
60 class Endpoint(object):
61
62 def __init__(self, ipv4, port, service_name):
63 """
64 @param ipv4: C{str} ipv4 address.
65 @param port: C{int} port number.
66 @param service_name: C{str} service name.
67 """
68 self.ipv4 = ipv4
69 self.port = port
70 self.service_name = service_name
71
72 def __ne__(self, other):
73 return not self == other
74
75 def __repr__(self):
76 return ('{0.__class__.__name__}({0.ipv4!r}, {0.port!r}, '
77 '{0.service_name!r})').format(self)
78
79
80 class Annotation(object):
81
82 def __init__(self, name, value, annotation_type, endpoint=None):
83 """
84 @param name: C{str} name of this annotation.
85
86 @param value: A value of the appropriate type based on
87 C{annotation_type}.
88
89 @param annotation_type: C{str} the expected type of our C{value}.
90
91 @param endpoint: An optional L{IEndpoint} provider to associate with
92 this annotation or C{None}
93 """
94 self.name = name
95 self.value = value
96 self.annotation_type = annotation_type
97 self.endpoint = endpoint
98
99 def __ne__(self, other):
100 return not self == other
101
102 def __repr__(self):
103 return (
104 '{0.__class__.__name__}({0.name!r}, {0.value!r}, '
105 '{0.annotation_type!r}, {0.endpoint})'
106 ).format(self)
107
108 @classmethod
109 def timestamp(cls, name, timestamp=None):
110 if timestamp is None:
111 timestamp = math.trunc(time.time() * 1000 * 1000)
112
113 return cls(name, timestamp, 'timestamp')
114
115 @classmethod
116 def client_send(cls, timestamp=None):
117 return cls.timestamp(constants.CLIENT_SEND, timestamp)
118
119 @classmethod
120 def client_recv(cls, timestamp=None):
121 return cls.timestamp(constants.CLIENT_RECV, timestamp)
122
123 @classmethod
124 def server_send(cls, timestamp=None):
125 return cls.timestamp(constants.SERVER_SEND, timestamp)
126
127 @classmethod
128 def server_recv(cls, timestamp=None):
129 return cls.timestamp(constants.SERVER_RECV, timestamp)
130
131 @classmethod
132 def string(cls, name, value):
133 return cls(name, value, 'string')
134
135 @classmethod
136 def bytes(cls, name, value):
137 return cls(name, value, 'bytes')