Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e58b8a

Browse files
authored
api: Handle boolean, integer and float values in Configuration (open-telemetry#662)
Many times, configuration values are just boolean values. The Configuration object now only returns the literal configuration value. It would be useful to have this object return True instead of "True" to keep code a bit shorter
1 parent 7eccae2 commit 9e58b8a

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

ext/opentelemetry-ext-django/src/opentelemetry/ext/django/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _instrument(self, **kwargs):
4141
# built inside the Configuration class itself with the magic method
4242
# __bool__
4343

44-
if Configuration().DJANGO_INSTRUMENT != "True":
44+
if not Configuration().DJANGO_INSTRUMENT:
4545
return
4646

4747
# This can not be solved, but is an inherent problem of this approach:

opentelemetry-api/src/opentelemetry/configuration/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
OpenTelemetry API provided providers are the default ones used if no
7979
configuration is found in the environment variables).
8080
81+
Configuration values that are exactly ``"True"`` or ``"False"`` will be
82+
converted to its boolean values of ``True`` and ``False`` respectively.
83+
84+
Configuration values that can be casted to integers or floats will be casted.
85+
8186
This object can be used by any OpenTelemetry component, native or external.
8287
For that reason, the ``Configuration`` object is designed to be immutable.
8388
If a component would change the value of one of the ``Configuration`` object
@@ -110,6 +115,20 @@ def __new__(cls) -> "Configuration":
110115

111116
key = match.group(1)
112117

118+
if value == "True":
119+
value = True
120+
elif value == "False":
121+
value = False
122+
else:
123+
try:
124+
value = int(value)
125+
except ValueError:
126+
pass
127+
try:
128+
value = float(value)
129+
except ValueError:
130+
pass
131+
113132
setattr(Configuration, "_{}".format(key), value)
114133
setattr(
115134
Configuration,

opentelemetry-api/tests/configuration/test_configuration.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,58 @@ def test_reset(self):
8888
self.assertIsNone(
8989
Configuration().TRACER_PROVIDER
9090
) # pylint: disable=no-member
91+
92+
@patch.dict(
93+
"os.environ", # type: ignore
94+
{
95+
"OPENTELEMETRY_PYTHON_TRUE": "True",
96+
"OPENTELEMETRY_PYTHON_FALSE": "False",
97+
},
98+
)
99+
def test_boolean(self):
100+
self.assertIsInstance(
101+
Configuration().TRUE, bool
102+
) # pylint: disable=no-member
103+
self.assertIsInstance(
104+
Configuration().FALSE, bool
105+
) # pylint: disable=no-member
106+
self.assertTrue(Configuration().TRUE) # pylint: disable=no-member
107+
self.assertFalse(Configuration().FALSE) # pylint: disable=no-member
108+
109+
@patch.dict(
110+
"os.environ", # type: ignore
111+
{
112+
"OPENTELEMETRY_PYTHON_POSITIVE_INTEGER": "123",
113+
"OPENTELEMETRY_PYTHON_NEGATIVE_INTEGER": "-123",
114+
"OPENTELEMETRY_PYTHON_NON_INTEGER": "-12z3",
115+
},
116+
)
117+
def test_integer(self):
118+
self.assertEqual(
119+
Configuration().POSITIVE_INTEGER, 123
120+
) # pylint: disable=no-member
121+
self.assertEqual(
122+
Configuration().NEGATIVE_INTEGER, -123
123+
) # pylint: disable=no-member
124+
self.assertEqual(
125+
Configuration().NON_INTEGER, "-12z3"
126+
) # pylint: disable=no-member
127+
128+
@patch.dict(
129+
"os.environ", # type: ignore
130+
{
131+
"OPENTELEMETRY_PYTHON_POSITIVE_FLOAT": "123.123",
132+
"OPENTELEMETRY_PYTHON_NEGATIVE_FLOAT": "-123.123",
133+
"OPENTELEMETRY_PYTHON_NON_FLOAT": "-12z3.123",
134+
},
135+
)
136+
def test_float(self):
137+
self.assertEqual(
138+
Configuration().POSITIVE_FLOAT, 123.123
139+
) # pylint: disable=no-member
140+
self.assertEqual(
141+
Configuration().NEGATIVE_FLOAT, -123.123
142+
) # pylint: disable=no-member
143+
self.assertEqual(
144+
Configuration().NON_FLOAT, "-12z3.123"
145+
) # pylint: disable=no-member

0 commit comments

Comments
 (0)