From bd5899c0f6c11902c612528c577a56d7700c6755 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Fri, 24 May 2024 13:13:18 -0700 Subject: [PATCH] Handle invalid status codes in standard metric payload (#35762) --- .../CHANGELOG.md | 3 +++ .../exporter/export/metrics/_exporter.py | 8 +++++++- .../tests/metrics/test_metrics.py | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index 195d9b976a54..62e2a8db6859 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -8,6 +8,9 @@ ### Bugs Fixed +- Handle invalid status codes in std metric payload + ([#35762](https://github.com/Azure/azure-sdk-for-python/pull/35762)) + ### Other Changes - Update live metrics to use typespec generated swagger diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py index 0485a20fb49a..3cb6b68baf5e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py @@ -274,7 +274,13 @@ def _handle_std_metric_envelope( def _is_status_code_success(status_code: Optional[str], threshold: int) -> bool: - return status_code is not None and int(status_code) < threshold + if status_code is None: + return False + try: + return int(status_code) < threshold + except ValueError: + return False + def _is_metric_namespace_opted_in() -> bool: return os.environ.get(_APPLICATIONINSIGHTS_METRIC_NAMESPACE_OPT_IN, "False").lower() == "true" diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/metrics/test_metrics.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/metrics/test_metrics.py index 024c358e6b16..e6bdc663deed 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/metrics/test_metrics.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/metrics/test_metrics.py @@ -344,6 +344,14 @@ def test_point_to_envelope_std_metric_client_duration(self): envelope = exporter._point_to_envelope(point, "http.client.duration", resource) self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False") + point.attributes["http.status_code"] = None + envelope = exporter._point_to_envelope(point, "http.client.duration", resource) + self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False") + + point.attributes["http.status_code"] = "None" + envelope = exporter._point_to_envelope(point, "http.client.duration", resource) + self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False") + def test_point_to_envelope_std_metric_server_duration(self): exporter = self._exporter @@ -382,6 +390,15 @@ def test_point_to_envelope_std_metric_server_duration(self): envelope = exporter._point_to_envelope(point, "http.server.duration", resource) self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False") + point.attributes["http.status_code"] = None + envelope = exporter._point_to_envelope(point, "http.server.duration", resource) + self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False") + + point.attributes["http.status_code"] = "None" + envelope = exporter._point_to_envelope(point, "http.server.duration", resource) + self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False") + + def test_point_to_envelope_std_metric_unsupported(self): exporter = self._exporter resource = Resource(