Build Nginx live traffic monitor
This commit is contained in:
parent
2da9b83907
commit
fb66a69bd7
9 changed files with 525 additions and 2 deletions
28
tests/test_main.py
Normal file
28
tests/test_main.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import time
|
||||
|
||||
from app.main import Telemetry, parse_line
|
||||
|
||||
|
||||
def test_parse_line_and_multiple_upstreams():
|
||||
event = parse_line("1720000000.123 example.test GET 200 120 240 0.150 0.100, 0.200")
|
||||
assert event["host"] == "example.test"
|
||||
assert event["incoming"] == 120
|
||||
assert event["upstream_time"] == 0.15
|
||||
|
||||
|
||||
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
|
||||
assert parse_line("1 host GET 600 1 2 0.1 -") is None
|
||||
|
||||
|
||||
def test_telemetry_is_aggregated_and_bounded():
|
||||
telemetry = Telemetry(60)
|
||||
base = int(time.time()) - 30
|
||||
for second in range(70):
|
||||
telemetry.ingest(parse_line(f"{base + second} host GET 200 10 20 .1 -"))
|
||||
assert telemetry.total_incoming == 700
|
||||
assert len(telemetry.global_series.buckets) <= 60
|
||||
snapshot = telemetry.snapshot(60)
|
||||
assert snapshot["summary"]["requests"] >= 1
|
||||
assert snapshot["hosts"] == ["host"]
|
||||
Loading…
Reference in a new issue