Skip to content

Commit 99753f9

Browse files
committed
address review feedback: single coercion table, simplify attributes merge
- collapse _SCALAR_COERCIONS and _ARRAY_COERCIONS into a single _COERCIONS dict using an _array() factory, reducing _coerce_attribute_value to two lines - process attributes_list before attributes so explicit attributes naturally overwrite list entries without needing an explicit guard Assisted-by: Claude Sonnet 4.6
1 parent 6ed3425 commit 99753f9

File tree

1 file changed

+17
-31
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/_configuration

1 file changed

+17
-31
lines changed

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

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,27 @@ def _coerce_bool(value: object) -> bool:
3838
return bool(value)
3939

4040

41-
# Dispatch table for scalar type coercions
42-
_SCALAR_COERCIONS = {
41+
def _array(coerce: object) -> object:
42+
return lambda value: [coerce(item) for item in value] # type: ignore[operator]
43+
44+
45+
# Unified dispatch table for all attribute type coercions
46+
_COERCIONS = {
4347
AttributeType.string: str,
4448
AttributeType.int: int,
4549
AttributeType.double: float,
4650
AttributeType.bool: _coerce_bool,
47-
}
48-
49-
# Dispatch table for array type coercions
50-
_ARRAY_COERCIONS = {
51-
AttributeType.string_array: str,
52-
AttributeType.bool_array: _coerce_bool,
53-
AttributeType.int_array: int,
54-
AttributeType.double_array: float,
51+
AttributeType.string_array: _array(str),
52+
AttributeType.int_array: _array(int),
53+
AttributeType.double_array: _array(float),
54+
AttributeType.bool_array: _array(_coerce_bool),
5555
}
5656

5757

5858
def _coerce_attribute_value(attr: AttributeNameValue) -> object:
5959
"""Coerce an attribute value to the correct Python type based on AttributeType."""
60-
value = attr.value
61-
attr_type = attr.type
62-
63-
if attr_type is None:
64-
return value
65-
scalar_coercer = _SCALAR_COERCIONS.get(attr_type)
66-
if scalar_coercer is not None:
67-
return scalar_coercer(value) # type: ignore[arg-type]
68-
array_coercer = _ARRAY_COERCIONS.get(attr_type)
69-
if array_coercer is not None:
70-
return [array_coercer(item) for item in value] # type: ignore[union-attr,arg-type]
71-
return value
60+
coerce = _COERCIONS.get(attr.type) # type: ignore[arg-type]
61+
return coerce(attr.value) if coerce is not None else attr.value # type: ignore[operator]
7262

7363

7464
def _parse_attributes_list(attributes_list: str) -> dict[str, str]:
@@ -112,20 +102,16 @@ def create_resource(config: Optional[ResourceConfig]) -> Resource:
112102
service_resource = Resource({SERVICE_NAME: "unknown_service"})
113103
return base.merge(service_resource)
114104

115-
# Build attributes from config.attributes list
105+
# attributes_list is lower priority; process it first so that explicit
106+
# attributes can simply overwrite any conflicting keys.
116107
config_attrs: dict[str, object] = {}
108+
if config.attributes_list:
109+
config_attrs.update(_parse_attributes_list(config.attributes_list))
110+
117111
if config.attributes:
118112
for attr in config.attributes:
119113
config_attrs[attr.name] = _coerce_attribute_value(attr)
120114

121-
# Parse attributes_list (key=value,key=value string format)
122-
if config.attributes_list:
123-
list_attrs = _parse_attributes_list(config.attributes_list)
124-
# attributes_list entries do not override explicit attributes
125-
for attr_key, attr_val in list_attrs.items():
126-
if attr_key not in config_attrs:
127-
config_attrs[attr_key] = attr_val
128-
129115
schema_url = config.schema_url
130116

131117
config_resource = Resource(config_attrs, schema_url) # type: ignore[arg-type]

0 commit comments

Comments
 (0)