5
5
from opentelemetry import trace as otel_trace , context
6
6
from opentelemetry .trace import format_trace_id , format_span_id
7
7
from opentelemetry .trace .status import StatusCode
8
+ from opentelemetry .sdk .trace import ReadableSpan
8
9
9
10
import sentry_sdk
10
11
from sentry_sdk .consts import SPANSTATUS , SPANDATA
37
38
R = TypeVar ("R" )
38
39
39
40
import sentry_sdk .profiler
40
- from sentry_sdk .scope import Scope
41
41
from sentry_sdk ._types import (
42
42
Event ,
43
43
MeasurementUnit ,
@@ -1198,7 +1198,6 @@ def __init__(
1198
1198
op = None , # type: Optional[str]
1199
1199
description = None , # type: Optional[str]
1200
1200
status = None , # type: Optional[str]
1201
- scope = None , # type: Optional[Scope]
1202
1201
start_timestamp = None , # type: Optional[Union[datetime, float]]
1203
1202
origin = None , # type: Optional[str]
1204
1203
name = None , # type: Optional[str]
@@ -1218,10 +1217,9 @@ def __init__(
1218
1217
# OTel timestamps have nanosecond precision
1219
1218
start_timestamp = convert_to_otel_timestamp (start_timestamp )
1220
1219
1221
- # XXX deal with _otel_span being a NonRecordingSpan
1222
1220
self ._otel_span = tracer .start_span (
1223
1221
description or op or "" , start_time = start_timestamp
1224
- ) # XXX
1222
+ )
1225
1223
1226
1224
self .origin = origin or DEFAULT_SPAN_ORIGIN
1227
1225
self .op = op
@@ -1267,12 +1265,18 @@ def __exit__(self, ty, value, tb):
1267
1265
# XXX set status to error if unset and an exception occurred?
1268
1266
context .detach (self ._ctx_token )
1269
1267
1268
+ def _get_attribute (self , name ):
1269
+ # type: (str) -> Optional[Any]
1270
+ if not isinstance (self ._otel_span , ReadableSpan ):
1271
+ return None
1272
+ self ._otel_span .attributes .get (name )
1273
+
1270
1274
@property
1271
1275
def description (self ):
1272
1276
# type: () -> Optional[str]
1273
1277
from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
1274
1278
1275
- return self ._otel_span . attributes . get (SentrySpanAttribute .DESCRIPTION )
1279
+ return self ._get_attribute (SentrySpanAttribute .DESCRIPTION )
1276
1280
1277
1281
@description .setter
1278
1282
def description (self , value ):
@@ -1287,7 +1291,7 @@ def origin(self):
1287
1291
# type: () -> Optional[str]
1288
1292
from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
1289
1293
1290
- return self ._otel_span . attributes . get (SentrySpanAttribute .ORIGIN )
1294
+ return self ._get_attribute (SentrySpanAttribute .ORIGIN )
1291
1295
1292
1296
@origin .setter
1293
1297
def origin (self , value ):
@@ -1299,7 +1303,7 @@ def origin(self, value):
1299
1303
1300
1304
@property
1301
1305
def containing_transaction (self ):
1302
- # type: () -> Optional[Transaction ]
1306
+ # type: () -> Optional[POTelSpan ]
1303
1307
"""
1304
1308
Get the transaction this span is a child of.
1305
1309
@@ -1311,16 +1315,6 @@ def containing_transaction(self):
1311
1315
)
1312
1316
return self .root_span
1313
1317
1314
- @containing_transaction .setter
1315
- def containing_transaction (self , value ):
1316
- # type: (Span) -> None
1317
- """
1318
- Set this span's transaction.
1319
- .. deprecated:: 3.0.0
1320
- Use :func:`root_span` instead.
1321
- """
1322
- pass
1323
-
1324
1318
@property
1325
1319
def root_span (self ):
1326
1320
# type: () -> Optional[POTelSpan]
@@ -1329,21 +1323,22 @@ def root_span(self):
1329
1323
# not sure if there's a way to retrieve the parent with pure otel.
1330
1324
return None
1331
1325
1332
- @root_span .setter
1333
- def root_span (self , value ):
1334
- pass
1335
-
1336
1326
@property
1337
1327
def is_root_span (self ):
1338
- if isinstance ( self . _otel_span , otel_trace . NonRecordingSpan ):
1339
- return False
1340
-
1341
- return self . _otel_span . parent is None
1328
+ # type: () -> bool
1329
+ return (
1330
+ isinstance ( self . _otel_span , ReadableSpan ) and self . _otel_span . parent is None
1331
+ )
1342
1332
1343
1333
@property
1344
1334
def parent_span_id (self ):
1345
1335
# type: () -> Optional[str]
1346
- return self ._otel_span .parent if hasattr (self ._otel_span , "parent" ) else None
1336
+ if (
1337
+ not isinstance (self ._otel_span , ReadableSpan )
1338
+ or self ._otel_span .parent is None
1339
+ ):
1340
+ return None
1341
+ return format_span_id (self ._otel_span .parent .span_id )
1347
1342
1348
1343
@property
1349
1344
def trace_id (self ):
@@ -1370,7 +1365,7 @@ def op(self):
1370
1365
# type: () -> Optional[str]
1371
1366
from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
1372
1367
1373
- return self ._otel_span . attributes . get (SentrySpanAttribute .OP )
1368
+ self ._get_attribute (SentrySpanAttribute .OP )
1374
1369
1375
1370
@op .setter
1376
1371
def op (self , value ):
@@ -1385,7 +1380,7 @@ def name(self):
1385
1380
# type: () -> Optional[str]
1386
1381
from sentry_sdk .integrations .opentelemetry .consts import SentrySpanAttribute
1387
1382
1388
- return self ._otel_span . attributes . get (SentrySpanAttribute .NAME )
1383
+ self ._get_attribute (SentrySpanAttribute .NAME )
1389
1384
1390
1385
@name .setter
1391
1386
def name (self , value ):
@@ -1408,6 +1403,9 @@ def source(self, value):
1408
1403
@property
1409
1404
def start_timestamp (self ):
1410
1405
# type: () -> Optional[datetime]
1406
+ if not isinstance (self ._otel_span , ReadableSpan ):
1407
+ return None
1408
+
1411
1409
start_time = self ._otel_span .start_time
1412
1410
if start_time is None :
1413
1411
return None
@@ -1421,6 +1419,9 @@ def start_timestamp(self):
1421
1419
@property
1422
1420
def timestamp (self ):
1423
1421
# type: () -> Optional[datetime]
1422
+ if not isinstance (self ._otel_span , ReadableSpan ):
1423
+ return None
1424
+
1424
1425
end_time = self ._otel_span .end_time
1425
1426
if end_time is None :
1426
1427
return None
@@ -1432,23 +1433,12 @@ def timestamp(self):
1432
1433
return convert_from_otel_timestamp (end_time )
1433
1434
1434
1435
def start_child (self , ** kwargs ):
1435
- # type: (str, **Any) -> POTelSpan
1436
+ # type: (**Any) -> POTelSpan
1436
1437
kwargs .setdefault ("sampled" , self .sampled )
1437
1438
1438
1439
span = POTelSpan (** kwargs )
1439
1440
return span
1440
1441
1441
- @classmethod
1442
- def continue_from_environ (
1443
- cls ,
1444
- environ , # type: Mapping[str, str]
1445
- ** kwargs , # type: Any
1446
- ):
1447
- # type: (...) -> POTelSpan
1448
- # XXX actually propagate
1449
- span = POTelSpan (** kwargs )
1450
- return span
1451
-
1452
1442
@classmethod
1453
1443
def continue_from_headers (
1454
1444
cls ,
@@ -1464,17 +1454,6 @@ def iter_headers(self):
1464
1454
# type: () -> Iterator[Tuple[str, str]]
1465
1455
pass
1466
1456
1467
- @classmethod
1468
- def from_traceparent (
1469
- cls ,
1470
- traceparent , # type: Optional[str]
1471
- ** kwargs , # type: Any
1472
- ):
1473
- # type: (...) -> Optional[Transaction]
1474
- # XXX actually propagate
1475
- span = POTelSpan (** kwargs )
1476
- return span
1477
-
1478
1457
def to_traceparent (self ):
1479
1458
# type: () -> str
1480
1459
if self .sampled is True :
0 commit comments