Skip to content

Commit 506d816

Browse files
committed
address review feedback: use _DEFAULT_RESOURCE, fix bool_array coercion
- replace _sdk_default_attributes() with _DEFAULT_RESOURCE from resources module - move _coerce_bool into dispatch tables for both scalar and array bool types, fixing a bug where bool_array with string values like "false" would coerce incorrectly via plain bool() (non-empty string -> True) - add test for bool_array with string values to cover the bug Assisted-by: Claude Sonnet 4.6
1 parent 8329ae4 commit 506d816

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/_resource.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,44 @@
2424
)
2525
from opentelemetry.sdk._configuration.models import Resource as ResourceConfig
2626
from opentelemetry.sdk.resources import (
27-
_OPENTELEMETRY_SDK_VERSION,
2827
SERVICE_NAME,
29-
TELEMETRY_SDK_LANGUAGE,
30-
TELEMETRY_SDK_NAME,
31-
TELEMETRY_SDK_VERSION,
3228
Resource,
29+
_DEFAULT_RESOURCE,
3330
)
3431

3532
_logger = logging.getLogger(__name__)
3633

34+
35+
def _coerce_bool(value: object) -> bool:
36+
if isinstance(value, str):
37+
return value.lower() not in ("false", "0", "")
38+
return bool(value)
39+
40+
3741
# Dispatch table for scalar type coercions
3842
_SCALAR_COERCIONS = {
3943
AttributeType.string: str,
4044
AttributeType.int: int,
4145
AttributeType.double: float,
46+
AttributeType.bool: _coerce_bool,
4247
}
4348

4449
# Dispatch table for array type coercions
4550
_ARRAY_COERCIONS = {
4651
AttributeType.string_array: str,
47-
AttributeType.bool_array: bool,
52+
AttributeType.bool_array: _coerce_bool,
4853
AttributeType.int_array: int,
4954
AttributeType.double_array: float,
5055
}
5156

5257

53-
def _coerce_bool(value: object) -> bool:
54-
if isinstance(value, str):
55-
return value.lower() not in ("false", "0", "")
56-
return bool(value)
57-
58-
5958
def _coerce_attribute_value(attr: AttributeNameValue) -> object:
6059
"""Coerce an attribute value to the correct Python type based on AttributeType."""
6160
value = attr.value
6261
attr_type = attr.type
6362

6463
if attr_type is None:
6564
return value
66-
if attr_type == AttributeType.bool:
67-
return _coerce_bool(value)
6865
scalar_coercer = _SCALAR_COERCIONS.get(attr_type)
6966
if scalar_coercer is not None:
7067
return scalar_coercer(value) # type: ignore[arg-type]
@@ -96,15 +93,6 @@ def _parse_attributes_list(attributes_list: str) -> dict[str, str]:
9693
return result
9794

9895

99-
def _sdk_default_attributes() -> dict[str, str]:
100-
"""Return the SDK telemetry attributes (equivalent to Java's Resource.getDefault())."""
101-
return {
102-
TELEMETRY_SDK_LANGUAGE: "python",
103-
TELEMETRY_SDK_NAME: "opentelemetry",
104-
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
105-
}
106-
107-
10896
def create_resource(config: Optional[ResourceConfig]) -> Resource:
10997
"""Create an SDK Resource from declarative config.
11098
@@ -118,7 +106,7 @@ def create_resource(config: Optional[ResourceConfig]) -> Resource:
118106
Returns:
119107
A Resource with SDK defaults merged with any config-specified attributes.
120108
"""
121-
base = Resource(_sdk_default_attributes())
109+
base = _DEFAULT_RESOURCE
122110

123111
if config is None:
124112
service_resource = Resource({SERVICE_NAME: "unknown_service"})

opentelemetry-sdk/tests/_configuration/test_resource.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ def test_attribute_type_bool_array(self):
223223
resource = create_resource(config)
224224
self.assertEqual(list(resource.attributes["k"]), [True, False]) # type: ignore[arg-type]
225225

226+
def test_attribute_type_bool_array_string_values(self):
227+
"""bool_array must use _coerce_bool, not plain bool() — 'false' must be False."""
228+
config = ResourceConfig(
229+
attributes=[
230+
AttributeNameValue(
231+
name="k",
232+
value=["true", "false"],
233+
type=AttributeType.bool_array,
234+
)
235+
]
236+
)
237+
resource = create_resource(config)
238+
self.assertEqual(list(resource.attributes["k"]), [True, False]) # type: ignore[arg-type]
239+
226240

227241
class TestCreateResourceAttributesList(unittest.TestCase):
228242
def test_attributes_list_parsed(self):

0 commit comments

Comments
 (0)