Skip to content

Commit 891233f

Browse files
committed
SpanLimit: Treat empty value env vars as unset
Fixes #2052
1 parent 061e72c commit 891233f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
_DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT = 128
6868

6969

70-
_ENV_VALUE_UNSET = "unset"
70+
_ENV_VALUE_UNSET = ""
7171

7272
# pylint: disable=protected-access
7373
_TRACE_SAMPLER = sampling._get_from_env_or_default()
@@ -533,7 +533,7 @@ class SpanLimits:
533533
- All limit arguments are optional.
534534
- If a limit argument is not set, the class will try to read its value from the corresponding
535535
environment variable.
536-
- If the environment variable is not set, the default value for the limit is used.
536+
- If the environment variable is not set, the default value, if any, will be used.
537537
538538
Args:
539539
max_attributes: Maximum number of attributes that can be added to a Span.
@@ -553,7 +553,7 @@ class SpanLimits:
553553
the specified length will be truncated.
554554
"""
555555

556-
UNSET = -1
556+
UNSET = "unset"
557557

558558
def __init__(
559559
self,
@@ -609,15 +609,18 @@ def __repr__(self):
609609
def _from_env_if_absent(
610610
cls, value: Optional[int], env_var: str, default: Optional[int] = None
611611
) -> Optional[int]:
612-
if value is cls.UNSET:
612+
if value == cls.UNSET:
613613
return None
614614

615615
err_msg = "{0} must be a non-negative integer but got {}"
616616

617+
# if no value is provided for the limit, try to load it from env
617618
if value is None:
618-
str_value = environ.get(env_var, "").strip().lower()
619-
if not str_value:
619+
# return default value if env var is not set
620+
if env_var not in environ:
620621
return default
622+
623+
str_value = environ.get(env_var, "").strip().lower()
621624
if str_value == _ENV_VALUE_UNSET:
622625
return None
623626

opentelemetry-sdk/tests/trace/test_trace.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1533,10 +1533,10 @@ def test_span_limits_code(self):
15331533
@mock.patch.dict(
15341534
"os.environ",
15351535
{
1536-
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: "unset",
1537-
OTEL_SPAN_EVENT_COUNT_LIMIT: "unset",
1538-
OTEL_SPAN_LINK_COUNT_LIMIT: "unset",
1539-
OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "unset",
1536+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: "",
1537+
OTEL_SPAN_EVENT_COUNT_LIMIT: "",
1538+
OTEL_SPAN_LINK_COUNT_LIMIT: "",
1539+
OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "",
15401540
},
15411541
)
15421542
def test_span_no_limits_env(self):

0 commit comments

Comments
 (0)