def start_transaction( self, transaction=None, # type: Optional[Transaction] **kwargs # type: Any ): # type: (...) -> Transaction """ Start and return a transaction. Start an existing transaction if given, otherwise create and start a new transaction with kwargs. This is the entry point to manual tracing instrumentation. A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a new child span within the transaction or any span, call the respective `.start_child()` method. Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded. When used as context managers, spans and transactions are automatically finished at the end of the `with` block. If not using context managers, call the `.finish()` method. When the transaction is finished, it will be sent to Sentry with all its finished child spans. """ custom_sampling_context = kwargs.pop("custom_sampling_context", {}) # if we haven't been given a transaction, make one if transaction is None: kwargs.setdefault("hub", self) transaction = Transaction(**kwargs) # use traces_sample_rate, traces_sampler, and/or inheritance to make a # sampling decision sampling_context = { "transaction_context": transaction.to_json(), "parent_sampled": transaction.parent_sampled, } sampling_context.update(custom_sampling_context) transaction._set_initial_sampling_decision( sampling_context=sampling_context) # we don't bother to keep spans if we already know we're not going to # send the transaction if transaction.sampled: max_spans = ( self.client and self.client.options["_experiments"].get("max_spans")) or 1000 transaction.init_span_recorder(maxlen=max_spans) return transaction
def start_transaction( self, transaction=None, # type: Optional[Transaction] **kwargs # type: Any ): # type: (...) -> Transaction """ Start and return a transaction. Start an existing transaction if given, otherwise create and start a new transaction with kwargs. This is the entry point to manual tracing instrumentation. A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a new child span within the transaction or any span, call the respective `.start_child()` method. Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded. When used as context managers, spans and transactions are automatically finished at the end of the `with` block. If not using context managers, call the `.finish()` method. When the transaction is finished, it will be sent to Sentry with all its finished child spans. """ if transaction is None: kwargs.setdefault("hub", self) transaction = Transaction(**kwargs) client, scope = self._stack[-1] if transaction.sampled is None: sample_rate = client and client.options["traces_sample_rate"] or 0 transaction.sampled = random.random() < sample_rate if transaction.sampled: max_spans = ( client and client.options["_experiments"].get("max_spans") or 1000 ) transaction.init_span_recorder(maxlen=max_spans) return transaction