Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/instana/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
- GCROptions - Options class for Google cloud Run. Holds settings specific to GCR.
"""

import os
import logging
import os
from typing import Any, Dict

from instana.configurator import config
from instana.log import logger
from instana.util.config import (
parse_ignored_endpoints,
parse_ignored_endpoints_from_yaml,
)
from instana.util.config import (is_truthy, parse_ignored_endpoints,
parse_ignored_endpoints_from_yaml)
from instana.util.runtime import determine_service_name
from instana.configurator import config


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

if "1" in [
os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None), # deprecated
os.environ.get("INSTANA_ALLOW_ROOT_EXIT_SPAN", None),
]:
# Check if either of the environment variables is truthy
if is_truthy(os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None)) or \
is_truthy(os.environ.get("INSTANA_ALLOW_ROOT_EXIT_SPAN", None)):
self.allow_exit_as_root = True

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

if "INSTANA_KAFKA_TRACE_CORRELATION" in os.environ:
self.kafka_trace_correlation = (
os.environ["INSTANA_KAFKA_TRACE_CORRELATION"].lower() == "true"
)
self.kafka_trace_correlation = is_truthy(os.environ["INSTANA_KAFKA_TRACE_CORRELATION"])
elif isinstance(config.get("tracing"), dict) and "kafka" in config["tracing"]:
self.kafka_trace_correlation = config["tracing"]["kafka"].get(
"trace_correlation", True
Expand Down Expand Up @@ -167,8 +162,8 @@ def set_tracing(self, tracing: Dict[str, Any]) -> None:
)
and "trace-correlation" in tracing["kafka"]
):
self.kafka_trace_correlation = (
str(tracing["kafka"].get("trace-correlation", True)) == "true"
self.kafka_trace_correlation = is_truthy(
tracing["kafka"].get("trace-correlation", True)
)

if (
Expand Down
31 changes: 31 additions & 0 deletions src/instana/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,34 @@ def parse_ignored_endpoints_from_yaml(file_path: str) -> List[str]:
return ignored_endpoints
else:
return []


def is_truthy(value: Any) -> bool:
"""
Check if a value is truthy, accepting various formats.

@param value: The value to check
@return: True if the value is considered truthy, False otherwise

Accepts the following as True:
- True (Python boolean)
- "True", "true" (case-insensitive string)
- "1" (string)
- 1 (integer)
"""
if value is None:
return False

if isinstance(value, bool):
return value

if isinstance(value, int):
return value == 1

if isinstance(value, str):
value_lower = value.lower()
return value_lower == "true" or value == "1"

return False

# Made with Bob
34 changes: 27 additions & 7 deletions tests/util/test_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# (c) Copyright IBM Corp. 2025

from instana.util.config import (
parse_endpoints_of_service,
parse_ignored_endpoints,
parse_ignored_endpoints_dict,
parse_kafka_methods,
parse_service_pair,
)
import pytest

from instana.util.config import (is_truthy, parse_endpoints_of_service,
parse_ignored_endpoints,
parse_ignored_endpoints_dict,
parse_kafka_methods, parse_service_pair)


class TestConfig:
Expand Down Expand Up @@ -168,3 +167,24 @@ def test_parse_kafka_methods_as_str(self) -> None:
test_rule_as_str = ["send"]
parsed_rule = parse_kafka_methods(test_rule_as_str)
assert parsed_rule == ["kafka.send.*"]

@pytest.mark.parametrize("value, expected", [
(True, True),
(False, False),
("True", True),
("true", True),
("1", True),
(1, True),
("False", False),
("false", False),
("0", False),
(0, False),
(None, False),
("TRUE", True),
("FALSE", False),
("yes", False), # Only "true" and "1" are considered truthy
("no", False),
])
def test_is_truthy(self, value, expected) -> None:
"""Test the is_truthy function with various input values."""
assert is_truthy(value) == expected
Loading