Skip to content

Commit 3d8f4fa

Browse files
sdk/trace/span: expose members using properties
Given that we are passing the whole Span directly to the exporters, expose members using properties to avoid undesired updates.
1 parent 139e960 commit 3d8f4fa

File tree

1 file changed

+64
-35
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/trace

1 file changed

+64
-35
lines changed

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

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -246,82 +246,111 @@ def __init__(
246246
span_processor: SpanProcessor = SpanProcessor(),
247247
) -> None:
248248

249-
self.name = name
250-
self.context = context
251-
self.parent = parent
252-
self.sampler = sampler
253-
self.trace_config = trace_config
254-
self.resource = resource
255-
self.attributes = attributes
256-
self.events = events
257-
self.links = links
258-
self.span_processor = span_processor
249+
self._name = name
250+
self._context = context
251+
self._parent = parent
252+
self._sampler = sampler
253+
self._trace_config = trace_config
254+
self._resource = resource
255+
self._attributes = attributes
256+
self._events = events
257+
self._links = links
258+
self._span_processor = span_processor
259259

260260
if attributes is None:
261-
self.attributes = Span.empty_attributes
261+
self._attributes = Span.empty_attributes
262262
else:
263-
self.attributes = BoundedDict.from_map(
263+
self._attributes = BoundedDict.from_map(
264264
MAX_NUM_ATTRIBUTES, attributes
265265
)
266266

267267
if events is None:
268-
self.events = Span.empty_events
268+
self._events = Span.empty_events
269269
else:
270-
self.events = BoundedList.from_seq(MAX_NUM_EVENTS, events)
270+
self._events = BoundedList.from_seq(MAX_NUM_EVENTS, events)
271271

272272
if links is None:
273-
self.links = Span.empty_links
273+
self._links = Span.empty_links
274274
else:
275-
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)
275+
self._links = BoundedList.from_seq(MAX_NUM_LINKS, links)
276276

277-
self.end_time = None
278-
self.start_time = None
277+
self._end_time = None
278+
self._start_time = None
279279

280280
def __repr__(self):
281281
return '{}(name="{}")'.format(type(self).__name__, self.name)
282282

283283
def get_context(self):
284-
return self.context
284+
return self._context
285285

286286
def set_attribute(
287287
self: "Span", key: str, value: types.AttributeValue
288288
) -> None:
289-
if self.attributes is Span.empty_attributes:
290-
self.attributes = BoundedDict(MAX_NUM_ATTRIBUTES)
291-
self.attributes[key] = value
289+
if self._attributes is Span.empty_attributes:
290+
self._attributes = BoundedDict(MAX_NUM_ATTRIBUTES)
291+
self._attributes[key] = value
292292

293293
def add_event(
294294
self: "Span", name: str, attributes: types.Attributes = None
295295
) -> None:
296-
if self.events is Span.empty_events:
297-
self.events = BoundedList(MAX_NUM_EVENTS)
296+
if self._events is Span.empty_events:
297+
self._events = BoundedList(MAX_NUM_EVENTS)
298298
if attributes is None:
299299
attributes = Span.empty_attributes
300-
self.events.append(Event(name, attributes))
300+
self._events.append(Event(name, attributes))
301301

302302
def add_link(
303303
self: "Span",
304304
link_target_context: "trace_api.SpanContext",
305305
attributes: types.Attributes = None,
306306
) -> None:
307-
if self.links is Span.empty_links:
308-
self.links = BoundedList(MAX_NUM_LINKS)
307+
if self._links is Span.empty_links:
308+
self._links = BoundedList(MAX_NUM_LINKS)
309309
if attributes is None:
310310
attributes = Span.empty_attributes
311-
self.links.append(Link(link_target_context, attributes))
311+
self._links.append(Link(link_target_context, attributes))
312312

313313
def start(self):
314-
if self.start_time is None:
315-
self.start_time = util.time_ns()
316-
self.span_processor.on_start(self)
314+
if self._start_time is None:
315+
self._start_time = util.time_ns()
316+
self._span_processor.on_start(self)
317317

318318
def end(self):
319-
if self.end_time is None:
320-
self.end_time = util.time_ns()
321-
self.span_processor.on_end(self)
319+
if self._end_time is None:
320+
self._end_time = util.time_ns()
321+
self._span_processor.on_end(self)
322322

323323
def update_name(self, name: str) -> None:
324-
self.name = name
324+
self._name = name
325+
326+
# accessors
327+
@property
328+
def events(self) -> Event:
329+
return self._events
330+
331+
@property
332+
def attributes(self) -> types.Attributes:
333+
return self._attributes
334+
335+
@property
336+
def links(self) -> Link:
337+
return self._links
338+
339+
@property
340+
def name(self) -> str:
341+
return self._name
342+
343+
@property
344+
def start_time(self) -> int:
345+
return self._start_time
346+
347+
@property
348+
def end_time(self) -> int:
349+
return self._end_time
350+
351+
@property
352+
def parent(self) -> trace_api.ParentSpan:
353+
return self._parent
325354

326355

327356
def generate_span_id():

0 commit comments

Comments
 (0)