Skip to content

Commit a339e7d

Browse files
add Event class
Make Event a class and implement add_lazy_event
1 parent 96b7567 commit a339e7d

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

opentelemetry-api/src/opentelemetry/trace/__init__.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ def attributes(self) -> types.Attributes:
8888
return self._attributes
8989

9090

91+
class Event:
92+
"""A text annotation with a set of attributes."""
93+
94+
def __init__(
95+
self, name: str, timestamp: int, attributes: types.Attributes = None
96+
) -> None:
97+
self._name = name
98+
self._attributes = attributes
99+
self._timestamp = timestamp
100+
101+
@property
102+
def name(self) -> str:
103+
return self._name
104+
105+
@property
106+
def attributes(self) -> types.Attributes:
107+
return self._attributes
108+
109+
@property
110+
def timestamp(self) -> int:
111+
return self._timestamp
112+
113+
91114
class Span:
92115
"""A span represents a single operation within a trace."""
93116

@@ -129,12 +152,18 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
129152
def add_event(
130153
self, name: str, attributes: types.Attributes = None
131154
) -> None:
132-
"""Adds an Event.
155+
"""Adds an `Event`.
133156
134-
Adds a single Event with the name and, optionally, attributes passed
157+
Adds a single `Event` with the name and, optionally, attributes passed
135158
as arguments.
136159
"""
137160

161+
def add_lazy_event(self, event: Event) -> None:
162+
"""Adds an `Event`.
163+
164+
Adds an `Event` that has previously been created.
165+
"""
166+
138167
def add_link(
139168
self,
140169
link_target_context: "SpanContext",

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import random
1717
import threading
1818
import typing
19-
from collections import OrderedDict, deque, namedtuple
19+
from collections import OrderedDict, deque
2020
from contextlib import contextmanager
2121

2222
from opentelemetry import trace as trace_api
@@ -140,9 +140,6 @@ def from_map(cls, maxlen, mapping):
140140
return bounded_dict
141141

142142

143-
Event = namedtuple("Event", ("name", "attributes"))
144-
145-
146143
class SpanProcessor:
147144
"""Interface which allows hooks for SDK's `Span`s start and end method
148145
invocations.
@@ -239,7 +236,7 @@ def __init__(
239236
trace_config=None, # TODO
240237
resource=None, # TODO
241238
attributes: types.Attributes = None, # TODO
242-
events: typing.Sequence[Event] = None, # TODO
239+
events: typing.Sequence[trace_api.Event] = None, # TODO
243240
links: typing.Sequence[trace_api.Link] = None, # TODO
244241
span_processor: SpanProcessor = SpanProcessor(),
245242
) -> None:
@@ -289,11 +286,14 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
289286
def add_event(
290287
self, name: str, attributes: types.Attributes = None
291288
) -> None:
292-
if self.events is Span.empty_events:
293-
self.events = BoundedList(MAX_NUM_EVENTS)
294289
if attributes is None:
295290
attributes = Span.empty_attributes
296-
self.events.append(Event(name, attributes))
291+
self.add_lazy_event(trace_api.Event(name, util.time_ns(), attributes))
292+
293+
def add_lazy_event(self, event: trace_api.Event) -> None:
294+
if self.events is Span.empty_events:
295+
self.events = BoundedList(MAX_NUM_EVENTS)
296+
self.events.append(event)
297297

298298
def add_link(
299299
self,

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from opentelemetry import trace as trace_api
1919
from opentelemetry.sdk import trace
20+
from opentelemetry.sdk import util
2021

2122

2223
class TestTracer(unittest.TestCase):
@@ -164,16 +165,23 @@ def test_span_members(self):
164165
# events
165166
root.add_event("event0")
166167
root.add_event("event1", {"name": "birthday"})
167-
168-
self.assertEqual(len(root.events), 2)
169-
self.assertEqual(
170-
root.events[0], trace.Event(name="event0", attributes={})
171-
)
172-
self.assertEqual(
173-
root.events[1],
174-
trace.Event(name="event1", attributes={"name": "birthday"}),
168+
now = util.time_ns()
169+
root.add_lazy_event(
170+
trace_api.Event("event2", now, {"name": "hello"})
175171
)
176172

173+
self.assertEqual(len(root.events), 3)
174+
175+
self.assertEqual(root.events[0].name, "event0")
176+
self.assertEqual(root.events[0].attributes, {})
177+
178+
self.assertEqual(root.events[1].name, "event1")
179+
self.assertEqual(root.events[1].attributes, {"name": "birthday"})
180+
181+
self.assertEqual(root.events[2].name, "event2")
182+
self.assertEqual(root.events[2].attributes, {"name": "hello"})
183+
self.assertEqual(root.events[2].timestamp, now)
184+
177185
# links
178186
root.add_link(other_context1)
179187
root.add_link(other_context2, {"name": "neighbor"})

0 commit comments

Comments
 (0)