Skip to content

Commit

Permalink
Fix typing WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Jan 20, 2025
1 parent 72620d1 commit 91fd8d8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 59 deletions.
2 changes: 2 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
- You can no longer change the sampled status of a span with `span.sampled = False` after starting it.
- The `Span()` constructor does not accept a `hub` parameter anymore.
- `Span.finish()` does not accept a `hub` parameter anymore.
- `Span.finish()` no longer returns the `event_id` if the event is sent to sentry.
- The `Profile()` constructor does not accept a `hub` parameter anymore.
- A `Profile` object does not have a `.hub` property anymore.
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
Expand Down Expand Up @@ -146,6 +147,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
- `continue_from_headers`, `continue_from_environ` and `from_traceparent` have been removed, please use top-level API `sentry_sdk.continue_trace` instead.
- `PropagationContext` constructor no longer takes a `dynamic_sampling_context` but takes a `baggage` object instead.
- `ThreadingIntegration` no longer takes the `propagate_hub` argument.
- `Baggage.populate_from_transaction` has been removed.

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/opentelemetry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import sentry_sdk
from sentry_sdk.utils import Dsn
from sentry_sdk.consts import SPANSTATUS, OP, SPANDATA
from sentry_sdk.tracing import get_span_status_from_http_code, DEFAULT_SPAN_ORIGIN
from sentry_sdk.tracing_utils import Baggage, LOW_QUALITY_TRANSACTION_SOURCES
from sentry_sdk.tracing import get_span_status_from_http_code, DEFAULT_SPAN_ORIGIN, LOW_QUALITY_TRANSACTION_SOURCES
from sentry_sdk.tracing_utils import Baggage
from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute

from sentry_sdk._types import TYPE_CHECKING
Expand Down
24 changes: 14 additions & 10 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def finish(
scope=None, # type: Optional[sentry_sdk.Scope]
end_timestamp=None, # type: Optional[Union[float, datetime]]
):
# type: (...) -> Optional[str]
# type: (...) -> None
pass

def set_measurement(self, name, value, unit=""):
Expand Down Expand Up @@ -375,7 +375,9 @@ def __init__(
self.set_status(status)

def __eq__(self, other):
# type: (Span) -> bool
# type: (object) -> bool
if not isinstance(other, Span):
return False
return self._otel_span == other._otel_span

def __repr__(self):
Expand Down Expand Up @@ -526,7 +528,6 @@ def sample_rate(self):
sample_rate = self._otel_span.get_span_context().trace_state.get(
TRACESTATE_SAMPLE_RATE_KEY
)
sample_rate = cast("Optional[str]", sample_rate)
return float(sample_rate) if sample_rate is not None else None

@property
Expand Down Expand Up @@ -668,18 +669,21 @@ def set_data(self, key, value):

def get_attribute(self, name):
# type: (str) -> Optional[Any]
if not isinstance(self._otel_span, ReadableSpan):
if not isinstance(self._otel_span, ReadableSpan) or not self._otel_span.attributes:
return None
return self._otel_span.attributes.get(name)

def set_attribute(self, key, value):
# type: (str, Any) -> None
# otel doesn't support None as values, preferring to not set the key
# at all instead
if value is None:
# otel doesn't support None as values, preferring to not set the key
# at all instead
return
serialized_value = _serialize_span_attribute(value)
if serialized_value is None:
return

self._otel_span.set_attribute(key, _serialize_span_attribute(value))
self._otel_span.set_attribute(key, serialized_value)

@property
def status(self):
Expand All @@ -690,7 +694,7 @@ def status(self):
Sentry `SPANSTATUS` it can not be guaranteed that the status
set in `set_status()` will be the same as the one returned here.
"""
if not hasattr(self._otel_span, "status"):
if not isinstance(self._otel_span, ReadableSpan):
return None

if self._otel_span.status.status_code == StatusCode.UNSET:
Expand Down Expand Up @@ -740,10 +744,10 @@ def set_http_status(self, http_status):

def is_success(self):
# type: () -> bool
return self._otel_span.status.code == StatusCode.OK
return self.status == SPANSTATUS.OK

def finish(self, end_timestamp=None):
# type: (Optional[Union[float, datetime]]) -> Optional[str]
# type: (Optional[Union[float, datetime]]) -> None
if end_timestamp is not None:
from sentry_sdk.integrations.opentelemetry.utils import (
convert_to_otel_timestamp,
Expand Down
47 changes: 0 additions & 47 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,52 +525,6 @@ def from_options(cls, scope):

return Baggage(sentry_items, third_party_items, mutable)

@classmethod
def populate_from_transaction(cls, transaction):
# type: (sentry_sdk.tracing.Transaction) -> Baggage
"""
Populate fresh baggage entry with sentry_items and make it immutable
if this is the head SDK which originates traces.
"""
client = sentry_sdk.get_client()
sentry_items = {} # type: Dict[str, str]

if not client.is_active():
return Baggage(sentry_items)

options = client.options or {}

sentry_items["trace_id"] = transaction.trace_id

if options.get("environment"):
sentry_items["environment"] = options["environment"]

if options.get("release"):
sentry_items["release"] = options["release"]

if options.get("dsn"):
sentry_items["public_key"] = Dsn(options["dsn"]).public_key

if (
transaction.name
and transaction.source not in LOW_QUALITY_TRANSACTION_SOURCES
):
sentry_items["transaction"] = transaction.name

if transaction.sample_rate is not None:
sentry_items["sample_rate"] = str(transaction.sample_rate)

if transaction.sampled is not None:
sentry_items["sampled"] = "true" if transaction.sampled else "false"

# there's an existing baggage but it was mutable,
# which is why we are creating this new baggage.
# However, if by chance the user put some sentry items in there, give them precedence.
if transaction._baggage and transaction._baggage.sentry_items:
sentry_items.update(transaction._baggage.sentry_items)

return Baggage(sentry_items, mutable=False)

def freeze(self):
# type: () -> None
self.mutable = False
Expand Down Expand Up @@ -722,6 +676,5 @@ def get_current_span(scope=None):
# Circular imports
from sentry_sdk.tracing import (
BAGGAGE_HEADER_NAME,
LOW_QUALITY_TRANSACTION_SOURCES,
SENTRY_TRACE_HEADER_NAME,
)

0 comments on commit 91fd8d8

Please sign in to comment.