Skip to content

Commit bd5899c

Browse files
authored
Handle invalid status codes in standard metric payload (#35762)
1 parent 8e7522f commit bd5899c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
### Bugs Fixed
1010

11+
- Handle invalid status codes in std metric payload
12+
([#35762](https://github.com/Azure/azure-sdk-for-python/pull/35762))
13+
1114
### Other Changes
1215

1316
- Update live metrics to use typespec generated swagger

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,13 @@ def _handle_std_metric_envelope(
274274

275275

276276
def _is_status_code_success(status_code: Optional[str], threshold: int) -> bool:
277-
return status_code is not None and int(status_code) < threshold
277+
if status_code is None:
278+
return False
279+
try:
280+
return int(status_code) < threshold
281+
except ValueError:
282+
return False
283+
278284

279285
def _is_metric_namespace_opted_in() -> bool:
280286
return os.environ.get(_APPLICATIONINSIGHTS_METRIC_NAMESPACE_OPT_IN, "False").lower() == "true"

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/metrics/test_metrics.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ def test_point_to_envelope_std_metric_client_duration(self):
344344
envelope = exporter._point_to_envelope(point, "http.client.duration", resource)
345345
self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False")
346346

347+
point.attributes["http.status_code"] = None
348+
envelope = exporter._point_to_envelope(point, "http.client.duration", resource)
349+
self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False")
350+
351+
point.attributes["http.status_code"] = "None"
352+
envelope = exporter._point_to_envelope(point, "http.client.duration", resource)
353+
self.assertEqual(envelope.data.base_data.properties['Dependency.Success'], "False")
354+
347355

348356
def test_point_to_envelope_std_metric_server_duration(self):
349357
exporter = self._exporter
@@ -382,6 +390,15 @@ def test_point_to_envelope_std_metric_server_duration(self):
382390
envelope = exporter._point_to_envelope(point, "http.server.duration", resource)
383391
self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False")
384392

393+
point.attributes["http.status_code"] = None
394+
envelope = exporter._point_to_envelope(point, "http.server.duration", resource)
395+
self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False")
396+
397+
point.attributes["http.status_code"] = "None"
398+
envelope = exporter._point_to_envelope(point, "http.server.duration", resource)
399+
self.assertEqual(envelope.data.base_data.properties['Request.Success'], "False")
400+
401+
385402
def test_point_to_envelope_std_metric_unsupported(self):
386403
exporter = self._exporter
387404
resource = Resource(

0 commit comments

Comments
 (0)