1010from sentry_sdk ._compat import with_metaclass
1111from sentry_sdk .scope import Scope
1212from sentry_sdk .client import Client
13- from sentry_sdk .tracing import Span , maybe_create_breadcrumbs_from_span
13+ from sentry_sdk .tracing import Span
1414from sentry_sdk .utils import (
1515 exc_info_from_error ,
1616 event_from_exception ,
@@ -128,17 +128,6 @@ def main(self):
128128 return GLOBAL_HUB
129129
130130
131- class _HubManager (object ):
132- def __init__ (self , hub ):
133- # type: (Hub) -> None
134- self ._old = Hub .current
135- _local .set (hub )
136-
137- def __exit__ (self , exc_type , exc_value , tb ):
138- # type: (Any, Any, Any) -> None
139- _local .set (self ._old )
140-
141-
142131class _ScopeManager (object ):
143132 def __init__ (self , hub ):
144133 # type: (Hub) -> None
@@ -429,44 +418,27 @@ def add_breadcrumb(
429418 while len (scope ._breadcrumbs ) > max_breadcrumbs :
430419 scope ._breadcrumbs .popleft ()
431420
432- @contextmanager
433- def span (
434- self ,
435- span = None , # type: Optional[Span]
436- ** kwargs # type: Any
437- ):
438- # type: (...) -> Generator[Span, None, None]
439- # TODO: Document
440- span = self .start_span (span = span , ** kwargs )
441-
442- _ , scope = self ._stack [- 1 ]
443- old_span = scope .span
444- scope .span = span
445-
446- try :
447- yield span
448- except Exception :
449- span .set_failure ()
450- raise
451- finally :
452- try :
453- span .finish ()
454- maybe_create_breadcrumbs_from_span (self , span )
455- self .finish_span (span )
456- except Exception :
457- self ._capture_internal_exception (sys .exc_info ())
458- scope .span = old_span
459-
460421 def start_span (
461422 self ,
462423 span = None , # type: Optional[Span]
463424 ** kwargs # type: Any
464425 ):
465426 # type: (...) -> Span
466- # TODO: Document
427+ """
428+ Create a new span whose parent span is the currently active
429+ span, if any. The return value is the span object that can
430+ be used as a context manager to start and stop timing.
431+
432+ Note that you will not see any span that is not contained
433+ within a transaction. Create a transaction with
434+ ``start_span(transaction="my transaction")`` if an
435+ integration doesn't already do this for you.
436+ """
467437
468438 client , scope = self ._stack [- 1 ]
469439
440+ kwargs .setdefault ("hub" , self )
441+
470442 if span is None :
471443 if scope .span is not None :
472444 span = scope .span .new_span (** kwargs )
@@ -482,48 +454,6 @@ def start_span(
482454
483455 return span
484456
485- def finish_span (
486- self , span # type: Span
487- ):
488- # type: (...) -> Optional[str]
489- # TODO: Document
490- if span .timestamp is None :
491- # This transaction is not yet finished so we just finish it.
492- span .finish ()
493-
494- if span .transaction is None :
495- # If this has no transaction set we assume there's a parent
496- # transaction for this span that would be flushed out eventually.
497- return None
498-
499- if self .client is None :
500- # We have no client and therefore nowhere to send this transaction
501- # event.
502- return None
503-
504- if not span .sampled :
505- # At this point a `sampled = None` should have already been
506- # resolved to a concrete decision. If `sampled` is `None`, it's
507- # likely that somebody used `with Hub.span(..)` on a
508- # non-transaction span and later decided to make it a transaction.
509- assert (
510- span .sampled is not None
511- ), "Need to set transaction when entering span!"
512- return None
513-
514- return self .capture_event (
515- {
516- "type" : "transaction" ,
517- "transaction" : span .transaction ,
518- "contexts" : {"trace" : span .get_trace_context ()},
519- "timestamp" : span .timestamp ,
520- "start_timestamp" : span .start_timestamp ,
521- "spans" : [
522- s .to_json () for s in (span ._finished_spans or ()) if s is not span
523- ],
524- }
525- )
526-
527457 @overload # noqa
528458 def push_scope (
529459 self , callback = None # type: Optional[None]
0 commit comments