13
13
# limitations under the License.
14
14
15
15
16
+ import abc
16
17
import atexit
17
18
import logging
18
19
import random
@@ -114,6 +115,77 @@ def shutdown(self) -> None:
114
115
sp .shutdown ()
115
116
116
117
118
+ class EventBase (abc .ABC ):
119
+ def __init__ (self , name : str , timestamp : Optional [int ] = None ) -> None :
120
+ self ._name = name
121
+ if timestamp is None :
122
+ self ._timestamp = time_ns ()
123
+ else :
124
+ self ._timestamp = timestamp
125
+
126
+ @property
127
+ def name (self ) -> str :
128
+ return self ._name
129
+
130
+ @property
131
+ def timestamp (self ) -> int :
132
+ return self ._timestamp
133
+
134
+ @property
135
+ @abc .abstractmethod
136
+ def attributes (self ) -> types .Attributes :
137
+ pass
138
+
139
+
140
+ class Event (EventBase ):
141
+ """A text annotation with a set of attributes.
142
+
143
+ Args:
144
+ name: Name of the event.
145
+ attributes: Attributes of the event.
146
+ timestamp: Timestamp of the event. If `None` it will filled
147
+ automatically.
148
+ """
149
+
150
+ def __init__ (
151
+ self ,
152
+ name : str ,
153
+ attributes : types .Attributes = None ,
154
+ timestamp : Optional [int ] = None ,
155
+ ) -> None :
156
+ super ().__init__ (name , timestamp )
157
+ self ._attributes = attributes
158
+
159
+ @property
160
+ def attributes (self ) -> types .Attributes :
161
+ return self ._attributes
162
+
163
+
164
+ class LazyEvent (EventBase ):
165
+ """A text annotation with a set of attributes.
166
+
167
+ Args:
168
+ name: Name of the event.
169
+ event_formatter: Callable object that returns the attributes of the
170
+ event.
171
+ timestamp: Timestamp of the event. If `None` it will filled
172
+ automatically.
173
+ """
174
+
175
+ def __init__ (
176
+ self ,
177
+ name : str ,
178
+ event_formatter : types .AttributesFormatter ,
179
+ timestamp : Optional [int ] = None ,
180
+ ) -> None :
181
+ super ().__init__ (name , timestamp )
182
+ self ._event_formatter = event_formatter
183
+
184
+ @property
185
+ def attributes (self ) -> types .Attributes :
186
+ return self ._event_formatter ()
187
+
188
+
117
189
class Span (trace_api .Span ):
118
190
"""See `opentelemetry.trace.Span`.
119
191
@@ -149,7 +221,7 @@ def __init__(
149
221
trace_config : None = None , # TODO
150
222
resource : None = None ,
151
223
attributes : types .Attributes = None , # TODO
152
- events : Sequence [trace_api . Event ] = None , # TODO
224
+ events : Sequence [Event ] = None , # TODO
153
225
links : Sequence [trace_api .Link ] = (),
154
226
kind : trace_api .SpanKind = trace_api .SpanKind .INTERNAL ,
155
227
span_processor : SpanProcessor = SpanProcessor (),
@@ -266,21 +338,7 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]:
266
338
return "different type"
267
339
return None
268
340
269
- def add_event (
270
- self ,
271
- name : str ,
272
- attributes : types .Attributes = None ,
273
- timestamp : Optional [int ] = None ,
274
- ) -> None :
275
- self .add_lazy_event (
276
- trace_api .Event (
277
- name ,
278
- Span ._empty_attributes if attributes is None else attributes ,
279
- time_ns () if timestamp is None else timestamp ,
280
- )
281
- )
282
-
283
- def add_lazy_event (self , event : trace_api .Event ) -> None :
341
+ def _add_event (self , event : EventBase ) -> None :
284
342
with self ._lock :
285
343
if not self .is_recording_events ():
286
344
return
@@ -293,6 +351,36 @@ def add_lazy_event(self, event: trace_api.Event) -> None:
293
351
return
294
352
self .events .append (event )
295
353
354
+ def add_event (
355
+ self ,
356
+ name : str ,
357
+ attributes : types .Attributes = None ,
358
+ timestamp : Optional [int ] = None ,
359
+ ) -> None :
360
+ if attributes is None :
361
+ attributes = Span ._empty_attributes
362
+ self ._add_event (
363
+ Event (
364
+ name = name ,
365
+ attributes = attributes ,
366
+ timestamp = time_ns () if timestamp is None else timestamp ,
367
+ )
368
+ )
369
+
370
+ def add_lazy_event (
371
+ self ,
372
+ name : str ,
373
+ event_formatter : types .AttributesFormatter ,
374
+ timestamp : Optional [int ] = None ,
375
+ ) -> None :
376
+ self ._add_event (
377
+ LazyEvent (
378
+ name = name ,
379
+ event_formatter = event_formatter ,
380
+ timestamp = time_ns () if timestamp is None else timestamp ,
381
+ )
382
+ )
383
+
296
384
def start (self , start_time : Optional [int ] = None ) -> None :
297
385
with self ._lock :
298
386
if not self .is_recording_events ():
0 commit comments