Skip to content

Commit 91fd8d8

Browse files
committed
Fix typing WIP
1 parent 72620d1 commit 91fd8d8

File tree

4 files changed

+18
-59
lines changed

4 files changed

+18
-59
lines changed

MIGRATION_GUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
1414
- You can no longer change the sampled status of a span with `span.sampled = False` after starting it.
1515
- The `Span()` constructor does not accept a `hub` parameter anymore.
1616
- `Span.finish()` does not accept a `hub` parameter anymore.
17+
- `Span.finish()` no longer returns the `event_id` if the event is sent to sentry.
1718
- The `Profile()` constructor does not accept a `hub` parameter anymore.
1819
- A `Profile` object does not have a `.hub` property anymore.
1920
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
@@ -146,6 +147,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
146147
- `continue_from_headers`, `continue_from_environ` and `from_traceparent` have been removed, please use top-level API `sentry_sdk.continue_trace` instead.
147148
- `PropagationContext` constructor no longer takes a `dynamic_sampling_context` but takes a `baggage` object instead.
148149
- `ThreadingIntegration` no longer takes the `propagate_hub` argument.
150+
- `Baggage.populate_from_transaction` has been removed.
149151

150152
### Deprecated
151153

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import sentry_sdk
1919
from sentry_sdk.utils import Dsn
2020
from sentry_sdk.consts import SPANSTATUS, OP, SPANDATA
21-
from sentry_sdk.tracing import get_span_status_from_http_code, DEFAULT_SPAN_ORIGIN
22-
from sentry_sdk.tracing_utils import Baggage, LOW_QUALITY_TRANSACTION_SOURCES
21+
from sentry_sdk.tracing import get_span_status_from_http_code, DEFAULT_SPAN_ORIGIN, LOW_QUALITY_TRANSACTION_SOURCES
22+
from sentry_sdk.tracing_utils import Baggage
2323
from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute
2424

2525
from sentry_sdk._types import TYPE_CHECKING

sentry_sdk/tracing.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def finish(
266266
scope=None, # type: Optional[sentry_sdk.Scope]
267267
end_timestamp=None, # type: Optional[Union[float, datetime]]
268268
):
269-
# type: (...) -> Optional[str]
269+
# type: (...) -> None
270270
pass
271271

272272
def set_measurement(self, name, value, unit=""):
@@ -375,7 +375,9 @@ def __init__(
375375
self.set_status(status)
376376

377377
def __eq__(self, other):
378-
# type: (Span) -> bool
378+
# type: (object) -> bool
379+
if not isinstance(other, Span):
380+
return False
379381
return self._otel_span == other._otel_span
380382

381383
def __repr__(self):
@@ -526,7 +528,6 @@ def sample_rate(self):
526528
sample_rate = self._otel_span.get_span_context().trace_state.get(
527529
TRACESTATE_SAMPLE_RATE_KEY
528530
)
529-
sample_rate = cast("Optional[str]", sample_rate)
530531
return float(sample_rate) if sample_rate is not None else None
531532

532533
@property
@@ -668,18 +669,21 @@ def set_data(self, key, value):
668669

669670
def get_attribute(self, name):
670671
# type: (str) -> Optional[Any]
671-
if not isinstance(self._otel_span, ReadableSpan):
672+
if not isinstance(self._otel_span, ReadableSpan) or not self._otel_span.attributes:
672673
return None
673674
return self._otel_span.attributes.get(name)
674675

675676
def set_attribute(self, key, value):
676677
# type: (str, Any) -> None
678+
# otel doesn't support None as values, preferring to not set the key
679+
# at all instead
677680
if value is None:
678-
# otel doesn't support None as values, preferring to not set the key
679-
# at all instead
681+
return
682+
serialized_value = _serialize_span_attribute(value)
683+
if serialized_value is None:
680684
return
681685

682-
self._otel_span.set_attribute(key, _serialize_span_attribute(value))
686+
self._otel_span.set_attribute(key, serialized_value)
683687

684688
@property
685689
def status(self):
@@ -690,7 +694,7 @@ def status(self):
690694
Sentry `SPANSTATUS` it can not be guaranteed that the status
691695
set in `set_status()` will be the same as the one returned here.
692696
"""
693-
if not hasattr(self._otel_span, "status"):
697+
if not isinstance(self._otel_span, ReadableSpan):
694698
return None
695699

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

741745
def is_success(self):
742746
# type: () -> bool
743-
return self._otel_span.status.code == StatusCode.OK
747+
return self.status == SPANSTATUS.OK
744748

745749
def finish(self, end_timestamp=None):
746-
# type: (Optional[Union[float, datetime]]) -> Optional[str]
750+
# type: (Optional[Union[float, datetime]]) -> None
747751
if end_timestamp is not None:
748752
from sentry_sdk.integrations.opentelemetry.utils import (
749753
convert_to_otel_timestamp,

sentry_sdk/tracing_utils.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -525,52 +525,6 @@ def from_options(cls, scope):
525525

526526
return Baggage(sentry_items, third_party_items, mutable)
527527

528-
@classmethod
529-
def populate_from_transaction(cls, transaction):
530-
# type: (sentry_sdk.tracing.Transaction) -> Baggage
531-
"""
532-
Populate fresh baggage entry with sentry_items and make it immutable
533-
if this is the head SDK which originates traces.
534-
"""
535-
client = sentry_sdk.get_client()
536-
sentry_items = {} # type: Dict[str, str]
537-
538-
if not client.is_active():
539-
return Baggage(sentry_items)
540-
541-
options = client.options or {}
542-
543-
sentry_items["trace_id"] = transaction.trace_id
544-
545-
if options.get("environment"):
546-
sentry_items["environment"] = options["environment"]
547-
548-
if options.get("release"):
549-
sentry_items["release"] = options["release"]
550-
551-
if options.get("dsn"):
552-
sentry_items["public_key"] = Dsn(options["dsn"]).public_key
553-
554-
if (
555-
transaction.name
556-
and transaction.source not in LOW_QUALITY_TRANSACTION_SOURCES
557-
):
558-
sentry_items["transaction"] = transaction.name
559-
560-
if transaction.sample_rate is not None:
561-
sentry_items["sample_rate"] = str(transaction.sample_rate)
562-
563-
if transaction.sampled is not None:
564-
sentry_items["sampled"] = "true" if transaction.sampled else "false"
565-
566-
# there's an existing baggage but it was mutable,
567-
# which is why we are creating this new baggage.
568-
# However, if by chance the user put some sentry items in there, give them precedence.
569-
if transaction._baggage and transaction._baggage.sentry_items:
570-
sentry_items.update(transaction._baggage.sentry_items)
571-
572-
return Baggage(sentry_items, mutable=False)
573-
574528
def freeze(self):
575529
# type: () -> None
576530
self.mutable = False
@@ -722,6 +676,5 @@ def get_current_span(scope=None):
722676
# Circular imports
723677
from sentry_sdk.tracing import (
724678
BAGGAGE_HEADER_NAME,
725-
LOW_QUALITY_TRANSACTION_SOURCES,
726679
SENTRY_TRACE_HEADER_NAME,
727680
)

0 commit comments

Comments
 (0)