def __init__( self, name: str, context: trace_api.SpanContext, parent: trace_api.ParentSpan = None, sampler: Optional[sampling.Sampler] = None, trace_config: None = None, # TODO resource: None = None, attributes: types.Attributes = None, # TODO events: Sequence[Event] = None, # TODO links: Sequence[trace_api.Link] = (), kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, span_processor: SpanProcessor = SpanProcessor(), instrumentation_info: InstrumentationInfo = None, set_status_on_exception: bool = True, ) -> None: self.name = name self.context = context self.parent = parent self.sampler = sampler self.trace_config = trace_config self.resource = resource self.kind = kind self._set_status_on_exception = set_status_on_exception self.span_processor = span_processor self.status = None self._lock = threading.Lock() if attributes is None: self.attributes = Span._empty_attributes else: self.attributes = BoundedDict.from_map( MAX_NUM_ATTRIBUTES, attributes ) if events is None: self.events = Span._empty_events else: self.events = BoundedList.from_seq(MAX_NUM_EVENTS, events) if links is None: self.links = Span._empty_links else: self.links = BoundedList.from_seq(MAX_NUM_LINKS, links) self._end_time = None # type: Optional[int] self._start_time = None # type: Optional[int] self.instrumentation_info = instrumentation_info
def __init__( self, name: str, context: trace_api.SpanContext, parent: Optional[trace_api.SpanContext] = None, sampler: Optional[sampling.Sampler] = None, trace_config: None = None, # TODO resource: Resource = Resource.create({}), attributes: types.Attributes = None, events: Sequence[Event] = None, links: Sequence[trace_api.Link] = (), kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, span_processor: SpanProcessor = SpanProcessor(), instrumentation_info: InstrumentationInfo = None, record_exception: bool = True, set_status_on_exception: bool = True, limits=_UnsetLimits, ) -> None: super().__init__( name=name, context=context, parent=parent, kind=kind, resource=resource, instrumentation_info=instrumentation_info, ) self._sampler = sampler self._trace_config = trace_config self._record_exception = record_exception self._set_status_on_exception = set_status_on_exception self._span_processor = span_processor self._limits = limits self._lock = threading.Lock() self._attributes = BoundedAttributes( self._limits.max_span_attributes, attributes, immutable=False, max_value_len=self._limits.max_span_attribute_length, ) self._events = self._new_events() if events: for event in events: event._attributes = BoundedAttributes( self._limits.max_event_attributes, event.attributes, max_value_len=self._limits.max_attribute_length, ) self._events.append(event) if links is None: self._links = self._new_links() else: for link in links: link._attributes = BoundedAttributes( self._limits.max_link_attributes, link.attributes, max_value_len=self._limits.max_attribute_length, ) self._links = BoundedList.from_seq(self._limits.max_links, links)
def __init__( self, name: str, context: trace_api.SpanContext, parent: Optional[trace_api.SpanContext] = None, sampler: Optional[sampling.Sampler] = None, trace_config: None = None, # TODO resource: Resource = Resource.create({}), attributes: types.Attributes = None, events: Sequence[Event] = None, links: Sequence[trace_api.Link] = (), kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, span_processor: SpanProcessor = SpanProcessor(), instrumentation_info: InstrumentationInfo = None, record_exception: bool = True, set_status_on_exception: bool = True, ) -> None: self.name = name self.context = context self.parent = parent self.sampler = sampler self.trace_config = trace_config self.resource = resource self.kind = kind self._record_exception = record_exception self._set_status_on_exception = set_status_on_exception self.span_processor = span_processor self.status = Status(StatusCode.UNSET) self._lock = threading.Lock() _filter_attribute_values(attributes) if not attributes: self.attributes = self._new_attributes() else: self.attributes = BoundedDict.from_map( SPAN_ATTRIBUTE_COUNT_LIMIT, attributes ) self.events = self._new_events() if events: for event in events: _filter_attribute_values(event.attributes) # pylint: disable=protected-access event._attributes = _create_immutable_attributes( event.attributes ) self.events.append(event) if links is None: self.links = self._new_links() else: self.links = BoundedList.from_seq(SPAN_LINK_COUNT_LIMIT, links) self._end_time = None # type: Optional[int] self._start_time = None # type: Optional[int] self.instrumentation_info = instrumentation_info
def test_from_seq(self): list_len = len(self.base) base_copy = list(self.base) blist = BoundedList.from_seq(list_len, base_copy) self.assertEqual(len(blist), list_len) # modify base_copy and test that blist is not changed for idx in range(list_len): base_copy[idx] = idx * base_copy[idx] for idx in range(list_len): self.assertEqual(blist[idx], self.base[idx]) # test that iter yields the correct number of elements self.assertEqual(len(tuple(blist)), list_len) # sequence too big with self.assertRaises(ValueError): BoundedList.from_seq(list_len / 2, self.base)
def test_extend_drop(self): list_len = len(self.base) # create full BoundedList blist = BoundedList.from_seq(list_len, self.base) other_list = [13, 37, 51, 91] # try to extend with more elements blist.extend(other_list) self.assertEqual(len(blist), list_len) self.assertEqual(blist.dropped, len(other_list))
def test_append_drop(self): """Append more than max capacity elements and test that oldest ones are dropped.""" list_len = len(self.base) # create full BoundedList blist = BoundedList.from_seq(list_len, self.base) # try to append more items for val in self.base: # should drop the element without raising exceptions blist.append(2 * val) self.assertEqual(len(blist), list_len) self.assertEqual(blist.dropped, list_len) # test that new elements are in the list for idx in range(list_len): self.assertEqual(blist[idx], 2 * self.base[idx])