Skip to content

Commit

Permalink
add Event class
Browse files Browse the repository at this point in the history
Make Event a class and implement add_lazy_event
  • Loading branch information
mauriciovasquezbernal committed Sep 10, 2019
1 parent 96b7567 commit a339e7d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
33 changes: 31 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ def attributes(self) -> types.Attributes:
return self._attributes


class Event:
"""A text annotation with a set of attributes."""

def __init__(
self, name: str, timestamp: int, attributes: types.Attributes = None
) -> None:
self._name = name
self._attributes = attributes
self._timestamp = timestamp

@property
def name(self) -> str:
return self._name

@property
def attributes(self) -> types.Attributes:
return self._attributes

@property
def timestamp(self) -> int:
return self._timestamp


class Span:
"""A span represents a single operation within a trace."""

Expand Down Expand Up @@ -129,12 +152,18 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
def add_event(
self, name: str, attributes: types.Attributes = None
) -> None:
"""Adds an Event.
"""Adds an `Event`.
Adds a single Event with the name and, optionally, attributes passed
Adds a single `Event` with the name and, optionally, attributes passed
as arguments.
"""

def add_lazy_event(self, event: Event) -> None:
"""Adds an `Event`.
Adds an `Event` that has previously been created.
"""

def add_link(
self,
link_target_context: "SpanContext",
Expand Down
16 changes: 8 additions & 8 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import random
import threading
import typing
from collections import OrderedDict, deque, namedtuple
from collections import OrderedDict, deque
from contextlib import contextmanager

from opentelemetry import trace as trace_api
Expand Down Expand Up @@ -140,9 +140,6 @@ def from_map(cls, maxlen, mapping):
return bounded_dict


Event = namedtuple("Event", ("name", "attributes"))


class SpanProcessor:
"""Interface which allows hooks for SDK's `Span`s start and end method
invocations.
Expand Down Expand Up @@ -239,7 +236,7 @@ def __init__(
trace_config=None, # TODO
resource=None, # TODO
attributes: types.Attributes = None, # TODO
events: typing.Sequence[Event] = None, # TODO
events: typing.Sequence[trace_api.Event] = None, # TODO
links: typing.Sequence[trace_api.Link] = None, # TODO
span_processor: SpanProcessor = SpanProcessor(),
) -> None:
Expand Down Expand Up @@ -289,11 +286,14 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
def add_event(
self, name: str, attributes: types.Attributes = None
) -> None:
if self.events is Span.empty_events:
self.events = BoundedList(MAX_NUM_EVENTS)
if attributes is None:
attributes = Span.empty_attributes
self.events.append(Event(name, attributes))
self.add_lazy_event(trace_api.Event(name, util.time_ns(), attributes))

def add_lazy_event(self, event: trace_api.Event) -> None:
if self.events is Span.empty_events:
self.events = BoundedList(MAX_NUM_EVENTS)
self.events.append(event)

def add_link(
self,
Expand Down
24 changes: 16 additions & 8 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from opentelemetry import trace as trace_api
from opentelemetry.sdk import trace
from opentelemetry.sdk import util


class TestTracer(unittest.TestCase):
Expand Down Expand Up @@ -164,16 +165,23 @@ def test_span_members(self):
# events
root.add_event("event0")
root.add_event("event1", {"name": "birthday"})

self.assertEqual(len(root.events), 2)
self.assertEqual(
root.events[0], trace.Event(name="event0", attributes={})
)
self.assertEqual(
root.events[1],
trace.Event(name="event1", attributes={"name": "birthday"}),
now = util.time_ns()
root.add_lazy_event(
trace_api.Event("event2", now, {"name": "hello"})
)

self.assertEqual(len(root.events), 3)

self.assertEqual(root.events[0].name, "event0")
self.assertEqual(root.events[0].attributes, {})

self.assertEqual(root.events[1].name, "event1")
self.assertEqual(root.events[1].attributes, {"name": "birthday"})

self.assertEqual(root.events[2].name, "event2")
self.assertEqual(root.events[2].attributes, {"name": "hello"})
self.assertEqual(root.events[2].timestamp, now)

# links
root.add_link(other_context1)
root.add_link(other_context2, {"name": "neighbor"})
Expand Down

0 comments on commit a339e7d

Please sign in to comment.