Skip to content

Commit

Permalink
Handle invalid status codes in standard metric payload (#35762)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed May 24, 2024
1 parent 8e7522f commit bd5899c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bd5899c

Please sign in to comment.