Accept logs without upstream timing

This commit is contained in:
Jordan Wages 2026-09-17 18:14:53 -05:00
commit 61630b4adb
2 changed files with 11 additions and 2 deletions

View file

@ -60,7 +60,9 @@ def percentile(values: list[float], p: float) -> float | None:
def parse_line(line: str) -> dict[str, Any] | None:
"""Parse the configured, whitespace-delimited Nginx traffic format."""
fields = line.strip().split()
if len(fields) < 8:
# A trailing empty $upstream_response_time disappears when splitting on
# whitespace, so seven fields is valid and means "no upstream timing".
if len(fields) < 7:
return None
try:
timestamp = float(fields[0])
@ -68,7 +70,7 @@ def parse_line(line: str) -> dict[str, Any] | None:
request_length = int(fields[4])
bytes_sent = int(fields[5])
request_time = float(fields[6])
upstream_raw = "".join(fields[7:])
upstream_raw = "".join(fields[7:]) if len(fields) > 7 else "-"
upstream_values = [float(value) for value in upstream_raw.split(",") if value not in ("", "-")]
if not all(math.isfinite(value) and value >= 0 for value in
(timestamp, request_length, bytes_sent, request_time, *upstream_values)):

View file

@ -10,6 +10,13 @@ def test_parse_line_and_multiple_upstreams():
assert event["upstream_time"] == 0.15
def test_parse_line_with_empty_trailing_upstream_time():
event = parse_line("1720000000.123 example.test GET 200 120 240 0.150")
assert event is not None
assert event["host"] == "example.test"
assert event["upstream_time"] is None
def test_malformed_lines_are_ignored():
assert parse_line("not enough fields") is None
assert parse_line("1 host GET nope 1 2 0.1 -") is None