Skip to content

Commit 1b073db

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 1b073db

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
17+
# from opentelemetry.sdk.trace.
18+
from unittest.mock import patch
19+
20+
from opentelemetry.sdk.environment_variables import (
21+
OTEL_ATTRIBUTE_COUNT_LIMIT,
22+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
23+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
24+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
25+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
26+
OTEL_SPAN_EVENT_COUNT_LIMIT,
27+
OTEL_SPAN_LINK_COUNT_LIMIT,
28+
)
29+
from opentelemetry.sdk.trace import (
30+
SpanLimits,
31+
)
32+
33+
34+
class TestSpanLimits(unittest.TestCase):
35+
def test_non_integer_env_vars_raise(self):
36+
test_cases = [
37+
(env_val, "bad")
38+
for env_val in [
39+
OTEL_SPAN_EVENT_COUNT_LIMIT,
40+
OTEL_SPAN_LINK_COUNT_LIMIT,
41+
OTEL_ATTRIBUTE_COUNT_LIMIT,
42+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
43+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
44+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
45+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
46+
]
47+
]
48+
49+
for env_var, bad_value in test_cases:
50+
with self.subTest(env_var=env_var, value=bad_value):
51+
env = {env_var: bad_value}
52+
53+
with self.assertRaises(ValueError) as error, patch.dict(
54+
"os.environ", env
55+
):
56+
SpanLimits()
57+
58+
expected_msg = f"{env_var} must be a non-negative integer but got {bad_value}"
59+
self.assertEqual(
60+
expected_msg,
61+
str(error.exception),
62+
f"Unexpected error message for {env_var}={bad_value}",
63+
)
64+
65+
def test_negative_integer_env_vars_raise(self):
66+
test_cases = [
67+
(env_val, "-1")
68+
for env_val in [
69+
OTEL_SPAN_EVENT_COUNT_LIMIT,
70+
OTEL_SPAN_LINK_COUNT_LIMIT,
71+
OTEL_ATTRIBUTE_COUNT_LIMIT,
72+
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
73+
OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT,
74+
OTEL_LINK_ATTRIBUTE_COUNT_LIMIT,
75+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
76+
]
77+
]
78+
79+
for env_var, invalid_value in test_cases:
80+
with self.subTest(env_var=env_var, value=invalid_value):
81+
env = {env_var: invalid_value}
82+
83+
with self.assertRaises(ValueError) as error, patch.dict(
84+
"os.environ", env
85+
):
86+
SpanLimits()
87+
88+
expected_msg = f"{env_var} must be a non-negative integer but got {invalid_value}"
89+
self.assertEqual(
90+
expected_msg,
91+
str(error.exception),
92+
f"Unexpected error message for {env_var}={invalid_value}",
93+
)

0 commit comments

Comments
 (0)