@@ -1394,6 +1394,102 @@ def test_limits_values_env(self):
1394
1394
self .assertEqual (limits .max_links , max_links )
1395
1395
1396
1396
1397
+ def _test_span_limits (
1398
+ self , tracer , max_attrs , max_events , max_links , max_attr_len
1399
+ ):
1400
+ id_generator = RandomIdGenerator ()
1401
+ some_links = [
1402
+ trace_api .Link (
1403
+ trace_api .SpanContext (
1404
+ trace_id = id_generator .generate_trace_id (),
1405
+ span_id = id_generator .generate_span_id (),
1406
+ is_remote = False ,
1407
+ ),
1408
+ attributes = {"k" : self .long_val },
1409
+ )
1410
+ for _ in range (100 )
1411
+ ]
1412
+
1413
+ some_attrs = {
1414
+ "init_attribute_{}" .format (idx ): self .long_val
1415
+ for idx in range (100 )
1416
+ }
1417
+ with tracer .start_as_current_span (
1418
+ "root" , links = some_links , attributes = some_attrs
1419
+ ) as root :
1420
+ self .assertEqual (len (root .links ), max_links )
1421
+ self .assertEqual (len (root .attributes ), max_attrs )
1422
+ for idx in range (100 ):
1423
+ root .set_attribute (
1424
+ "my_str_attribute_{}" .format (idx ), self .long_val
1425
+ )
1426
+ root .set_attribute (
1427
+ "my_byte_attribute_{}" .format (idx ), self .long_val .encode ()
1428
+ )
1429
+ root .set_attribute (
1430
+ "my_int_attribute_{}" .format (idx ), self .long_val .encode ()
1431
+ )
1432
+ root .add_event (
1433
+ "my_event_{}" .format (idx ), attributes = {"k" : self .long_val }
1434
+ )
1435
+
1436
+ self .assertEqual (len (root .attributes ), max_attrs )
1437
+ self .assertEqual (len (root .events ), max_events )
1438
+
1439
+ for link in root .links :
1440
+ for attr_val in link .attributes .values ():
1441
+ self ._assert_attr_length (attr_val , max_attr_len )
1442
+
1443
+ for event in root .events :
1444
+ for attr_val in event .attributes .values ():
1445
+ self ._assert_attr_length (attr_val , max_attr_len )
1446
+
1447
+ for attr_val in root .attributes .values ():
1448
+ self ._assert_attr_length (attr_val , max_attr_len )
1449
+
1450
+ def _test_span_no_limits (self , tracer ):
1451
+ num_links = int (trace ._DEFAULT_OTEL_SPAN_LINK_COUNT_LIMIT ) + randint (
1452
+ 1 , 100
1453
+ )
1454
+
1455
+ id_generator = RandomIdGenerator ()
1456
+ some_links = [
1457
+ trace_api .Link (
1458
+ trace_api .SpanContext (
1459
+ trace_id = id_generator .generate_trace_id (),
1460
+ span_id = id_generator .generate_span_id (),
1461
+ is_remote = False ,
1462
+ )
1463
+ )
1464
+ for _ in range (num_links )
1465
+ ]
1466
+ with tracer .start_as_current_span ("root" , links = some_links ) as root :
1467
+ self .assertEqual (len (root .links ), num_links )
1468
+
1469
+ num_events = int (trace ._DEFAULT_OTEL_SPAN_EVENT_COUNT_LIMIT ) + randint (
1470
+ 1 , 100
1471
+ )
1472
+ with tracer .start_as_current_span ("root" ) as root :
1473
+ for idx in range (num_events ):
1474
+ root .add_event (
1475
+ "my_event_{}" .format (idx ), attributes = {"k" : self .long_val }
1476
+ )
1477
+
1478
+ self .assertEqual (len (root .events ), num_events )
1479
+
1480
+ num_attributes = int (
1481
+ trace ._DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT
1482
+ ) + randint (1 , 100 )
1483
+ with tracer .start_as_current_span ("root" ) as root :
1484
+ for idx in range (num_attributes ):
1485
+ root .set_attribute (
1486
+ "my_attribute_{}" .format (idx ), self .long_val
1487
+ )
1488
+
1489
+ self .assertEqual (len (root .attributes ), num_attributes )
1490
+ for attr_val in root .attributes .values ():
1491
+ self .assertEqual (attr_val , self .long_val )
1492
+
1397
1493
@mock .patch .dict (
1398
1494
"os.environ" ,
1399
1495
{
0 commit comments