Skip to content

Commit 07d9d0d

Browse files
authored
Fix EMF exporter to generate EMF metrics with dimension empty (#434)
*Description of changes:* * Populate the correct `CloudWatchMetrics` in EMF when metrics has empty dimension list *Test* ``` { "_aws": { "Timestamp": 1753139679038, "CloudWatchMetrics": [ { "Namespace": "MyApplication1", "Metrics": [ { "Name": "system_cpu_usage_percent", "Unit": "Percent" }, { "Name": "http_request_duration_seconds", "Unit": "Seconds" } ] } ] }, "Version": "1", "otel.resource.telemetry.sdk.language": "python", "otel.resource.telemetry.sdk.name": "opentelemetry", "otel.resource.telemetry.sdk.version": "1.33.1", "otel.resource.service.name": "my-service", "otel.resource.service.version": "0.1.0", "otel.resource.deployment.environment": "production", "system_cpu_usage_percent": 0.2, "http_request_duration_seconds": { "Values": [ 0.39614037455626866, 0.4866507397084614 ], "Counts": [ 1, 1 ], "Count": 2, "Sum": 0.8848342305738114, "Max": 0.4877384525234254, "Min": 0.397095778050386 } } ``` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 16a0a25 commit 07d9d0d

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,13 @@ def _create_emf_log(
509509
for name, value in all_attributes.items():
510510
emf_log[name] = str(value)
511511

512-
# Add the single dimension set to CloudWatch Metrics if we have dimensions and metrics
513-
if dimension_names and metric_definitions:
514-
emf_log["_aws"]["CloudWatchMetrics"].append(
515-
{"Namespace": self.namespace, "Dimensions": [dimension_names], "Metrics": metric_definitions}
516-
)
512+
# Add CloudWatch Metrics if we have metrics, include dimensions only if they exist
513+
if metric_definitions:
514+
cloudwatch_metric = {"Namespace": self.namespace, "Metrics": metric_definitions}
515+
if dimension_names:
516+
cloudwatch_metric["Dimensions"] = [dimension_names]
517+
518+
emf_log["_aws"]["CloudWatchMetrics"].append(cloudwatch_metric)
517519

518520
return emf_log
519521

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/exporter/aws/metrics/test_aws_cloudwatch_emf_exporter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,38 @@ def test_create_emf_log_with_resource(self):
391391
# Check CloudWatch metrics structure
392392
cw_metrics = result["_aws"]["CloudWatchMetrics"][0]
393393
self.assertEqual(cw_metrics["Namespace"], "TestNamespace")
394+
self.assertIn("Dimensions", cw_metrics)
394395
self.assertEqual(set(cw_metrics["Dimensions"][0]), {"env", "service"})
395396
self.assertEqual(cw_metrics["Metrics"][0]["Name"], "gauge_metric")
396397

398+
def test_create_emf_log_without_dimensions(self):
399+
"""Test EMF log creation with metrics but no dimensions."""
400+
# Create test record without attributes (no dimensions)
401+
gauge_record = self.exporter._create_metric_record("gauge_metric", "Count", "Gauge")
402+
gauge_record.value = 75.0
403+
gauge_record.timestamp = int(time.time() * 1000)
404+
gauge_record.attributes = {} # No attributes = no dimensions
405+
406+
records = [gauge_record]
407+
resource = Resource.create({"service.name": "test-service"})
408+
409+
result = self.exporter._create_emf_log(records, resource, 1234567890)
410+
411+
# Verify EMF log structure
412+
self.assertIn("_aws", result)
413+
self.assertIn("CloudWatchMetrics", result["_aws"])
414+
self.assertEqual(result["_aws"]["Timestamp"], 1234567890)
415+
self.assertEqual(result["Version"], "1")
416+
417+
# Check metric value
418+
self.assertEqual(result["gauge_metric"], 75.0)
419+
420+
# Check CloudWatch metrics structure - should have metrics but no dimensions
421+
cw_metrics = result["_aws"]["CloudWatchMetrics"][0]
422+
self.assertEqual(cw_metrics["Namespace"], "TestNamespace")
423+
self.assertNotIn("Dimensions", cw_metrics) # No dimensions should be present
424+
self.assertEqual(cw_metrics["Metrics"][0]["Name"], "gauge_metric")
425+
397426
def test_create_emf_log_skips_empty_metric_names(self):
398427
"""Test that EMF log creation skips records with empty metric names."""
399428
# Create a record with no metric name

0 commit comments

Comments
 (0)