Override otel error tags for expected 404 Not Found responses 25/143125/2
authorFiete Ostkamp <fiete.ostkamp@telekom.de>
Sun, 8 Feb 2026 10:27:11 +0000 (11:27 +0100)
committerFiete Ostkamp <fiete.ostkamp@telekom.de>
Sun, 8 Feb 2026 14:18:30 +0000 (15:18 +0100)
Issue-ID: INT-2347
Change-Id: Ic19e9f5000599731e2f543616c174e806689a03c
Signed-off-by: Fiete Ostkamp <fiete.ostkamp@telekom.de>
src/onaptests/steps/cloud/cloud_region_create.py
src/onaptests/utils/tracing.py [new file with mode: 0644]

index afed216..1c92a05 100644 (file)
@@ -1,8 +1,10 @@
 """A&AI cloud region creation module."""
+
 from onapsdk.aai.cloud_infrastructure import CloudRegion
 from onapsdk.configuration import settings
 from onapsdk.exceptions import ResourceNotFound
 
+from ...utils.tracing import otel_expect_not_found
 from ..base import BaseStep
 
 
@@ -44,6 +46,7 @@ class CloudRegionCreateStep(BaseStep):
                 cloud_region_id=settings.CLOUD_REGION_ID,
             )
         except ResourceNotFound:
+            otel_expect_not_found()
             CloudRegion.create(
                 cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER,
                 cloud_region_id=settings.CLOUD_REGION_ID,
@@ -52,12 +55,11 @@ class CloudRegionCreateStep(BaseStep):
                 cloud_type=settings.CLOUD_REGION_TYPE,
                 cloud_region_version=settings.CLOUD_REGION_VERSION,
                 owner_defined_type=settings.CLOUD_OWNER_DEFINED_TYPE,
-                complex_name=settings.COMPLEX_PHYSICAL_LOCATION_ID
+                complex_name=settings.COMPLEX_PHYSICAL_LOCATION_ID,
             )
 
     @BaseStep.store_state(cleanup=True)
     def cleanup(self) -> None:
-
         """Cleanup created cloud region."""
         self._logger.info("Clean the cloud region")
         try:
diff --git a/src/onaptests/utils/tracing.py b/src/onaptests/utils/tracing.py
new file mode 100644 (file)
index 0000000..54c8c9a
--- /dev/null
@@ -0,0 +1,24 @@
+"""OpenTelemetry tracing utilities."""
+
+from opentelemetry import trace
+
+
+def otel_expect_not_found() -> None:
+    """Mark the current span to indicate that a 404/ResourceNotFound is expected.
+
+    This prevents OpenTelemetry from automatically marking the span as an error
+    when a ResourceNotFound exception (404 HTTP status) occurs in contexts where
+    this is the expected behavior (e.g., checking if a resource exists before creating it).
+
+    Example:
+        try:
+            resource = SomeResource.get_by_id(resource_id)
+        except ResourceNotFound:
+            expect_not_found()
+            # Create the resource since it doesn't exist
+            resource = SomeResource.create(...)
+    """
+    current_span = trace.get_current_span()
+    if current_span and current_span.is_recording():
+        current_span.set_attribute("error", False)
+        current_span.set_attribute("expected_404", True)