Skip to content

Commit ddceea5

Browse files
committed
feat(config): add support for multiple boolean formats in environment variables.
Add `is_truthy()` function to accept `True`, `true`, and `1` as boolean `True` values when reading environment variables. This makes configuration more flexible and user-friendly. - Add new `is_truthy()` utility function in config.py - Update environment variable checks in options.py to use the new function - Add parametrized tests to verify functionality with various input values Signed-off-by: Paulo Vital <[email protected]>
1 parent 7e58965 commit ddceea5

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

src/instana/options.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
- GCROptions - Options class for Google cloud Run. Holds settings specific to GCR.
1515
"""
1616

17-
import os
1817
import logging
18+
import os
1919
from typing import Any, Dict
2020

21+
from instana.configurator import config
2122
from instana.log import logger
22-
from instana.util.config import (
23-
parse_ignored_endpoints,
24-
parse_ignored_endpoints_from_yaml,
25-
)
23+
from instana.util.config import (is_truthy, parse_ignored_endpoints,
24+
parse_ignored_endpoints_from_yaml)
2625
from instana.util.runtime import determine_service_name
27-
from instana.configurator import config
2826

2927

3028
class BaseOptions(object):
@@ -76,10 +74,9 @@ def set_trace_configurations(self) -> None:
7674
str(os.environ["INSTANA_EXTRA_HTTP_HEADERS"]).lower().split(";")
7775
)
7876

79-
if "1" in [
80-
os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None), # deprecated
81-
os.environ.get("INSTANA_ALLOW_ROOT_EXIT_SPAN", None),
82-
]:
77+
# Check if either of the environment variables is truthy
78+
if is_truthy(os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None)) or \
79+
is_truthy(os.environ.get("INSTANA_ALLOW_ROOT_EXIT_SPAN", None)):
8380
self.allow_exit_as_root = True
8481

8582
# The priority is as follows:
@@ -102,9 +99,7 @@ def set_trace_configurations(self) -> None:
10299
)
103100

104101
if "INSTANA_KAFKA_TRACE_CORRELATION" in os.environ:
105-
self.kafka_trace_correlation = (
106-
os.environ["INSTANA_KAFKA_TRACE_CORRELATION"].lower() == "true"
107-
)
102+
self.kafka_trace_correlation = is_truthy(os.environ["INSTANA_KAFKA_TRACE_CORRELATION"])
108103
elif isinstance(config.get("tracing"), dict) and "kafka" in config["tracing"]:
109104
self.kafka_trace_correlation = config["tracing"]["kafka"].get(
110105
"trace_correlation", True
@@ -167,8 +162,8 @@ def set_tracing(self, tracing: Dict[str, Any]) -> None:
167162
)
168163
and "trace-correlation" in tracing["kafka"]
169164
):
170-
self.kafka_trace_correlation = (
171-
str(tracing["kafka"].get("trace-correlation", True)) == "true"
165+
self.kafka_trace_correlation = is_truthy(
166+
tracing["kafka"].get("trace-correlation", True)
172167
)
173168

174169
if (

src/instana/util/config.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,34 @@ def parse_ignored_endpoints_from_yaml(file_path: str) -> List[str]:
146146
return ignored_endpoints
147147
else:
148148
return []
149+
150+
151+
def is_truthy(value: Any) -> bool:
152+
"""
153+
Check if a value is truthy, accepting various formats.
154+
155+
@param value: The value to check
156+
@return: True if the value is considered truthy, False otherwise
157+
158+
Accepts the following as True:
159+
- True (Python boolean)
160+
- "True", "true" (case-insensitive string)
161+
- "1" (string)
162+
- 1 (integer)
163+
"""
164+
if value is None:
165+
return False
166+
167+
if isinstance(value, bool):
168+
return value
169+
170+
if isinstance(value, int):
171+
return value == 1
172+
173+
if isinstance(value, str):
174+
value_lower = value.lower()
175+
return value_lower == "true" or value == "1"
176+
177+
return False
178+
179+
# Made with Bob

tests/util/test_config.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# (c) Copyright IBM Corp. 2025
22

3-
from instana.util.config import (
4-
parse_endpoints_of_service,
5-
parse_ignored_endpoints,
6-
parse_ignored_endpoints_dict,
7-
parse_kafka_methods,
8-
parse_service_pair,
9-
)
3+
import pytest
4+
5+
from instana.util.config import (is_truthy, parse_endpoints_of_service,
6+
parse_ignored_endpoints,
7+
parse_ignored_endpoints_dict,
8+
parse_kafka_methods, parse_service_pair)
109

1110

1211
class TestConfig:
@@ -168,3 +167,24 @@ def test_parse_kafka_methods_as_str(self) -> None:
168167
test_rule_as_str = ["send"]
169168
parsed_rule = parse_kafka_methods(test_rule_as_str)
170169
assert parsed_rule == ["kafka.send.*"]
170+
171+
@pytest.mark.parametrize("value, expected", [
172+
(True, True),
173+
(False, False),
174+
("True", True),
175+
("true", True),
176+
("1", True),
177+
(1, True),
178+
("False", False),
179+
("false", False),
180+
("0", False),
181+
(0, False),
182+
(None, False),
183+
("TRUE", True),
184+
("FALSE", False),
185+
("yes", False), # Only "true" and "1" are considered truthy
186+
("no", False),
187+
])
188+
def test_is_truthy(self, value, expected) -> None:
189+
"""Test the is_truthy function with various input values."""
190+
assert is_truthy(value) == expected

0 commit comments

Comments
 (0)