|
1 | 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
| 4 | +import os |
4 | 5 | import unittest |
5 | | -from unittest.mock import Mock |
| 6 | +from unittest.mock import Mock, patch |
6 | 7 |
|
7 | 8 | from amazon.opentelemetry.distro.exporter.aws.metrics.base_emf_exporter import BaseEmfExporter, MetricRecord |
8 | 9 | from opentelemetry.sdk.metrics.export import MetricExportResult |
@@ -286,6 +287,173 @@ def test_export_failure_handling(self): |
286 | 287 | result = self.exporter.export(metrics_data) |
287 | 288 | self.assertEqual(result, MetricExportResult.FAILURE) |
288 | 289 |
|
| 290 | + def test_has_dimension_case_insensitive(self): |
| 291 | + """Test case-insensitive dimension checking.""" |
| 292 | + dimension_names = ["Service", "Environment", "operation"] |
| 293 | + |
| 294 | + # Exact match |
| 295 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "Service")) |
| 296 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "Environment")) |
| 297 | + |
| 298 | + # Case variations |
| 299 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "service")) |
| 300 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "SERVICE")) |
| 301 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "environment")) |
| 302 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "ENVIRONMENT")) |
| 303 | + self.assertTrue(self.exporter._has_dimension_case_insensitive(dimension_names, "OPERATION")) |
| 304 | + |
| 305 | + # Non-existent dimension |
| 306 | + self.assertFalse(self.exporter._has_dimension_case_insensitive(dimension_names, "NotExists")) |
| 307 | + |
| 308 | + # Empty list |
| 309 | + self.assertFalse(self.exporter._has_dimension_case_insensitive([], "Service")) |
| 310 | + |
| 311 | + def test_add_application_signals_dimensions_disabled(self): |
| 312 | + """Test that dimensions are not added when feature is disabled.""" |
| 313 | + # Default exporter has feature disabled |
| 314 | + dimension_names = ["operation"] |
| 315 | + emf_log = {} |
| 316 | + resource = Resource.create({"service.name": "my-service", "deployment.environment": "production"}) |
| 317 | + |
| 318 | + self.exporter._add_application_signals_dimensions(dimension_names, emf_log, resource) |
| 319 | + |
| 320 | + # Dimensions should not be added |
| 321 | + self.assertEqual(dimension_names, ["operation"]) |
| 322 | + self.assertNotIn("Service", emf_log) |
| 323 | + self.assertNotIn("Environment", emf_log) |
| 324 | + |
| 325 | + @patch.dict( |
| 326 | + os.environ, |
| 327 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 328 | + ) |
| 329 | + def test_add_application_signals_dimensions_enabled(self): |
| 330 | + """Test that dimensions are added when feature is enabled.""" |
| 331 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 332 | + dimension_names = ["operation"] |
| 333 | + emf_log = {} |
| 334 | + resource = Resource.create({"service.name": "my-service", "deployment.environment": "production"}) |
| 335 | + |
| 336 | + exporter._add_application_signals_dimensions(dimension_names, emf_log, resource) |
| 337 | + |
| 338 | + # Service and Environment should be added at the beginning |
| 339 | + self.assertEqual(dimension_names, ["Service", "Environment", "operation"]) |
| 340 | + self.assertEqual(emf_log["Service"], "my-service") |
| 341 | + self.assertEqual(emf_log["Environment"], "production") |
| 342 | + |
| 343 | + @patch.dict( |
| 344 | + os.environ, |
| 345 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 346 | + ) |
| 347 | + def test_add_application_signals_dimensions_fallback_values(self): |
| 348 | + """Test fallback values when resource attributes are not available.""" |
| 349 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 350 | + dimension_names = ["operation"] |
| 351 | + emf_log = {} |
| 352 | + # Resource without deployment.environment |
| 353 | + resource = Resource.create({"service.name": "my-service"}) |
| 354 | + |
| 355 | + exporter._add_application_signals_dimensions(dimension_names, emf_log, resource) |
| 356 | + |
| 357 | + # Service should use service.name, Environment should fallback to lambda:default |
| 358 | + self.assertIn("Service", dimension_names) |
| 359 | + self.assertIn("Environment", dimension_names) |
| 360 | + self.assertEqual(emf_log["Service"], "my-service") |
| 361 | + self.assertEqual(emf_log["Environment"], "lambda:default") |
| 362 | + |
| 363 | + @patch.dict( |
| 364 | + os.environ, |
| 365 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 366 | + ) |
| 367 | + def test_add_application_signals_dimensions_no_resource(self): |
| 368 | + """Test fallback when resource is None.""" |
| 369 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 370 | + dimension_names = ["operation"] |
| 371 | + emf_log = {} |
| 372 | + |
| 373 | + exporter._add_application_signals_dimensions(dimension_names, emf_log, None) |
| 374 | + |
| 375 | + # Should use fallback values |
| 376 | + self.assertIn("Service", dimension_names) |
| 377 | + self.assertIn("Environment", dimension_names) |
| 378 | + self.assertEqual(emf_log["Service"], "UnknownService") |
| 379 | + self.assertEqual(emf_log["Environment"], "lambda:default") |
| 380 | + |
| 381 | + @patch.dict( |
| 382 | + os.environ, |
| 383 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 384 | + ) |
| 385 | + def test_add_application_signals_dimensions_service_already_set(self): |
| 386 | + """Test that Service dimension is not overwritten if already set (case-insensitive).""" |
| 387 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 388 | + |
| 389 | + # User has set "service" (lowercase) |
| 390 | + dimension_names = ["service", "operation"] |
| 391 | + emf_log = {"service": "user-service"} |
| 392 | + resource = Resource.create({"service.name": "my-service", "deployment.environment": "production"}) |
| 393 | + |
| 394 | + exporter._add_application_signals_dimensions(dimension_names, emf_log, resource) |
| 395 | + |
| 396 | + # Service should NOT be added (case-insensitive match), but Environment should be |
| 397 | + self.assertIn("Environment", dimension_names) |
| 398 | + self.assertNotIn("Service", dimension_names) # "Service" not added because "service" exists |
| 399 | + self.assertEqual(emf_log.get("service"), "user-service") # User value preserved |
| 400 | + self.assertEqual(emf_log.get("Environment"), "production") |
| 401 | + |
| 402 | + @patch.dict( |
| 403 | + os.environ, |
| 404 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 405 | + ) |
| 406 | + def test_add_application_signals_dimensions_environment_already_set(self): |
| 407 | + """Test that Environment dimension is not overwritten if already set (case-insensitive).""" |
| 408 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 409 | + |
| 410 | + # User has set "ENVIRONMENT" (uppercase) |
| 411 | + dimension_names = ["ENVIRONMENT", "operation"] |
| 412 | + emf_log = {"ENVIRONMENT": "user-environment"} |
| 413 | + resource = Resource.create({"service.name": "my-service", "deployment.environment": "production"}) |
| 414 | + |
| 415 | + exporter._add_application_signals_dimensions(dimension_names, emf_log, resource) |
| 416 | + |
| 417 | + # Environment should NOT be added (case-insensitive match), but Service should be |
| 418 | + self.assertIn("Service", dimension_names) |
| 419 | + self.assertNotIn("Environment", dimension_names) # "Environment" not added because "ENVIRONMENT" exists |
| 420 | + self.assertEqual(emf_log.get("Service"), "my-service") |
| 421 | + self.assertEqual(emf_log.get("ENVIRONMENT"), "user-environment") # User value preserved |
| 422 | + |
| 423 | + @patch.dict( |
| 424 | + os.environ, |
| 425 | + {"OTEL_AWS_APPLICATION_SIGNALS_ENABLED": "true", "OTEL_AWS_APPLICATION_SIGNALS_EMF_EXPORT_ENABLED": "true"}, |
| 426 | + ) |
| 427 | + def test_create_emf_log_with_application_signals_enabled(self): |
| 428 | + """Test EMF log creation with Application Signals EMF export enabled.""" |
| 429 | + exporter = ConcreteEmfExporter(namespace="TestNamespace") |
| 430 | + |
| 431 | + record = exporter._create_metric_record("test_metric", "Count", "Test") |
| 432 | + record.value = 50.0 |
| 433 | + record.timestamp = 1234567890 |
| 434 | + record.attributes = {"operation": "test"} |
| 435 | + |
| 436 | + records = [record] |
| 437 | + resource = Resource.create( |
| 438 | + { |
| 439 | + "service.name": "test-service", |
| 440 | + "deployment.environment": "production", |
| 441 | + } |
| 442 | + ) |
| 443 | + |
| 444 | + result = exporter._create_emf_log(records, resource, 1234567890) |
| 445 | + |
| 446 | + # Check that Service and Environment dimensions were added |
| 447 | + self.assertEqual(result["Service"], "test-service") |
| 448 | + self.assertEqual(result["Environment"], "production") |
| 449 | + |
| 450 | + # Check CloudWatch metrics dimensions include Service and Environment |
| 451 | + cw_metrics = result["_aws"]["CloudWatchMetrics"][0] |
| 452 | + dimensions = cw_metrics["Dimensions"][0] |
| 453 | + self.assertIn("Service", dimensions) |
| 454 | + self.assertIn("Environment", dimensions) |
| 455 | + self.assertIn("operation", dimensions) |
| 456 | + |
289 | 457 |
|
290 | 458 | if __name__ == "__main__": |
291 | 459 | unittest.main() |
0 commit comments