1313# limitations under the License.
1414
1515
16+ import abc
1617import atexit
1718import logging
1819import random
@@ -114,6 +115,77 @@ def shutdown(self) -> None:
114115 sp .shutdown ()
115116
116117
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+
117189class Span (trace_api .Span ):
118190 """See `opentelemetry.trace.Span`.
119191
@@ -149,7 +221,7 @@ def __init__(
149221 trace_config : None = None , # TODO
150222 resource : None = None ,
151223 attributes : types .Attributes = None , # TODO
152- events : Sequence [trace_api . Event ] = None , # TODO
224+ events : Sequence [Event ] = None , # TODO
153225 links : Sequence [trace_api .Link ] = (),
154226 kind : trace_api .SpanKind = trace_api .SpanKind .INTERNAL ,
155227 span_processor : SpanProcessor = SpanProcessor (),
@@ -266,21 +338,7 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]:
266338 return "different type"
267339 return None
268340
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 :
284342 with self ._lock :
285343 if not self .is_recording_events ():
286344 return
@@ -293,6 +351,36 @@ def add_lazy_event(self, event: trace_api.Event) -> None:
293351 return
294352 self .events .append (event )
295353
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+
296384 def start (self , start_time : Optional [int ] = None ) -> None :
297385 with self ._lock :
298386 if not self .is_recording_events ():
0 commit comments