62
62
63
63
logger = logging .getLogger (__name__ )
64
64
65
- _DEFAULT_SPAN_EVENTS_LIMIT = 128
66
- _DEFAULT_SPAN_LINKS_LIMIT = 128
67
- _DEFAULT_SPAN_ATTRIBUTES_LIMIT = 128
65
+ _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT = 128
66
+ _DEFAULT_OTEL_SPAN_EVENT_COUNT_LIMIT = 128
67
+ _DEFAULT_OTEL_SPAN_LINK_COUNT_LIMIT = 128
68
+
69
+
70
+ _ENV_VALUE_UNSET = "unset"
68
71
69
72
# pylint: disable=protected-access
70
73
_TRACE_SAMPLER = sampling ._get_from_env_or_default ()
@@ -499,19 +502,29 @@ def _format_links(links):
499
502
return f_links
500
503
501
504
502
- class _Limits :
505
+ class SpanLimits :
503
506
"""The limits that should be enforce on recorded data such as events, links, attributes etc.
504
507
505
508
This class does not enforce any limits itself. It only provides an a way read limits from env,
506
- default values and in future from user provided arguments.
509
+ default values and from user provided arguments.
510
+
511
+ All limit arguments must be either a non-negative integer, ``None`` or ``SpanLimits.UNSET``.
507
512
508
- All limit must be either a non-negative integer or ``None``.
509
- Setting a limit to ``None`` will not set any limits for that field/type.
513
+ - All limit arguments are optional.
514
+ - If a limit argument is not set, the class will try to read it's value from the corresponding
515
+ environment variable.
516
+ - If the environment variable is not set, the default value for the limit is used.
510
517
511
518
Args:
519
+ max_attributes: Maximum number of attributes that can be added to a Span.
520
+ Environment variable: OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT
521
+ Default: {_DEFAULT_SPAN_ATTRIBUTE_COUNT_LIMIT}
512
522
max_events: Maximum number of events that can be added to a Span.
523
+ Environment variable: OTEL_SPAN_EVENT_COUNT_LIMIT
524
+ Default: {_DEFAULT_SPAN_EVENT_COUNT_LIMIT}
513
525
max_links: Maximum number of links that can be added to a Span.
514
- max_attributes: Maximum number of attributes that can be added to a Span.
526
+ Environment variable: OTEL_SPAN_LINK_COUNT_LIMIT
527
+ Default: {_DEFAULT_SPAN_LINK_COUNT_LIMIT}
515
528
"""
516
529
517
530
UNSET = - 1
@@ -529,13 +542,17 @@ def __init__(
529
542
self .max_attributes = self ._from_env_if_absent (
530
543
max_attributes ,
531
544
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ,
532
- _DEFAULT_SPAN_ATTRIBUTES_LIMIT ,
545
+ _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ,
533
546
)
534
547
self .max_events = self ._from_env_if_absent (
535
- max_events , OTEL_SPAN_EVENT_COUNT_LIMIT , _DEFAULT_SPAN_EVENTS_LIMIT
548
+ max_events ,
549
+ OTEL_SPAN_EVENT_COUNT_LIMIT ,
550
+ _DEFAULT_OTEL_SPAN_EVENT_COUNT_LIMIT ,
536
551
)
537
552
self .max_links = self ._from_env_if_absent (
538
- max_links , OTEL_SPAN_LINK_COUNT_LIMIT , _DEFAULT_SPAN_LINKS_LIMIT
553
+ max_links ,
554
+ OTEL_SPAN_LINK_COUNT_LIMIT ,
555
+ _DEFAULT_OTEL_SPAN_LINK_COUNT_LIMIT ,
539
556
)
540
557
541
558
def __repr__ (self ):
@@ -556,7 +573,7 @@ def _from_env_if_absent(
556
573
str_value = environ .get (env_var , "" ).strip ().lower ()
557
574
if not str_value :
558
575
return default
559
- if str_value == "unset" :
576
+ if str_value == _ENV_VALUE_UNSET :
560
577
return None
561
578
562
579
try :
@@ -569,14 +586,16 @@ def _from_env_if_absent(
569
586
return value
570
587
571
588
572
- _UnsetLimits = _Limits (
573
- max_attributes = _Limits .UNSET ,
574
- max_events = _Limits .UNSET ,
575
- max_links = _Limits .UNSET ,
589
+ _UnsetLimits = SpanLimits (
590
+ max_attributes = SpanLimits .UNSET ,
591
+ max_events = SpanLimits .UNSET ,
592
+ max_links = SpanLimits .UNSET ,
576
593
)
577
594
578
- SPAN_ATTRIBUTE_COUNT_LIMIT = _Limits ._from_env_if_absent (
579
- None , OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT , _DEFAULT_SPAN_ATTRIBUTES_LIMIT
595
+ SPAN_ATTRIBUTE_COUNT_LIMIT = SpanLimits ._from_env_if_absent (
596
+ None ,
597
+ OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ,
598
+ _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ,
580
599
)
581
600
582
601
@@ -599,6 +618,7 @@ class Span(trace_api.Span, ReadableSpan):
599
618
links: Links to other spans to be exported
600
619
span_processor: `SpanProcessor` to invoke when starting and ending
601
620
this `Span`.
621
+ limits: `SpanLimits` instance that was passed to the `TracerProvider`
602
622
"""
603
623
604
624
def __new__ (cls , * args , ** kwargs ):
@@ -623,6 +643,7 @@ def __init__(
623
643
instrumentation_info : InstrumentationInfo = None ,
624
644
record_exception : bool = True ,
625
645
set_status_on_exception : bool = True ,
646
+ limits = _UnsetLimits ,
626
647
) -> None :
627
648
super ().__init__ (
628
649
name = name ,
@@ -637,6 +658,7 @@ def __init__(
637
658
self ._record_exception = record_exception
638
659
self ._set_status_on_exception = set_status_on_exception
639
660
self ._span_processor = span_processor
661
+ self ._limits = limits
640
662
self ._lock = threading .Lock ()
641
663
642
664
_filter_attributes (attributes )
@@ -847,10 +869,6 @@ class _Span(Span):
847
869
by other mechanisms than through the `Tracer`.
848
870
"""
849
871
850
- def __init__ (self , * args , limits = _UnsetLimits , ** kwargs ):
851
- self ._limits = limits
852
- super ().__init__ (* args , ** kwargs )
853
-
854
872
855
873
class Tracer (trace_api .Tracer ):
856
874
"""See `opentelemetry.trace.Tracer`."""
@@ -864,13 +882,14 @@ def __init__(
864
882
],
865
883
id_generator : IdGenerator ,
866
884
instrumentation_info : InstrumentationInfo ,
885
+ span_limits : SpanLimits ,
867
886
) -> None :
868
887
self .sampler = sampler
869
888
self .resource = resource
870
889
self .span_processor = span_processor
871
890
self .id_generator = id_generator
872
891
self .instrumentation_info = instrumentation_info
873
- self ._limits = None
892
+ self ._span_limits = span_limits
874
893
875
894
@contextmanager
876
895
def start_as_current_span (
@@ -972,7 +991,7 @@ def start_span( # pylint: disable=too-many-locals
972
991
instrumentation_info = self .instrumentation_info ,
973
992
record_exception = record_exception ,
974
993
set_status_on_exception = set_status_on_exception ,
975
- limits = self ._limits ,
994
+ limits = self ._span_limits ,
976
995
)
977
996
span .start (start_time = start_time , parent_context = context )
978
997
else :
@@ -992,6 +1011,7 @@ def __init__(
992
1011
SynchronousMultiSpanProcessor , ConcurrentMultiSpanProcessor
993
1012
] = None ,
994
1013
id_generator : IdGenerator = None ,
1014
+ span_limits : SpanLimits = None ,
995
1015
):
996
1016
self ._active_span_processor = (
997
1017
active_span_processor or SynchronousMultiSpanProcessor ()
@@ -1002,7 +1022,7 @@ def __init__(
1002
1022
self .id_generator = id_generator
1003
1023
self ._resource = resource
1004
1024
self .sampler = sampler
1005
- self ._limits = _Limits ()
1025
+ self ._span_limits = span_limits or SpanLimits ()
1006
1026
self ._atexit_handler = None
1007
1027
if shutdown_on_exit :
1008
1028
self ._atexit_handler = atexit .register (self .shutdown )
@@ -1019,17 +1039,16 @@ def get_tracer(
1019
1039
if not instrumenting_module_name : # Reject empty strings too.
1020
1040
instrumenting_module_name = ""
1021
1041
logger .error ("get_tracer called with missing module name." )
1022
- tracer = Tracer (
1042
+ return Tracer (
1023
1043
self .sampler ,
1024
1044
self .resource ,
1025
1045
self ._active_span_processor ,
1026
1046
self .id_generator ,
1027
1047
InstrumentationInfo (
1028
1048
instrumenting_module_name , instrumenting_library_version
1029
1049
),
1050
+ self ._span_limits ,
1030
1051
)
1031
- tracer ._limits = self ._limits
1032
- return tracer
1033
1052
1034
1053
def add_span_processor (self , span_processor : SpanProcessor ) -> None :
1035
1054
"""Registers a new :class:`SpanProcessor` for this `TracerProvider`.
0 commit comments