def from_headers( cls, headers: Optional[Mapping], app: Optional['ipapp.app.BaseApplication'] = None, ) -> 'Span': if headers is not None: headers = {k.lower(): v for k, v in headers.items()} else: headers = {} sampled = azh.parse_sampled_header(headers) if azh.TRACE_ID_HEADER.lower() not in headers: span = cls.new() else: if app is None: app = misc.ctx_app_get() if app is None: # pragma: no cover raise UserWarning span = cls( logger=app.logger, trace_id=headers.get(azh.TRACE_ID_HEADER.lower()), id=azu.generate_random_64bit_string(), parent_id=headers.get(azh.SPAN_ID_HEADER.lower()), ) if sampled is not None and not sampled: span.skip() return span
def test_generate_random_64bit_string(rand): rand.return_value = b'17133d482ba4f605' random_string = utils.generate_random_64bit_string() assert random_string == '17133d482ba4f605' # This acts as a contract test of sorts. This should return a str # in both py2 and py3. IOW, no unicode objects. assert isinstance(random_string, str)
def test_generate_random_64bit_string(rand: mock.Mock) -> None: rand.return_value = 0x17133D482BA4F605 random_string = utils.generate_random_64bit_string() assert random_string == "17133d482ba4f605" # This acts as a contract test of sorts. This should return a str # in both py2 and py3. IOW, no unicode objects. assert isinstance(random_string, str)
def __init__( self, logger: Optional['ipapp.logger.Logger'], trace_id: str, id: Optional[str] = None, parent_id: Optional[str] = None, parent: Optional['Span'] = None, ) -> None: self.logger = logger self.trace_id = trace_id if id is None: self.id = azu.generate_random_64bit_string() else: self.id = id self.parent = parent self.parent_id: Optional[str] = parent_id if parent is not None and self.parent_id is None: self.parent_id = parent.id self._skip = False self._children: List['Span'] = [] self._name: str = '' self._name4adapter: Dict[str, str] = {} self._kind: Optional[str] = None self._annotations: Dict[str, List[Tuple[str, float]]] = {} self._annotations4adapter: Dict[str, Dict[str, List[Tuple[str, float]]]] = {} self._tags: Dict[str, str] = {} self._tags4adapter: Dict[str, Dict[str, str]] = {} self._start_stamp: Optional[float] = None self._finish_stamp: Optional[float] = None self._exception: Optional[BaseException] = None self._is_handled = False self._ctx_token: Optional[Token] = None
def create_trace_id(logger: Optional['ipapp.app.Logger']) -> str: if not logger: return azu.generate_random_128bit_string() zipkin_adapter = logger.adapters.get(logger.ADAPTER_ZIPKIN) if not zipkin_adapter: return azu.generate_random_128bit_string() adapter_config = zipkin_adapter.cfg if adapter_config.use_64bit_trace_id: # type: ignore trace_id = azu.generate_random_64bit_string() else: trace_id = azu.generate_random_128bit_string() return trace_id
def new( cls, name: Optional[str] = None, kind: Optional[str] = None, app: Optional['ipapp.app.BaseApplication'] = None, ) -> 'Span': if app is None: app = misc.ctx_app_get() if app is None: # pragma: no cover raise UserWarning span = cls( logger=app.logger, id=azu.generate_random_64bit_string(), ) if name is not None: span.name = name if kind: span.kind = kind return span
def new_child( self, name: Optional[str] = None, kind: Optional[str] = None, cls: Optional[Type['Span']] = None, ) -> 'Span': if cls is None: cls = Span span = cls( logger=self.logger, trace_id=self.trace_id, id=azu.generate_random_64bit_string(), parent=self, ) if self._skip: span.skip() if name is not None: span.name = name if kind: span.kind = kind self._children.append(span) return span