1717
1818import sentry_sdk
1919from sentry_sdk .consts import SPANSTATUS , SPANDATA
20+ from sentry_sdk .opentelemetry .consts import (
21+ TRACESTATE_SAMPLE_RATE_KEY ,
22+ SentrySpanAttribute ,
23+ )
24+ from sentry_sdk .opentelemetry .utils import (
25+ baggage_from_trace_state ,
26+ convert_from_otel_timestamp ,
27+ convert_to_otel_timestamp ,
28+ get_trace_context ,
29+ get_trace_state ,
30+ get_sentry_meta ,
31+ serialize_trace_state ,
32+ )
2033from sentry_sdk .utils import (
2134 _serialize_span_attribute ,
2235 get_current_thread_meta ,
@@ -260,12 +273,6 @@ def __init__(
260273 if skip_span :
261274 self ._otel_span = INVALID_SPAN
262275 else :
263- from sentry_sdk .integrations .opentelemetry .consts import (
264- SentrySpanAttribute ,
265- )
266- from sentry_sdk .integrations .opentelemetry .utils import (
267- convert_to_otel_timestamp ,
268- )
269276
270277 if start_timestamp is not None :
271278 # OTel timestamps have nanosecond precision
@@ -360,38 +367,26 @@ def __exit__(self, ty, value, tb):
360367 @property
361368 def description (self ):
362369 # type: () -> Optional[str]
363- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
364-
365370 return self .get_attribute (SentrySpanAttribute .DESCRIPTION )
366371
367372 @description .setter
368373 def description (self , value ):
369374 # type: (Optional[str]) -> None
370- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
371-
372375 self .set_attribute (SentrySpanAttribute .DESCRIPTION , value )
373376
374377 @property
375378 def origin (self ):
376379 # type: () -> Optional[str]
377- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
378-
379380 return self .get_attribute (SentrySpanAttribute .ORIGIN )
380381
381382 @origin .setter
382383 def origin (self , value ):
383384 # type: (Optional[str]) -> None
384- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
385-
386385 self .set_attribute (SentrySpanAttribute .ORIGIN , value )
387386
388387 @property
389388 def root_span (self ):
390389 # type: () -> Optional[Span]
391- from sentry_sdk .integrations .opentelemetry .utils import (
392- get_sentry_meta ,
393- )
394-
395390 root_otel_span = cast (
396391 "Optional[OtelSpan]" , get_sentry_meta (self ._otel_span , "root_span" )
397392 )
@@ -437,10 +432,6 @@ def sampled(self):
437432 @property
438433 def sample_rate (self ):
439434 # type: () -> Optional[float]
440- from sentry_sdk .integrations .opentelemetry .consts import (
441- TRACESTATE_SAMPLE_RATE_KEY ,
442- )
443-
444435 sample_rate = self ._otel_span .get_span_context ().trace_state .get (
445436 TRACESTATE_SAMPLE_RATE_KEY
446437 )
@@ -449,45 +440,33 @@ def sample_rate(self):
449440 @property
450441 def op (self ):
451442 # type: () -> Optional[str]
452- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
453-
454443 return self .get_attribute (SentrySpanAttribute .OP )
455444
456445 @op .setter
457446 def op (self , value ):
458447 # type: (Optional[str]) -> None
459- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
460-
461448 self .set_attribute (SentrySpanAttribute .OP , value )
462449
463450 @property
464451 def name (self ):
465452 # type: () -> Optional[str]
466- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
467-
468453 return self .get_attribute (SentrySpanAttribute .NAME )
469454
470455 @name .setter
471456 def name (self , value ):
472457 # type: (Optional[str]) -> None
473- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
474-
475458 self .set_attribute (SentrySpanAttribute .NAME , value )
476459
477460 @property
478461 def source (self ):
479462 # type: () -> str
480- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
481-
482463 return (
483464 self .get_attribute (SentrySpanAttribute .SOURCE ) or TransactionSource .CUSTOM
484465 )
485466
486467 @source .setter
487468 def source (self , value ):
488469 # type: (str) -> None
489- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
490-
491470 self .set_attribute (SentrySpanAttribute .SOURCE , value )
492471
493472 @property
@@ -500,10 +479,6 @@ def start_timestamp(self):
500479 if start_time is None :
501480 return None
502481
503- from sentry_sdk .integrations .opentelemetry .utils import (
504- convert_from_otel_timestamp ,
505- )
506-
507482 return convert_from_otel_timestamp (start_time )
508483
509484 @property
@@ -516,10 +491,6 @@ def timestamp(self):
516491 if end_time is None :
517492 return None
518493
519- from sentry_sdk .integrations .opentelemetry .utils import (
520- convert_from_otel_timestamp ,
521- )
522-
523494 return convert_from_otel_timestamp (end_time )
524495
525496 def start_child (self , ** kwargs ):
@@ -529,11 +500,6 @@ def start_child(self, **kwargs):
529500 def iter_headers (self ):
530501 # type: () -> Iterator[Tuple[str, str]]
531502 yield SENTRY_TRACE_HEADER_NAME , self .to_traceparent ()
532-
533- from sentry_sdk .integrations .opentelemetry .utils import (
534- serialize_trace_state ,
535- )
536-
537503 yield BAGGAGE_HEADER_NAME , serialize_trace_state (self .trace_state )
538504
539505 def to_traceparent (self ):
@@ -554,10 +520,6 @@ def to_traceparent(self):
554520 @property
555521 def trace_state (self ):
556522 # type: () -> TraceState
557- from sentry_sdk .integrations .opentelemetry .utils import (
558- get_trace_state ,
559- )
560-
561523 return get_trace_state (self ._otel_span )
562524
563525 def to_baggage (self ):
@@ -566,16 +528,10 @@ def to_baggage(self):
566528
567529 def get_baggage (self ):
568530 # type: () -> Baggage
569- from sentry_sdk .integrations .opentelemetry .utils import (
570- baggage_from_trace_state ,
571- )
572-
573531 return baggage_from_trace_state (self .trace_state )
574532
575533 def set_tag (self , key , value ):
576534 # type: (str, Any) -> None
577- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
578-
579535 self .set_attribute (f"{ SentrySpanAttribute .TAG } .{ key } " , value )
580536
581537 def set_data (self , key , value ):
@@ -642,8 +598,6 @@ def set_status(self, status):
642598
643599 def set_measurement (self , name , value , unit = "" ):
644600 # type: (str, float, MeasurementUnit) -> None
645- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
646-
647601 # Stringify value here since OTel expects all seq items to be of one type
648602 self .set_attribute (
649603 f"{ SentrySpanAttribute .MEASUREMENT } .{ name } " , (str (value ), unit )
@@ -674,10 +628,6 @@ def is_success(self):
674628 def finish (self , end_timestamp = None ):
675629 # type: (Optional[Union[float, datetime]]) -> None
676630 if end_timestamp is not None :
677- from sentry_sdk .integrations .opentelemetry .utils import (
678- convert_to_otel_timestamp ,
679- )
680-
681631 self ._otel_span .end (convert_to_otel_timestamp (end_timestamp ))
682632 else :
683633 self ._otel_span .end ()
@@ -696,16 +646,10 @@ def get_trace_context(self):
696646 if not isinstance (self ._otel_span , ReadableSpan ):
697647 return {}
698648
699- from sentry_sdk .integrations .opentelemetry .utils import (
700- get_trace_context ,
701- )
702-
703649 return get_trace_context (self ._otel_span )
704650
705651 def set_context (self , key , value ):
706652 # type: (str, Any) -> None
707- from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
708-
709653 # TODO-neel-potel we cannot add dicts here
710654
711655 self .set_attribute (f"{ SentrySpanAttribute .CONTEXT } .{ key } " , value )
0 commit comments