def __enter__(self): transaction = get_transaction() if transaction and transaction.is_sampled: return transaction.begin_span(self.name, self.type, context=self.extra, leaf=self.leaf)
def __exit__(self, exc_type, exc_val, exc_tb): transaction = get_transaction() if transaction and transaction.is_sampled: try: transaction.end_span(self.skip_frames) except LookupError: error_logger.info("ended non-existing span %s of type %s", self.name, self.type)
def set_context(data, key="custom"): transaction = get_transaction() if not transaction: return if callable(data) and transaction.is_sampled: data = data() if key in transaction.context: transaction.context[key].update(data) else: transaction.context[key] = data
def end_transaction(self, result=None, transaction_name=None): transaction = get_transaction(clear=True) if transaction: transaction.end_transaction() if transaction.name is None: transaction.name = transaction_name if transaction_name is not None else "" if self._should_ignore(transaction.name): return if transaction.result is None: transaction.result = result self.queue_func(TRANSACTION, transaction.to_dict()) return transaction
def tag(**tags): """ Tags current transaction. Both key and value of the tag should be strings. """ transaction = get_transaction() for name, value in tags.items(): if not transaction: error_logger.warning("Ignored tag %s. No transaction currently active.", name) return if TAG_RE.match(name): transaction.tags[compat.text_type(name)] = encoding.keyword_field(compat.text_type(value)) else: error_logger.warning("Ignored tag %s. Tag names can't contain stars, dots or double quotes.", name)
def tag(**tags): """ Tags current transaction. Both key and value of the tag should be strings. """ transaction = get_transaction() for name, value in tags.items(): if not transaction: error_logger.warning( "Ignored tag %s. No transaction currently active.", name) return # replace invalid characters for Elasticsearch field names with underscores name = TAG_RE.sub("_", compat.text_type(name)) transaction.tags[compat.text_type(name)] = encoding.keyword_field( compat.text_type(value))
def set_context(data, key="custom"): transaction = get_transaction() if not transaction: return if callable(data) and transaction.is_sampled: data = data() # remove invalid characters from key names if not callable( data ): # if transaction wasn't sampled, data is still a callable here and can be ignored for k in list(data.keys()): if TAG_RE.search(k): data[TAG_RE.sub("_", k)] = data.pop(k) if key in transaction.context: transaction.context[key].update(data) else: transaction.context[key] = data
def set_transaction_result(result, override=True): transaction = get_transaction() if not transaction: return if transaction.result is None or override: transaction.result = result
def set_transaction_name(name, override=True): transaction = get_transaction() if not transaction: return if transaction.name is None or override: transaction.name = name