Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5704d5

Browse files
owaislzchen
andauthored
Fix limit env vars unset/unlimited values (open-telemetry#2054)
* Added support for `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT` Fixes open-telemetry#2045 Fixes open-telemetry#2043 Fixes open-telemetry#2042 Fixes open-telemetry#2041 * SpanLimit: Treat empty value env vars as unset Fixes open-telemetry#2052 * Update CHANGELOG.md Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent aa1a2ac commit b5704d5

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
2121
- `opentelemetry-sdk` Fixed bugs (#2041, #2042 & #2045) in Span Limits
2222
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
23+
- `opentelemetry-sdk` Treat limit even vars set to empty values as unset/unlimited.
24+
([#2054](https://github.com/open-telemetry/opentelemetry-python/pull/2054))
2325
- `opentelemetry-api` Attribute keys must be non-empty strings.
2426
([#2057](https://github.com/open-telemetry/opentelemetry-python/pull/2057))
2527

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

Lines changed: 8 additions & 5 deletions
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.
@@ -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

Lines changed: 4 additions & 4 deletions
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)