Skip to content

Commit bab60ac

Browse files
Brian KrafftCopilot
andcommitted
fix: R2 hardening - NaN-safe JSON, isfinite guard, coupling docs
- json.dumps(allow_nan=False) prevents NaN/Infinity in JSON output - math.isfinite() guard on p99 interpolation result - ValueError/OverflowError catch in _get_request_counts - Cross-reference comment for status label coupling (execution.py) R2 review: 2 APPROVE (sec, opus), 2 REQUEST_CHANGES on documented design limitations (in-process counters, hardcoded dispatch) - v2 items. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c4faa4d commit bab60ac

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

openspace/observability/slos.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ def _get_request_counts(self) -> tuple[int, int]:
249249
250250
Aggregates across all agent labels, filtering to _total samples only
251251
to avoid counting _created timestamp samples.
252+
253+
COUPLING NOTE: This method assumes failure status is labeled "error".
254+
See execution.py lines ~310,331 where status labels are set.
255+
If new status values are added (e.g., "timeout", "cancelled"),
256+
update the failure detection here or switch to success-allowlist.
252257
"""
253258
total = 0
254259
failed = 0
@@ -260,7 +265,7 @@ def _get_request_counts(self) -> tuple[int, int]:
260265
total += val
261266
if sample.labels.get("status") == "error":
262267
failed += val
263-
except (IndexError, AttributeError):
268+
except (IndexError, AttributeError, ValueError, OverflowError):
264269
pass
265270
return total, failed
266271

@@ -407,7 +412,8 @@ def _estimate_p99(self) -> Optional[float]:
407412
if bucket_fraction <= 0:
408413
return le
409414
remaining = target_count - prev_count
410-
return prev_le + bucket_width * (remaining / bucket_fraction)
415+
result = prev_le + bucket_width * (remaining / bucket_fraction)
416+
return result if math.isfinite(result) else le
411417
prev_le = le
412418
prev_count = bucket_count
413419

@@ -422,4 +428,4 @@ def _estimate_p99(self) -> Optional[float]:
422428

423429
def to_json(self) -> str:
424430
"""Return evaluation result as JSON string."""
425-
return json.dumps(self.evaluate(), default=str, indent=2)
431+
return json.dumps(self.evaluate(), default=str, indent=2, allow_nan=False)

0 commit comments

Comments
 (0)