Skip to content

Commit 5e4d512

Browse files
Refactor Lambda dimensions into helper method to reduce branches
Extract Lambda dimension logic into _add_lambda_dimensions() method to reduce the number of branches in _create_emf_log() from 13 to 12, fixing the pylint too-many-branches error. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent de9383a commit 5e4d512

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/aws/metrics/base_emf_exporter.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,30 @@ def _get_dimension_names(self, attributes: Attributes) -> List[str]:
191191
# For now, use all attributes as dimensions
192192
return list(attributes.keys())
193193

194+
def _add_lambda_dimensions(
195+
self, emf_log: Dict, dimension_names: List[str], resource: Resource
196+
) -> List[str]:
197+
"""
198+
Add Service and Environment dimensions for Lambda environments.
199+
200+
Args:
201+
emf_log: The EMF log dictionary to add dimension values to
202+
dimension_names: The current list of dimension names
203+
resource: The OTel Resource to extract service name from
204+
205+
Returns:
206+
Updated list of dimension names with Service and Environment prepended (if Lambda)
207+
"""
208+
if not self._is_lambda:
209+
return dimension_names
210+
211+
service_name, _ = get_service_attribute(resource) if resource else ("UnknownService", True)
212+
# Add dimension values to the EMF log root
213+
emf_log["Service"] = service_name
214+
emf_log["Environment"] = LAMBDA_ENVIRONMENT_DEFAULT
215+
# Add Service and Environment to dimension names (at the beginning for consistency)
216+
return ["Service", "Environment"] + dimension_names
217+
194218
def _get_attributes_key(self, attributes: Attributes) -> str:
195219
"""
196220
Create a hashable key from attributes for grouping metrics.
@@ -501,13 +525,7 @@ def _create_emf_log(
501525
emf_log[name] = str(value)
502526

503527
# Add Service and Environment dimensions for Lambda
504-
if self._is_lambda:
505-
service_name, _ = get_service_attribute(resource) if resource else ("UnknownService", True)
506-
# Add Service and Environment to dimension names (at the beginning for consistency)
507-
dimension_names = ["Service", "Environment"] + dimension_names
508-
# Add dimension values to the EMF log root
509-
emf_log["Service"] = service_name
510-
emf_log["Environment"] = LAMBDA_ENVIRONMENT_DEFAULT
528+
dimension_names = self._add_lambda_dimensions(emf_log, dimension_names, resource)
511529

512530
# Add CloudWatch Metrics if we have metrics, include dimensions only if they exist
513531
if metric_definitions:

0 commit comments

Comments
 (0)