Skip to content

Commit ef65524

Browse files
committed
fix: error raised for env checks in LogLimits and SpanLimits
Current implementation raises a value error trying to create the error message. Updated error message format to use positional instead of named params. Added test cases to validate the correct errors are raised.
1 parent 39767ae commit ef65524

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
([#4448](https://github.com/open-telemetry/opentelemetry-python/pull/4448))
2727
- Make `trace_api.use_span()` record `BaseException` as well as `Exception`
2828
([#4406](https://github.com/open-telemetry/opentelemetry-python/pull/4406))
29+
- Fix env var error message for TraceLimits/SpanLimits
30+
([#4458](https://github.com/open-telemetry/opentelemetry-python/pull/4458))
2931

3032
## Version 1.30.0/0.51b0 (2025-02-03)
3133

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _from_env_if_absent(
134134
if value == cls.UNSET:
135135
return None
136136

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

139139
# if no value is provided for the limit, try to load it from env
140140
if value is None:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def _from_env_if_absent(
692692
if value == cls.UNSET:
693693
return None
694694

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

697697
# if no value is provided for the limit, try to load it from env
698698
if value is None:

opentelemetry-sdk/tests/logs/test_log_limits.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
from unittest.mock import patch
1617

1718
from opentelemetry.sdk._logs import LogLimits
1819
from opentelemetry.sdk._logs._internal import (
1920
_DEFAULT_OTEL_ATTRIBUTE_COUNT_LIMIT,
2021
)
22+
from opentelemetry.sdk.environment_variables import (
23+
OTEL_ATTRIBUTE_COUNT_LIMIT,
24+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
25+
)
2126

2227

2328
class TestLogLimits(unittest.TestCase):
@@ -38,3 +43,53 @@ def test_log_limits_max_attribute_length(self):
3843
limits = LogLimits(max_attribute_length=1)
3944

4045
self.assertEqual(expected, limits.max_attribute_length)
46+
47+
def test_non_integer_env_vars_raise(self):
48+
test_cases = [
49+
(env_val, "bad")
50+
for env_val in [
51+
OTEL_ATTRIBUTE_COUNT_LIMIT,
52+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
53+
]
54+
]
55+
56+
for env_var, bad_value in test_cases:
57+
with self.subTest(env_var=env_var, value=bad_value):
58+
env = {env_var: bad_value}
59+
60+
with self.assertRaises(ValueError) as error, patch.dict(
61+
"os.environ", env
62+
):
63+
LogLimits()
64+
65+
expected_msg = f"{env_var} must be a non-negative integer but got {bad_value}"
66+
self.assertEqual(
67+
expected_msg,
68+
str(error.exception),
69+
f"Unexpected error message for {env_var}={bad_value}",
70+
)
71+
72+
def test_negative_integer_env_vars_raise(self):
73+
test_cases = [
74+
(env_val, "-1")
75+
for env_val in [
76+
OTEL_ATTRIBUTE_COUNT_LIMIT,
77+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
78+
]
79+
]
80+
81+
for env_var, invalid_value in test_cases:
82+
with self.subTest(env_var=env_var, value=invalid_value):
83+
env = {env_var: invalid_value}
84+
85+
with self.assertRaises(ValueError) as error, patch.dict(
86+
"os.environ", env
87+
):
88+
LogLimits()
89+
90+
expected_msg = f"{env_var} must be a non-negative integer but got {invalid_value}"
91+
self.assertEqual(
92+
expected_msg,
93+
str(error.exception),
94+
f"Unexpected error message for {env_var}={invalid_value}",
95+
)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
from unittest.mock import patch
17+
18+
from opentelemetry.sdk.environment_variables import (
19+
OTEL_ATTRIBUTE_COUNT_LIMIT,
20+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
21+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
22+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
23+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
24+
OTEL_SPAN_EVENT_COUNT_LIMIT,
25+
OTEL_SPAN_LINK_COUNT_LIMIT,
26+
)
27+
from opentelemetry.sdk.trace import (
28+
SpanLimits,
29+
)
30+
31+
32+
class TestSpanLimits(unittest.TestCase):
33+
def test_non_integer_env_vars_raise(self):
34+
test_cases = [
35+
(env_val, "bad")
36+
for env_val in [
37+
OTEL_SPAN_EVENT_COUNT_LIMIT,
38+
OTEL_SPAN_LINK_COUNT_LIMIT,
39+
OTEL_ATTRIBUTE_COUNT_LIMIT,
40+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
41+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
42+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
43+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
44+
]
45+
]
46+
47+
for env_var, bad_value in test_cases:
48+
with self.subTest(env_var=env_var, value=bad_value):
49+
env = {env_var: bad_value}
50+
51+
with self.assertRaises(ValueError) as error, patch.dict(
52+
"os.environ", env
53+
):
54+
SpanLimits()
55+
56+
expected_msg = f"{env_var} must be a non-negative integer but got {bad_value}"
57+
self.assertEqual(
58+
expected_msg,
59+
str(error.exception),
60+
f"Unexpected error message for {env_var}={bad_value}",
61+
)
62+
63+
def test_negative_integer_env_vars_raise(self):
64+
test_cases = [
65+
(env_val, "-1")
66+
for env_val in [
67+
OTEL_SPAN_EVENT_COUNT_LIMIT,
68+
OTEL_SPAN_LINK_COUNT_LIMIT,
69+
OTEL_ATTRIBUTE_COUNT_LIMIT,
70+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
71+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
72+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
73+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
74+
]
75+
]
76+
77+
for env_var, invalid_value in test_cases:
78+
with self.subTest(env_var=env_var, value=invalid_value):
79+
env = {env_var: invalid_value}
80+
81+
with self.assertRaises(ValueError) as error, patch.dict(
82+
"os.environ", env
83+
):
84+
SpanLimits()
85+
86+
expected_msg = f"{env_var} must be a non-negative integer but got {invalid_value}"
87+
self.assertEqual(
88+
expected_msg,
89+
str(error.exception),
90+
f"Unexpected error message for {env_var}={invalid_value}",
91+
)

0 commit comments

Comments
 (0)