Skip to content

Commit

Permalink
Fix limit env vars unset/unlimited values (#2054)
Browse files Browse the repository at this point in the history
* Added support for `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT`

Fixes #2045
Fixes #2043
Fixes #2042
Fixes #2041

* SpanLimit: Treat empty value env vars as unset

Fixes #2052

* Update CHANGELOG.md

Co-authored-by: Leighton Chen <[email protected]>

Co-authored-by: Leighton Chen <[email protected]>
  • Loading branch information
owais and lzchen authored Aug 24, 2021
1 parent aa1a2ac commit b5704d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
- `opentelemetry-sdk` Fixed bugs (#2041, #2042 & #2045) in Span Limits
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
- `opentelemetry-sdk` Treat limit even vars set to empty values as unset/unlimited.
([#2054](https://github.com/open-telemetry/opentelemetry-python/pull/2054))
- `opentelemetry-api` Attribute keys must be non-empty strings.
([#2057](https://github.com/open-telemetry/opentelemetry-python/pull/2057))

Expand Down
13 changes: 8 additions & 5 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
_DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT = 128


_ENV_VALUE_UNSET = "unset"
_ENV_VALUE_UNSET = ""

# pylint: disable=protected-access
_TRACE_SAMPLER = sampling._get_from_env_or_default()
Expand Down Expand Up @@ -533,7 +533,7 @@ class SpanLimits:
- All limit arguments are optional.
- If a limit argument is not set, the class will try to read its value from the corresponding
environment variable.
- If the environment variable is not set, the default value for the limit is used.
- If the environment variable is not set, the default value, if any, will be used.
Args:
max_attributes: Maximum number of attributes that can be added to a Span.
Expand Down Expand Up @@ -609,15 +609,18 @@ def __repr__(self):
def _from_env_if_absent(
cls, value: Optional[int], env_var: str, default: Optional[int] = None
) -> Optional[int]:
if value is cls.UNSET:
if value == cls.UNSET:
return None

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

# if no value is provided for the limit, try to load it from env
if value is None:
str_value = environ.get(env_var, "").strip().lower()
if not str_value:
# return default value if env var is not set
if env_var not in environ:
return default

str_value = environ.get(env_var, "").strip().lower()
if str_value == _ENV_VALUE_UNSET:
return None

Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,10 +1533,10 @@ def test_span_limits_code(self):
@mock.patch.dict(
"os.environ",
{
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: "unset",
OTEL_SPAN_EVENT_COUNT_LIMIT: "unset",
OTEL_SPAN_LINK_COUNT_LIMIT: "unset",
OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "unset",
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: "",
OTEL_SPAN_EVENT_COUNT_LIMIT: "",
OTEL_SPAN_LINK_COUNT_LIMIT: "",
OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "",
},
)
def test_span_no_limits_env(self):
Expand Down

0 comments on commit b5704d5

Please sign in to comment.