Skip to content

Commit

Permalink
Lazy init for span attrs, events, and links
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t committed Jul 23, 2019
1 parent 4eade54 commit 87cac92
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def is_valid(self) -> bool:


class Span(trace_api.Span):

# Initialize these lazily assuming most spans won't have them.
empty_attributes = BoundedList(MAX_NUM_ATTRIBUTES)
empty_events = BoundedDict(MAX_NUM_EVENTS)
empty_links = BoundedDict(MAX_NUM_LINKS)

def __init__(self: 'Span',
name: str,
context: 'SpanContext',
Expand Down Expand Up @@ -176,18 +182,18 @@ def __init__(self: 'Span',
self.links = links

if attributes is None:
self.attributes = BoundedDict(MAX_NUM_ATTRIBUTES)
self.attributes = Span.empty_attributes
else:
self.attributes = BoundedDict.from_map(
MAX_NUM_ATTRIBUTES, attributes)

if events is None:
self.events = BoundedList(MAX_NUM_EVENTS)
self.events = Span.empty_events
else:
self.events = BoundedList.from_seq(MAX_NUM_EVENTS, events)

if links is None:
self.links = BoundedList(MAX_NUM_LINKS)
self.links = Span.empty_links
else:
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)

Expand All @@ -198,18 +204,24 @@ def set_attribute(self: 'Span',
key: str,
value: 'AttributeValue'
) -> None:
if self.attributes is Span.empty_attributes:
self.attributes = BoundedDict(MAX_NUM_ATTRIBUTES)
self.attributes[key] = value

def add_event(self: 'Span',
name: str,
attributes: typing.Dict[str, 'AttributeValue']
) -> None:
if self.events is Span.empty_events:
self.events = BoundedList(MAX_NUM_EVENTS)
self.events.append(Event(name, attributes))

def add_link(self: 'Span',
context: 'SpanContext',
attributes: typing.Dict[str, 'AttributeValue'],
) -> None:
if self.links is Span.empty_links:
self.links = BoundedList(MAX_NUM_LINKS)
self.links.append(Link(context, attributes))

def start(self):
Expand Down

0 comments on commit 87cac92

Please sign in to comment.