Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(3) Move tracing related functions from Hub to Scope #2558

Merged
merged 44 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f2a4a3a
Moved get_integration from Hub to Client
antonpirker Nov 29, 2023
4869365
Moved add_breadcrumb from Hub to Scope
antonpirker Nov 29, 2023
e5f9e9d
Moved session functions from Hub to Scope
antonpirker Nov 29, 2023
a338099
Fixed import
antonpirker Nov 29, 2023
d900a1b
Fixed Python 2.7 syntax
antonpirker Nov 29, 2023
0afb3ab
Merge branch 'master' into antonpirker/refactor-hub
antonpirker Nov 30, 2023
fe77d04
Give the client to the scope function. Want to establish a pattern.
antonpirker Dec 1, 2023
4ba7dce
Merge branch 'antonpirker/refactor-hub' of github.com:getsentry/sentr…
antonpirker Dec 1, 2023
b15613a
Moved capture_* functions from Hub to Client
antonpirker Dec 1, 2023
8331bd0
Renamed scope_args to scope_kwargs
antonpirker Dec 1, 2023
ce1759f
oops
antonpirker Dec 1, 2023
9a08b6e
preserve old behavior when called from client directly (not from hub)
antonpirker Dec 1, 2023
f9b8d5a
update test
antonpirker Dec 1, 2023
232ce23
Moved trancing related functions from Hub to Scope
antonpirker Dec 1, 2023
fc95050
Sort imports
antonpirker Dec 1, 2023
b0b3cec
Fixed some tests
antonpirker Dec 4, 2023
dec7dbf
Fixed test
antonpirker Dec 4, 2023
cfd3d54
Trying to fix broken tests because some something was released that b…
antonpirker Dec 4, 2023
ea13b55
fix aiohttp tests
antonpirker Dec 4, 2023
812c791
Merge branch 'master' into antonpirker/refactor-hub-tracing
antonpirker Dec 4, 2023
d233ae5
Merge branch 'master' into antonpirker/refactor-hub-capture
antonpirker Dec 4, 2023
2932adc
Merge branch 'master' into antonpirker/refactor-hub
antonpirker Dec 4, 2023
3a7af73
Merge branch 'antonpirker/refactor-hub' into antonpirker/refactor-hub…
antonpirker Dec 4, 2023
c3baed0
Merge branch 'antonpirker/refactor-hub-capture' into antonpirker/refa…
antonpirker Dec 4, 2023
345c419
Merge branch 'master' into antonpirker/refactor-hub-capture
sentrivana Dec 6, 2023
bb3250a
_update_scope belongs into the scope not the client
antonpirker Dec 11, 2023
3a22dd6
Made top_scope separate parameter
antonpirker Dec 11, 2023
c78b1f5
Hub calls capture_* on scope that calls it on client. Later with new …
antonpirker Dec 13, 2023
0901e72
Merge branch 'feat/new-scopes' into antonpirker/refactor-hub-capture
antonpirker Dec 13, 2023
269fd56
Moved everything to scope and only have capture_event in client
antonpirker Dec 13, 2023
0354065
small fix
antonpirker Dec 13, 2023
3d5825e
Updated test
antonpirker Dec 13, 2023
4e78d1f
Fixed test
antonpirker Dec 13, 2023
ba72216
Fixed test
antonpirker Dec 14, 2023
c5a7ae7
Fixed test for old python
antonpirker Dec 14, 2023
bbb93e6
Formatting
antonpirker Dec 14, 2023
12e632d
Passing the client as separate argument and updating api docs.
antonpirker Dec 14, 2023
eb56a12
ApiDocs
antonpirker Dec 14, 2023
074a9ef
Fix
antonpirker Dec 14, 2023
ac3955b
Update sentry_sdk/scope.py
antonpirker Dec 14, 2023
6733114
Remove condition from _merge_scope (also not present in current imple…
antonpirker Dec 19, 2023
c4ad19a
(4) Move transaction/span related functions from Hub to Scope (#2564)
antonpirker Dec 19, 2023
e8fb2af
Merge branch 'antonpirker/refactor-hub-capture' into antonpirker/refa…
antonpirker Dec 19, 2023
890fc04
Merge branch 'feat/new-scopes' into antonpirker/refactor-hub-tracing
antonpirker Dec 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 28 additions & 141 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
from sentry_sdk.consts import INSTRUMENTER
from sentry_sdk.scope import Scope
from sentry_sdk.client import Client
from sentry_sdk.profiler import Profile
from sentry_sdk.tracing import (
NoOpSpan,
Span,
Transaction,
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
)
from sentry_sdk.tracing_utils import (
has_tracing_enabled,
normalize_incoming_data,
)

from sentry_sdk.utils import (
Expand All @@ -28,18 +21,18 @@
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Union
from typing import Any
from typing import Optional
from typing import Tuple
from typing import Dict
from typing import List
from typing import Callable
from typing import ContextManager
from typing import Dict
from typing import Generator
from typing import List
from typing import Optional
from typing import overload
from typing import Tuple
from typing import Type
from typing import TypeVar
from typing import overload
from typing import ContextManager
from typing import Union

from sentry_sdk.integrations import Integration
from sentry_sdk._types import (
Expand Down Expand Up @@ -447,54 +440,12 @@ def start_span(self, span=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs):

For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
"""
configuration_instrumenter = self.client and self.client.options["instrumenter"]

if instrumenter != configuration_instrumenter:
return NoOpSpan()

# THIS BLOCK IS DEPRECATED
# TODO: consider removing this in a future release.
# This is for backwards compatibility with releases before
# start_transaction existed, to allow for a smoother transition.
if isinstance(span, Transaction) or "transaction" in kwargs:
deprecation_msg = (
"Deprecated: use start_transaction to start transactions and "
"Transaction.start_child to start spans."
)

if isinstance(span, Transaction):
logger.warning(deprecation_msg)
return self.start_transaction(span)

if "transaction" in kwargs:
logger.warning(deprecation_msg)
name = kwargs.pop("transaction")
return self.start_transaction(name=name, **kwargs)

# THIS BLOCK IS DEPRECATED
# We do not pass a span into start_span in our code base, so I deprecate this.
if span is not None:
deprecation_msg = "Deprecated: passing a span into `start_span` is deprecated and will be removed in the future."
logger.warning(deprecation_msg)
return span

kwargs.setdefault("hub", self)

active_span = self.scope.span
if active_span is not None:
new_child_span = active_span.start_child(**kwargs)
return new_child_span
client, scope = self._stack[-1]

# If there is already a trace_id in the propagation context, use it.
# This does not need to be done for `start_child` above because it takes
# the trace_id from the parent span.
if "trace_id" not in kwargs:
traceparent = self.get_traceparent()
trace_id = traceparent.split("-")[0] if traceparent else None
if trace_id is not None:
kwargs["trace_id"] = trace_id
kwargs["hub"] = self
kwargs["client"] = client

return Span(**kwargs)
return scope.start_span(span=span, instrumenter=instrumenter, **kwargs)

def start_transaction(
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
Expand Down Expand Up @@ -524,55 +475,25 @@ def start_transaction(

For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
"""
configuration_instrumenter = self.client and self.client.options["instrumenter"]

if instrumenter != configuration_instrumenter:
return NoOpSpan()

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)

profile = Profile(transaction, hub=self)
profile._set_initial_sampling_decision(sampling_context=sampling_context)
client, scope = self._stack[-1]

# 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)
kwargs["hub"] = self
kwargs["client"] = client

return transaction
return scope.start_transaction(
transaction=transaction, instrumenter=instrumenter, **kwargs
)

def continue_trace(self, environ_or_headers, op=None, name=None, source=None):
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
"""
Sets the propagation context from environment or headers and returns a transaction.
"""
with self.configure_scope() as scope:
scope.generate_propagation_context(environ_or_headers)
scope = self._stack[-1][1]

transaction = Transaction.continue_from_headers(
normalize_incoming_data(environ_or_headers),
op=op,
name=name,
source=source,
return scope.continue_trace(
environ_or_headers=environ_or_headers, op=op, name=name, source=source
)
return transaction

@overload
def push_scope(
Expand Down Expand Up @@ -735,25 +656,16 @@ def get_traceparent(self):
"""
Returns the traceparent either from the active span or from the scope.
"""
if self.client is not None:
if has_tracing_enabled(self.client.options) and self.scope.span is not None:
return self.scope.span.to_traceparent()

return self.scope.get_traceparent()
client, scope = self._stack[-1]
return scope.get_traceparent(client=client)

def get_baggage(self):
# type: () -> Optional[str]
"""
Returns Baggage either from the active span or from the scope.
"""
if (
self.client is not None
and has_tracing_enabled(self.client.options)
and self.scope.span is not None
):
baggage = self.scope.span.to_baggage()
else:
baggage = self.scope.get_baggage()
client, scope = self._stack[-1]
baggage = scope.get_baggage(client=client)

if baggage is not None:
return baggage.serialize()
Expand All @@ -767,19 +679,9 @@ def iter_trace_propagation_headers(self, span=None):
from the span representing the request, if available, or the current
span on the scope if not.
"""
client = self._stack[-1][0]
propagate_traces = client and client.options["propagate_traces"]
if not propagate_traces:
return

span = span or self.scope.span
client, scope = self._stack[-1]

if client and has_tracing_enabled(client.options) and span is not None:
for header in span.iter_headers():
yield header
else:
for header in self.scope.iter_headers():
yield header
return scope.iter_trace_propagation_headers(span=span, client=client)

def trace_propagation_meta(self, span=None):
# type: (Optional[Span]) -> str
Expand All @@ -792,23 +694,8 @@ def trace_propagation_meta(self, span=None):
"The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future."
)

meta = ""

sentry_trace = self.get_traceparent()
if sentry_trace is not None:
meta += '<meta name="%s" content="%s">' % (
SENTRY_TRACE_HEADER_NAME,
sentry_trace,
)

baggage = self.get_baggage()
if baggage is not None:
meta += '<meta name="%s" content="%s">' % (
BAGGAGE_HEADER_NAME,
baggage,
)

return meta
client, scope = self._stack[-1]
return scope.trace_propagation_meta(span=span, client=client)


GLOBAL_HUB = Hub()
Expand Down
Loading
Loading